Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 为什么不';当我使用http.NewRequest进行测试时,我的请求URL解析是否正确?_Go_Testing - Fatal编程技术网

Go 为什么不';当我使用http.NewRequest进行测试时,我的请求URL解析是否正确?

Go 为什么不';当我使用http.NewRequest进行测试时,我的请求URL解析是否正确?,go,testing,Go,Testing,当我使用curl测试我的/health/endpoint时,一切正常: curl localhost:8080/health/my\u id返回my\u id 但是当我运行测试时,处理程序无法从参数中提取ID。 我应该如何从测试中精心设计查询来实现这一点 健康测试 12 func TestHealth(t *testing.T) { 13 14 // Initialize a new httptest.ResponseRecorder. 15 rr :=

当我使用curl测试我的/health/endpoint时,一切正常:
curl localhost:8080/health/my\u id
返回
my\u id

但是当我运行测试时,处理程序无法从参数中提取ID。 我应该如何从测试中精心设计查询来实现这一点

健康测试

 12 func TestHealth(t *testing.T) {
 13
 14         // Initialize a new httptest.ResponseRecorder.
 15         rr := httptest.NewRecorder()
 16
 17         // Initialize a new dummy http.Request.
 18         r, err := http.NewRequest(http.MethodGet, "/health/my_id", nil)
 19         if err != nil {
 20                 t.Fatal(err)
 21         }
 22
 23         // Call the handler function, passing in the
 24         // httptest.ResponseRecorder and http.Request.
 25         handler.HandleHealth(rr, r)
 26
 27         // Call the Result() method on the http.ResponseRecorder to get the
 28         // http.Response generated by the handler.
 29         rs := rr.Result()
 30
 31         // We can then examine the http.Response to check that the status code // written by the handler was 200.
 32         if rs.StatusCode != http.StatusOK {
 33                 t.Errorf("want %d; got %d", http.StatusOK, rs.StatusCode)
 34         }
 35
 36         // And we can check that the response body written by the handler
 37         defer rs.Body.Close()
 38         body, err := ioutil.ReadAll(rs.Body)
 39         if err != nil {
 40                 t.Fatal(err)
 41         }
 42
 43         want := "my_id"
 44         got := string(body)
 45         if got != want {
 46                 t.Errorf("want body to equal %q. Got: %q", want, got)
 47         }
 48 }
运行该测试将导致:

internal/handler/health/handler_test.go|46| want body to equal "my_id". Got: ""
HealthHandlers

   23 func (h *Handler) HandleHealth(w http.ResponseWriter, r *http.Request) {
   24         id := chi.URLParam(r, "id")
   25         log.Println("ID: ", id)
   26         render.PlainText(w, r, id)
   27 }
   28

   29 func RegisterRoutes(router *chi.Mux, handler *Handler) {
   30         router.Get("/health/{id}", handler.HandleHealth)
   31 }

chi库处理通配符URL。但是,通过直接调用
handlehalth
,您在测试中绕过了chi

如果希望chi根据
RegisterRoutes
方法中提供的通配符处理请求,显然需要在测试中实际调用
RegisterRoutes

这样做需要稍微更改测试结构。而不是:

handlehalth.handlehalth(rr,r)
您将需要以下内容:

chi:=chi.NewMux()
注册表项(chi、handler)
气动伺服TTP(右后,右后)

也与您的问题无关,但有几个提示:对于测试,请使用
httptest.NewRequest()
而不是
http.NewRequest()
。而且,这不是一个“虚拟”请求。这是一个真实的、100%真实的、成熟的、正常的请求。