Package httptest

import "net/http/httptest"
Overview
Index
Examples

Overview ▾

包httptest提供了用于HTTP测试的实用程序.

Constants

如果未在ResponseRecorder上设置显式的DefaultRemoteAddr,则DefaultRemoteAddr是要在RemoteAddr中返回的默认远程地址.

const DefaultRemoteAddr = "1.2.3.4"

func NewRequest 1.7

func NewRequest(method, target string, body io.Reader) *http.Request

NewRequest返回一个新的传入服务器请求,适合传递给http.Handler进行测试.

目标是RFC 7230"请求目标":它可以是路径或绝对URL. 如果目标是绝对URL,则使用URL中的主机名. 否则,将使用" example.com".

如果目标的方案为" https",则TLS字段将设置为非null的伪值.

Request.Proto始终为HTTP / 1.1.

空方法表示" GET".

提供的主体可以为零. 如果主体的类型为* bytes.Reader,* strings.Reader或* bytes.Buffer,则设置Request.ContentLength.

对于错误,NewRequest会出现恐慌,以便于在容易接受的情况下进行测试.

要生成客户端HTTP请求而不是服务器请求,请参见net / http包中的NewRequest函数.

type ResponseRecorder

ResponseRecorder是http.ResponseWriter的实现,记录了它的突变以供以后在测试中检查.

type ResponseRecorder struct {
    // Code is the HTTP response code set by WriteHeader.
    //
    // Note that if a Handler never calls WriteHeader or Write,
    // this might end up being 0, rather than the implicit
    // http.StatusOK. To get the implicit value, use the Result
    // method.
    Code int

    // HeaderMap contains the headers explicitly set by the Handler.
    // It is an internal detail.
    //
    // Deprecated: HeaderMap exists for historical compatibility
    // and should not be used. To access the headers returned by a handler,
    // use the Response.Header map as returned by the Result method.
    HeaderMap http.Header

    // Body is the buffer to which the Handler's Write calls are sent.
    // If nil, the Writes are silently discarded.
    Body *bytes.Buffer

    // Flushed is whether the Handler called Flush.
    Flushed bool
    // contains filtered or unexported fields
}

Example

200
text/html; charset=utf-8
<html><body>Hello World!</body></html>

func NewRecorder

func NewRecorder() *ResponseRecorder

NewRecorder返回一个初始化的ResponseRecorder.

func (*ResponseRecorder) Flush

func (rw *ResponseRecorder) Flush()

Flush实现http.Flusher. 要测试是否调用了Flush,请参阅rw.Flushed.

func (*ResponseRecorder) Header

func (rw *ResponseRecorder) Header() http.Header

标头实现http.ResponseWriter. 它返回响应标头以在处理程序中进行更改. 若要测试在处理程序完成后编写的标头,请使用Result方法并查看返回的Response值的Header.

func (*ResponseRecorder) Result 1.7

func (rw *ResponseRecorder) Result() *http.Response

结果返回处理程序生成的响应.

返回的Response将至少填充其StatusCode,Header,Body和可选的Trailer. 将来可能会填充更多字段,因此调用方不应在测试中对结果进行DeepEqual.

如果处理程序从未执行过写,则Response.Header是在第一次写调用时或在此调用时标头的快照.

确保Response.Body为非nil,确保Body.Read调用不返回io.EOF以外的任何错误.

仅在处理程序完成运行后才调用结果.

func (*ResponseRecorder) Write

func (rw *ResponseRecorder) Write(buf []byte) (int, error)

编写工具http.ResponseWriter. 如果buf中的数据不为nil,则将其写入rw.Body.

func (*ResponseRecorder) WriteHeader

func (rw *ResponseRecorder) WriteHeader(code int)

WriteHeader实现http.ResponseWriter.

func (*ResponseRecorder) WriteString 1.6

func (rw *ResponseRecorder) WriteString(str string) (int, error)

WriteString实现io.StringWriter. 如果不为nil,则str中的数据将写入rw.Body.

type Server

服务器是侦听本地回送接口上系统选择的端口的HTTP服务器,用于端到端HTTP测试.

type Server struct {
    URL      string // base URL of form http://ipaddr:port with no trailing slash
    Listener net.Listener

    // TLS is the optional TLS configuration, populated with a new config
    // after TLS is started. If set on an unstarted server before StartTLS
    // is called, existing fields are copied into the new config.
    TLS *tls.Config

    // Config may be changed after calling NewUnstartedServer and
    // before Start or StartTLS.
    Config *http.Server
    // contains filtered or unexported fields
}

Example

Hello, client

func NewServer

func NewServer(handler http.Handler) *Server

NewServer启动并返回新服务器. 完成后,呼叫者应致电Close来关闭它.

func NewTLSServer

func NewTLSServer(handler http.Handler) *Server

NewTLSServer启动并使用TLS返回新的服务器. 完成后,呼叫者应致电Close来关闭它.

Example

Hello, client

func NewUnstartedServer

func NewUnstartedServer(handler http.Handler) *Server

NewUnstartedServer返回一个新服务器,但不启动它.

更改其配置后,调用方应调用Start或StartTLS.

完成后,呼叫者应致电Close来关闭它.

func (*Server) Certificate 1.9

func (s *Server) Certificate() *x509.Certificate

证书返回服务器使用的证书,如果服务器不使用TLS,则返回null.

func (*Server) Client 1.9

func (s *Server) Client() *http.Client

客户端返回配置用于向服务器发出请求的HTTP客户端. 它被配置为信任服务器的TLS测试证书,并将在Server.Close上关闭其空闲连接.

func (*Server) Close

func (s *Server) Close()

关闭将关闭服务器并阻塞,直到该服务器上的所有未完成请求都已完成.

func (*Server) CloseClientConnections

func (s *Server) CloseClientConnections()

CloseClientConnections关闭与测试服务器的所有打开的HTTP连接.

func (*Server) Start

func (s *Server) Start()

启动从NewUnstartedServer启动服务器.

func (*Server) StartTLS

func (s *Server) StartTLS()

StartTLS从NewUnstartedServer启动服务器上的TLS.

by  ICOPY.SITE