golio: github.com/KnutZuidema/golio/mock Index | Examples | Files

package mock

import "github.com/KnutZuidema/golio/mock"

Package mock includes mock constructs used for testing the API.

Index

Examples

Package Files

docs.go mock_doer.go response_body.go

Variables

var (
    // ErrBodyClosed is returned if read is attempted on a closed body
    ErrBodyClosed = fmt.Errorf("body is closed")
)

type Doer Uses

type Doer struct {
    Response     http.Response
    ResponseBody *ResponseBody
    ResponseTime time.Duration
    Custom       func(r *http.Request) (*http.Response, error)
}

Doer is an implementation of the Doer interface for testing purposes which always returns the set response attribute

func NewHeaderMockDoer Uses

func NewHeaderMockDoer(code int, headers http.Header) *Doer

NewHeaderMockDoer constructs a new MockDoer with the given headers and status code

Code:

doer := NewHeaderMockDoer(200, http.Header{"Content-Type": []string{"application/json"}})
request, _ := http.NewRequest("GET", "http://example.com", nil)
response, _ := doer.Do(request)
fmt.Printf("status code: %d, content-type: %v", response.StatusCode, response.Header.Get("Content-Type"))

Output:

status code: 200, content-type: application/json

func NewJSONMockDoer Uses

func NewJSONMockDoer(object interface{}, code int) *Doer

NewJSONMockDoer constructs a new MockDoer with the json representation of an object as body and the given status code CAUTION: silently returns an empty response if object is fails json marshaling

Code:

type object struct {
    Attr int
}
input := object{
    Attr: 2,
}
doer := NewJSONMockDoer(input, 200)
request, _ := http.NewRequest("GET", "https://example.com", nil)
response, _ := doer.Do(request)
var output object
_ = json.NewDecoder(response.Body).Decode(&output)
fmt.Printf("status code: %d, body: %+v\n", response.StatusCode, output)

Output:

status code: 200, body: {Attr:2}

func NewStatusMockDoer Uses

func NewStatusMockDoer(code int) *Doer

NewStatusMockDoer constructs a new MockDoer with the given status code

Code:

doer := NewStatusMockDoer(200)
request, _ := http.NewRequest("GET", "http://example.com", nil)
response, _ := doer.Do(request)
fmt.Printf("status code: %d", response.StatusCode)

Output:

status code: 200

func (*Doer) Do Uses

func (d *Doer) Do(r *http.Request) (*http.Response, error)

Do returns the set HTTP response attribute after waiting for the specified amount of time if a custom function is specified it is used instead

type ResponseBody Uses

type ResponseBody struct {
    Content []byte
    // contains filtered or unexported fields
}

ResponseBody implements the ReadCloser interface to act as a response body for testing purposes

func (*ResponseBody) Close Uses

func (b *ResponseBody) Close() error

Close closes the response body. After closing the response body it cannot be read from

func (*ResponseBody) Read Uses

func (b *ResponseBody) Read(p []byte) (n int, err error)

Read reads from the content of the response body into p

Package mock imports 5 packages (graph). Updated 2019-06-16. Refresh now. Tools for package owners.