httptest

package
v0.0.0-...-3597e88 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 9, 2020 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package httptest enhances the stdlib net/http/httptest package by integrating it with gopkg.in/gavv/httpexpect.v1, reducing the boilerplate needed for http tests. In addition to the functions that make it easier to stand up a test server, this package also provides a set of tools to make it easier to mock out http client responses.

Test Servers vs. Client mocking

When building a testing fixture for HTTP, you can approach the problem in two ways: Use a mocked server and make real http requests to the server, or use a mocked client and have _no_ server. While this package provides facilities for both, we recommend that you follow the conventions for decided which to use in your tests.

The test server system should be used when the object under test is our own server code; usually that means our own http.Handler implementations. The mocked client system should be used when the object under test is making http requests.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	*http.Client
	*httpmock.MockTransport
}

Client represents an easier way to mock http client behavior. It assumes that your packages use interfaces to store an http client instead of a concrete *http.Client.

func NewClient

func NewClient() *Client

NewClient returns a new mocked http client. A value being tested can be configured to use this http client allowing the tester to control the server responses without needing to run an actual server.

func (*Client) On

func (c *Client) On(method string, url string) *ClientExpectation

On is the entrypoint method into this packages "client" mocking system.

type ClientExpectation

type ClientExpectation struct {
	Method string
	URL    string
	Client *Client
}

ClientExpectation represents a in-process-of-being-built http client mocking operation. The `On` method of `Client` returns an instance of this struct upon which you can call further methods to customize the response.

func (*ClientExpectation) Return

Return specifies the response for a ClientExpectation, which is then committed to the connected mock client.

func (*ClientExpectation) ReturnError

func (ce *ClientExpectation) ReturnError(msg string) *ClientExpectation

ReturnError causes this expectation to resolve to an error.

func (*ClientExpectation) ReturnJSON

func (ce *ClientExpectation) ReturnJSON(
	status int,
	body interface{},
) *ClientExpectation

ReturnText causes this expectation to resolve to a json-based body with the provided status code. Panics when the provided body cannot be encoded to JSON.

func (*ClientExpectation) ReturnNotFound

func (ce *ClientExpectation) ReturnNotFound() *ClientExpectation

ReturnNotFound is a simple helper that causes this expectation to resolve to a 404 error. If a customized body is needed, use something like `ReturnString“ instead.

func (*ClientExpectation) ReturnString

func (ce *ClientExpectation) ReturnString(
	status int,
	body string,
) *ClientExpectation

ReturnString causes this expectation to resolve to a string-based body with the provided status code.

type Server

type Server struct {
	*httpexpect.Expect
	*stdtest.Server
}

func NewServer

func NewServer(t *testing.T, handler http.Handler) *Server

NewServer returns a new test server instance using the provided handler.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL