<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ## <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) } } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看