多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
## <span style="font-size:15px">**1、httptest测试客户端请求**</span> <p style="display:none">https://blog.csdn.net/neweastsun/article/details/129528959</p> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;对于HTTP客户端测试,需要模拟出http服务端的响应;可以通过调用`httptest.NewServer`函数生成我们想要的 httptest.Server。NewServer 启动并返回新的HTTP服务,调用者使用完成后应该调用Close方法结束服务. ``` func TestServer(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, "Hello, client") })) defer ts.Close() // Do something with ts.URL, such as issuing requests client := http.Client{} resp, _ := client.Get(ts.URL) defer resp.Body.Close() respBody, _ := ioutil.ReadAll(resp.Body) if string(respBody) != "Hello, client\n" { t.Errorf("Unexpected body: %q", respBody) } } ``` &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;上面示例中使用httptest.NewServer函数创建了模拟http服务器,传入自定义的模拟处理器,返回“Hello, client”。并使用服务端url作为客户端请求url,从而模拟并让服务端返回任何我们想测试的内容。 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;也可以修改处理器的逻辑,自定义处理响应结果 ``` func TestServer(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { query, err := url.ParseQuery(r.URL.RawQuery) if err != nil { w.WriteHeader(http.StatusBadRequest) fmt.Fprintf(w, "invalid request") return } word := query.Get("requestId") fmt.Fprintf(w, requestId) })) defer ts.Close() // Do something with ts.URL, such as issuing requests client := http.Client{} resp, _ := client.Get(ts.URL) defer resp.Body.Close() respBody, _ := ioutil.ReadAll(resp.Body) if string(respBody) != "Hello, client\n" { t.Errorf("Unexpected body: %q", respBody) } } ``` 2、gorilla/mux+httptest 进行http请求测试 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;另一种创建mock的http服务的方法是使用gorilla/mux包。它是一个基于net/http包的路由器和调度程序,可以用于处理具有多个路由的复杂请求。 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;使用mux.NewRouter函数创建了一个路由器,并将请求路由到myHandler函数来处理。然后,我们使用httptest.NewServer函数创建了一个mock的http服务,并在测试中向该服务发起请求,验证响应是否符合预期。 ``` func TestMux(t *testing.T) { r := mux.NewRouter() r.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, "Hello, client") }) ts := httptest.NewServer(r) defer ts.Close() // Do something with ts.URL, such as issuing requests client := http.Client{} resp, _ := client.Get(ts.URL + "/hello") defer resp.Body.Close() respBody, _ := ioutil.ReadAll(resp.Body) if string(respBody) != "Hello, client\n" { t.Errorf("Unexpected body: %q", respBody) } } ``` 如果需要创建多个路由,可以在mux.NewRouter函数中添加多个路由。 ``` func TestMux(t *testing.T) { r := mux.NewRouter() r.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, "Hello, client") }) r.HandleFunc("/world", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, "Hello, world") }) //r.PathPrefix("/products/") //前缀匹配 //r.Methods("GET", "POST") //请求方法匹配 //r.Schemes("https") //schemes //r.Headers("X-Requested-With", "XMLHttpRequest") //header 匹配 //r.Queries("key", "value") //query的值匹配 ts := httptest.NewServer(r) defer ts.Close() // Do something with ts.URL, such as issuing requests client := http.Client{} resp, _ := client.Get(ts.URL + "/hello") defer resp.Body.Close() respBody, _ := ioutil.ReadAll(resp.Body) if string(respBody) != "Hello, client\n" { t.Errorf("Unexpected body: %q", respBody) } } ```