Documentation
¶
Overview ¶
Package test has a HTTP test server of an API, which records every request and response, and checks them against the API's OpenAPI specification when the test ends, for the tests of go-server's packages, and of the servers which use them
Index ¶
- Constants
- type Exchange
- type Server
- func (s *Server) Do(t *testing.T, method, path string, body any) (int, []byte)
- func (s *Server) DoWithContentType(t *testing.T, method, path, contentType string, body any) (int, []byte)
- func (s *Server) Exchanges() []Exchange
- func (s *Server) Head(t *testing.T, path string, header http.Header) (int, http.Header)
- func (s *Server) JSON(t *testing.T, method, path string, body, v any) int
- func (s *Server) Request(t *testing.T, method, path string, header http.Header) (int, http.Header, []byte)
- func (s *Server) Reset()
- func (s *Server) Send(t *testing.T, method, path string, header http.Header, body []byte) (int, http.Header, []byte)
- func (s *Server) Spec() *openapi.Spec
- func (s *Server) Verify() []error
Constants ¶
const Prefix = "/api"
Prefix is the path prefix for the API routes
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Exchange ¶
type Exchange struct {
Method string
Path string
// Pattern is the route pattern which matched the request, such as
// "/api/changes", or empty if no route matched
Pattern string
RequestHeader http.Header
RequestBody []byte
RequestErr error // reading the request's body, or the request's context, when the client went away
Status int
ResponseHeader http.Header
ResponseBody []byte
}
Exchange is a recorded request and response
type Server ¶
Server is a HTTP test server of an API, which a function registers with a router. Every request and response is recorded, and checked against the router's OpenAPI specification when the test ends.
func NewServer ¶
NewServer starts a HTTP test server serving the API which the function registers with a router, at Prefix. When the test ends the server is closed, and every recorded exchange is checked against the router's OpenAPI specification.
func (*Server) Do ¶
Do sends a request to the API with a JSON body, and returns the status code and response body. The path is relative to the API prefix, and may include a query string. A body which is a string or []byte is sent as-is, nil sends no body, and any other value is encoded as JSON.
func (*Server) DoWithContentType ¶
func (s *Server) DoWithContentType(t *testing.T, method, path, contentType string, body any) (int, []byte)
DoWithContentType sends a request with the body as the content type, which is used as-is for a string or []byte body, and otherwise encoded as JSON. Returns the status code and the response body.
func (*Server) Head ¶
Head sends a HEAD request to the API with the headers, and returns the status code and response headers. The path is relative to the API prefix.
func (*Server) JSON ¶
JSON sends a request to the API as Do does, and decodes a successful response body into v. Returns the status code.
func (*Server) Request ¶
func (s *Server) Request(t *testing.T, method, path string, header http.Header) (int, http.Header, []byte)
Request sends a request to the API without a body, with the headers, and returns the status code, response headers and response body. The path is relative to the API prefix, and may include a query string.
func (*Server) Send ¶
func (s *Server) Send(t *testing.T, method, path string, header http.Header, body []byte) (int, http.Header, []byte)
Send sends a request to the API with the headers and body, which is sent when it isn't nil, and returns the status code, response headers and response body. The path is relative to the API prefix, and may include a query string.