如何结束 end/integration 测试使用反向代理管理子域的 Go 应用程序?
How to end to end/integration test a Go app that use a reverse proxy to manage subdomain?
我有一个使用 Gin gonic 的 Go 应用程序和一个 Nginx 反向代理,它将流量发送到 domain.com 上的另一个应用程序,并将所有 *.domain.com 子域流量直接发送到我的 go 应用程序。
我的 Go 应用程序然后有一个中间件,它将读取 nginx 从 Context 传递给它的主机名,并允许我的处理程序知道正在请求哪个子域以及 return 所述子域的正确数据和 cookie。
这是一个非常简单的设置,从我在邮递员中的测试来看它似乎工作正常,因为我的所有路由在我的所有子域中都是相同的所以这样我只能为所有子域使用一个路由器而不是每个路由器一个路由器subodmain.
现在,当我尝试进行端到端测试时,我的大问题来了。
我正在这样设置我的测试:
router := initRouter()
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/api/login", bytes.NewBuffer(jsonLogin))
req.Header.Set("Content-Type", "application/json")
router.ServeHTTP(w, req)
assert.Equal(t, 200, w.Code)
使用 initRouter()
returning 一个 gin 引擎,加载了我所有的路由和中间件,其余的作为基本测试设置。
显然测试会失败,因为 gin Context 永远不会从 context 接收子域,并且就像所有内容都来自 localhost:8000.
有没有办法:
“模拟”一个子域,以便路由器认为呼叫来自 foo.localhost.com 而不是本地主机
设置我的测试服,以便通过 nginx 路由测试请求。我更喜欢解决方案 1,因为这样设置/维护起来会很麻烦。
编辑:
根据 httptest 文档,我尝试将 foo.localhost 硬编码为 NewRequest 的参数,但它的行为并不像我需要的那样:
NewRequest returns a new incoming server Request, suitable for passing to an http.Handler for testing.
The target is the RFC 7230 "request-target": it may be either a path or an absolute URL. If target is an absolute URL, the host name from the URL is used. Otherwise, "example.com" is used.
当将 http://foo.localhost.com/api/login 或 foo.localhost.com/api/login 硬编码为请求目标时,它直接将其传递到“foo.localhost.com/api/login”下的我的路由器,而nginx 会直接点击 /api/login 并从 c.Request.Host
解析
编辑 2:
我目前正在探索使用手动设置主机:
req.Header.Set("Host", "foo.localhost")
http.NewRequest 返回的请求不适合直接传递给 ServeHTTP。使用 httptest.NewRequest 返回的一个。
直接设置the Host field即可:
package main
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestHelloWorld(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.Host != "foobar" {
t.Errorf("Host is %q, want foobar", r.Host)
}
})
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/api/login", nil)
r.Host = "foobar"
mux.ServeHTTP(w, r)
}
我有一个使用 Gin gonic 的 Go 应用程序和一个 Nginx 反向代理,它将流量发送到 domain.com 上的另一个应用程序,并将所有 *.domain.com 子域流量直接发送到我的 go 应用程序。
我的 Go 应用程序然后有一个中间件,它将读取 nginx 从 Context 传递给它的主机名,并允许我的处理程序知道正在请求哪个子域以及 return 所述子域的正确数据和 cookie。
这是一个非常简单的设置,从我在邮递员中的测试来看它似乎工作正常,因为我的所有路由在我的所有子域中都是相同的所以这样我只能为所有子域使用一个路由器而不是每个路由器一个路由器subodmain.
现在,当我尝试进行端到端测试时,我的大问题来了。
我正在这样设置我的测试:
router := initRouter()
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/api/login", bytes.NewBuffer(jsonLogin))
req.Header.Set("Content-Type", "application/json")
router.ServeHTTP(w, req)
assert.Equal(t, 200, w.Code)
使用 initRouter()
returning 一个 gin 引擎,加载了我所有的路由和中间件,其余的作为基本测试设置。
显然测试会失败,因为 gin Context 永远不会从 context 接收子域,并且就像所有内容都来自 localhost:8000.
有没有办法:
“模拟”一个子域,以便路由器认为呼叫来自 foo.localhost.com 而不是本地主机
设置我的测试服,以便通过 nginx 路由测试请求。我更喜欢解决方案 1,因为这样设置/维护起来会很麻烦。
编辑:
根据 httptest 文档,我尝试将 foo.localhost 硬编码为 NewRequest 的参数,但它的行为并不像我需要的那样:
NewRequest returns a new incoming server Request, suitable for passing to an http.Handler for testing.
The target is the RFC 7230 "request-target": it may be either a path or an absolute URL. If target is an absolute URL, the host name from the URL is used. Otherwise, "example.com" is used.
当将 http://foo.localhost.com/api/login 或 foo.localhost.com/api/login 硬编码为请求目标时,它直接将其传递到“foo.localhost.com/api/login”下的我的路由器,而nginx 会直接点击 /api/login 并从 c.Request.Host
编辑 2:
我目前正在探索使用手动设置主机:
req.Header.Set("Host", "foo.localhost")
http.NewRequest 返回的请求不适合直接传递给 ServeHTTP。使用 httptest.NewRequest 返回的一个。
直接设置the Host field即可:
package main
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestHelloWorld(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.Host != "foobar" {
t.Errorf("Host is %q, want foobar", r.Host)
}
})
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/api/login", nil)
r.Host = "foobar"
mux.ServeHTTP(w, r)
}