<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之旅 廣告
                [TOC] ## 語法 ``` func NewRequest(method, target string, body io.Reader) *http.Request type ResponseRecorder func NewRecorder() *ResponseRecorder func (rw *ResponseRecorder) Flush() func (rw *ResponseRecorder) Header() http.Header func (rw *ResponseRecorder) Result() *http.Response func (rw *ResponseRecorder) Write(buf []byte) (int, error) func (rw *ResponseRecorder) WriteHeader(code int) func (rw *ResponseRecorder) WriteString(str string) (int, error) type Server func NewServer(handler http.Handler) *Server func NewTLSServer(handler http.Handler) *Server func NewUnstartedServer(handler http.Handler) *Server func (s *Server) Certificate() *x509.Certificate func (s *Server) Client() *http.Client func (s *Server) Close() func (s *Server) CloseClientConnections() func (s *Server) Start() func (s *Server) StartTLS() ``` ## 示例 ### httptest.NewRequest 創建請求頭 ``` httptest.NewRequest("GET","http://127.0.0.1/foo",nil) ``` ### 兩種模擬http 服務器的方式 #### 訪問模擬的server ``` func Api(w http.ResponseWriter, r *http.Request) { io.WriteString(w, "hello world") } // test func TestApi(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(Api)) defer server.Close() res, err := http.Get(server.URL) if err != nil { t.Log(err) } all, err := ioutil.ReadAll(res.Body) if err != nil { t.Log(err) } if string(all) != "hello world" { t.Error("返回值不正確") } } ``` #### Recorder ``` func Api(w http.ResponseWriter, r *http.Request) { io.WriteString(w, "hello world") } // test func TestApi(t *testing.T) { // 域名可以隨便寫,參數可以按照實際需求寫 req := httptest.NewRequest("GET", "http://127.0.0.1:8010/foo", nil) recorder := httptest.NewRecorder() Api(recorder, req) all, err := ioutil.ReadAll(recorder.Body) if err != nil { t.Error(err) } t.Log(string(all)) } ``` ### http2 查看官方的 httptest 的 example_test.go ### 支持 TLS 查看官方的 httptest 的 example_test.go
                  <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>

                              哎呀哎呀视频在线观看