Documentation ¶
Overview ¶
Package httptestclient generates and executes standard http requests. It follows the basic builder pattern to build and/or execute the request against a httptest.Server Any errors that occur during the build or execute of the request will fail the test so that you can focus on your server not on the http client
Index ¶
- Constants
- Variables
- type Client
- func (c *Client) BodyBytes(body []byte) *Client
- func (c *Client) BodyJSON(payload interface{}) *Client
- func (c *Client) BodyString(body string) *Client
- func (c *Client) BuildRequest() *http.Request
- func (c *Client) ClearHeaders() *Client
- func (c *Client) Context(ctx context.Context) *Client
- func (c *Client) Delete(url string, args ...interface{}) *Client
- func (c *Client) Do(server *httptest.Server) *http.Response
- func (c *Client) DoSimple(server *httptest.Server) SimpleResponse
- func (c *Client) ExpectRedirectTo(path string) *Client
- func (c *Client) ExpectedStatusCode(status int) *Client
- func (c *Client) FormData(args ...string) *Client
- func (c *Client) Get(url string, args ...interface{}) *Client
- func (c *Client) Header(name, value string, moreValues ...string) *Client
- func (c *Client) Method(method string) *Client
- func (c *Client) Patch(url string, args ...interface{}) *Client
- func (c *Client) Post(url string, args ...interface{}) *Client
- func (c *Client) Put(url string, args ...interface{}) *Client
- func (c *Client) URL(url string, args ...interface{}) *Client
- type SimpleResponse
- type TestingT
Constants ¶
const ContentTypeApplicationJson = "application/json"
ContentTypeApplicationJson for http header Content-Type
const UserAgent = "test-http-request"
UserAgent default value
Variables ¶
var ( // DefaultContentType when content is detected DefaultContentType = ContentTypeApplicationJson )
var ErrNilBodyJSON = errors.New("BodyJson requires non nil value")
ErrNilBodyJSON sentinel error
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct { // MaxRedirects, (per request) default is 10 MaxRedirects int // contains filtered or unexported fields }
Client simplifies creating test client http request
func New ¶
New for testing, finish with Client.Do or Client.DoSimple `t` use `*testing.T` `s` server under test example:
resp2 := New(t). Post("/some-request/%s/resource", id). Header("special-header", "magic"). BodyString(`{"token":"opaque-string"}`). Do(server)
func (*Client) BodyString ¶
BodyString is a literal string version of the body if the body has a value the content-type will be 'application/json' will be added unless you set an alternative or use ClearHeaders()
func (*Client) BuildRequest ¶
BuildRequest a raw unsent request
func (*Client) ClearHeaders ¶
ClearHeaders removes default http headers, Accept, Content-Type, User-Agent. Must be called before adding other headers
func (*Client) Context ¶
Context when sending the request, defaults to `context.Context()` if not set
func (*Client) DoSimple ¶
func (c *Client) DoSimple(server *httptest.Server) SimpleResponse
DoSimple performs as Do but reads any response payload to a string
func (*Client) ExpectRedirectTo ¶
ExpectRedirectTo a specific url
func (*Client) ExpectedStatusCode ¶
ExpectedStatusCode for the test to pass. By default, any 2xx will pass otherwise explicitly state the success status do not use this to expect redirects, see ExpectRedirectTo
func (*Client) FormData ¶
FormData for posting x-www-form-urlencoded forms args is expected to be pairs of key:values
func (*Client) Header ¶
Header for request, can be called multiple times and is additive unless using the same header name, then it overwrites
type SimpleResponse ¶
type SimpleResponse struct { Header http.Header Body string Status int RedirectedVia string Response *http.Response // contains filtered or unexported fields }
SimpleResponse simplified status response rather than using the http.Response directly
func (SimpleResponse) BodyJSON ¶
func (r SimpleResponse) BodyJSON(payload interface{})
BodyJSON uses json.Unmarshal to map the Body to the struct