🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# package httptest `import "net/http/httptest"` httptest包提供了HTTP测试的常用函数。 ## Index * [Constants](#pkg-constants) * [type ResponseRecorder](#ResponseRecorder) * [func NewRecorder() \*ResponseRecorder](#NewRecorder) * [func (rw \*ResponseRecorder) Header() http.Header](#ResponseRecorder.Header) * [func (rw \*ResponseRecorder) WriteHeader(code int)](#ResponseRecorder.WriteHeader) * [func (rw \*ResponseRecorder) Write(buf []byte) (int, error)](#ResponseRecorder.Write) * [func (rw \*ResponseRecorder) Flush()](#ResponseRecorder.Flush) * [type Server](#Server) * [func NewServer(handler http.Handler) \*Server](#NewServer) * [func NewTLSServer(handler http.Handler) \*Server](#NewTLSServer) * [func NewUnstartedServer(handler http.Handler) \*Server](#NewUnstartedServer) * [func (s \*Server) Start()](#Server.Start) * [func (s \*Server) StartTLS()](#Server.StartTLS) * [func (s \*Server) CloseClientConnections()](#Server.CloseClientConnections) * [func (s \*Server) Close()](#Server.Close) ### Examples * [ResponseRecorder](#example-ResponseRecorder) * [Server](#example-Server) ## Constants ``` const DefaultRemoteAddr = "1.2.3.4" ``` DefaultRemoteAddr是默认的远端地址。如果ResponseRecorder未显式的设置该属性,RemoteAddr方法就会返回该值。 ## type [ResponseRecorder](http://code.google.com/p/go/source/browse/src/pkg/net/http/httptest/recorder.go?name=release#15 "View Source") ``` type ResponseRecorder struct { Code int // HTTP回复的状态码 HeaderMap http.Header // HTTP回复的头域 Body *bytes.Buffer // 如非nil,会将Write方法写入的数据写入bytes.Buffer Flushed bool // 内含隐藏或非导出字段 } ``` ResponseRecorder实现了http.ResponseWriter接口,它记录了其修改,用于之后的检查。 Example ``` handler := func(w http.ResponseWriter, r *http.Request) { http.Error(w, "something failed", http.StatusInternalServerError) } req, err := http.NewRequest("GET", "http://example.com/foo", nil) if err != nil { log.Fatal(err) } w := httptest.NewRecorder() handler(w, req) fmt.Printf("%d - %s", w.Code, w.Body.String()) ``` Output: ``` 500 - something failed ``` ### func [NewRecorder](http://code.google.com/p/go/source/browse/src/pkg/net/http/httptest/recorder.go?name=release#25 "View Source") ``` func NewRecorder() *ResponseRecorder ``` NewRecorder返回一个初始化了的ResponseRecorder. ### func (\*ResponseRecorder) [Header](http://code.google.com/p/go/source/browse/src/pkg/net/http/httptest/recorder.go?name=release#38 "View Source") ``` func (rw *ResponseRecorder) Header() http.Header ``` Header返回回复的头域,即Header字段。 ### func (\*ResponseRecorder) [WriteHeader](http://code.google.com/p/go/source/browse/src/pkg/net/http/httptest/recorder.go?name=release#59 "View Source") ``` func (rw *ResponseRecorder) WriteHeader(code int) ``` WriteHeader设置rw.Code。 ### func (\*ResponseRecorder) [Write](http://code.google.com/p/go/source/browse/src/pkg/net/http/httptest/recorder.go?name=release#48 "View Source") ``` func (rw *ResponseRecorder) Write(buf []byte) (int, error) ``` Write总是成功,如果rw.Body非nil会把数据写入该字段。 ### func (\*ResponseRecorder) [Flush](http://code.google.com/p/go/source/browse/src/pkg/net/http/httptest/recorder.go?name=release#67 "View Source") ``` func (rw *ResponseRecorder) Flush() ``` Flush将rw.Flushed设置为真。 ## type [Server](http://code.google.com/p/go/source/browse/src/pkg/net/http/httptest/server.go?name=release#21 "View Source") ``` type Server struct { URL string // 格式为http://ipaddr:port,没有末尾斜杠的基地址 Listener net.Listener // TLS是可选的TLS配置,在TLS开始后会填写为新的配置。 // 如果在未启动的Server调用StartTLS方法前设置,已经存在的字段会拷贝进新配置里。 TLS *tls.Config // Config可能会在调用Start/StartTLS方法之前调用NewUnstartedServer时被修改。 Config *http.Server // 内含隐藏或非导出字段 } ``` Server是一个HTTP服务端,在本地环回接口的某个系统选择的端口监听,用于点对点HTTP测试。 Example ``` ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, "Hello, client") })) defer ts.Close() res, err := http.Get(ts.URL) if err != nil { log.Fatal(err) } greeting, err := ioutil.ReadAll(res.Body) res.Body.Close() if err != nil { log.Fatal(err) } fmt.Printf("%s", greeting) ``` Output: ``` Hello, client ``` ### func [NewServer](http://code.google.com/p/go/source/browse/src/pkg/net/http/httptest/server.go?name=release#82 "View Source") ``` func NewServer(handler http.Handler) *Server ``` NewServer返回一个新的、已启动的Server。调用者必须在用完时调用Close方法关闭它。 ### func [NewTLSServer](http://code.google.com/p/go/source/browse/src/pkg/net/http/httptest/server.go?name=release#158 "View Source") ``` func NewTLSServer(handler http.Handler) *Server ``` NewTLSServer返回一个新的、使用TLS的、已启动的Server。调用者必须在用完时调用Close方法关闭它。 ### func [NewUnstartedServer](http://code.google.com/p/go/source/browse/src/pkg/net/http/httptest/server.go?name=release#94 "View Source") ``` func NewUnstartedServer(handler http.Handler) *Server ``` NewUnstartedServer返回一个新的、未启动的Server。在修改其配置后,调用者应该调用Start或StartTLS启动它;调在用完时用者必须调用Close方法关闭它。 ### func (\*Server) [Start](http://code.google.com/p/go/source/browse/src/pkg/net/http/httptest/server.go?name=release#102 "View Source") ``` func (s *Server) Start() ``` Start启动NewUnstartedServer返回的服务端。 ### func (\*Server) [StartTLS](http://code.google.com/p/go/source/browse/src/pkg/net/http/httptest/server.go?name=release#117 "View Source") ``` func (s *Server) StartTLS() ``` StartTLS启动NewUnstartedServer函数返回的服务端的TLS监听。 ### func (\*Server) [CloseClientConnections](http://code.google.com/p/go/source/browse/src/pkg/net/http/httptest/server.go?name=release#177 "View Source") ``` func (s *Server) CloseClientConnections() ``` CloseClientConnections关闭当前任何与该服务端建立的HTTP连接。 ### func (\*Server) [Close](http://code.google.com/p/go/source/browse/src/pkg/net/http/httptest/server.go?name=release#166 "View Source") ``` func (s *Server) Close() ``` Close关闭服务端,并阻塞直到所有该服务端未完成的请求都结束为止。