clientmock

package module
v0.0.0-...-eb6c237 Latest Latest
Warning

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

Go to latest
Published: May 5, 2019 License: GPL-3.0 Imports: 6 Imported by: 0

README

clientmock

GoDoc Go Report Card codecov

A mockable http.Client used to validate request data and return fake response data by replacing the http.Client transport layer.

Install

go get github.com/MordFustang21/clientmock

Documentation and Examples

Example returning status code and body
package main

import (
	"bytes"
	"io/ioutil"
	"net/http"
	"testing"
	
	"github.com/MordFustang21/clientmock"
)

func TestGet(t *testing.T) {
	client, mock, err := clientmock.NewClient()
	if err != nil {
		t.Fatal(err)
	}
	
	expectedStatus := http.StatusBadRequest
	expectedBody := "empty request body"
	
	mock.ReturnStatus(expectedStatus)
	mock.ReturnBody(bytes.NewBufferString(expectedBody))
	
	req, err := http.NewRequest(http.MethodPost, "http://www.github.com", nil)
	if err != nil {
		t.Fatal(err)
	}
	
	res, err := client.Do(req)
	if err != nil {
		t.Fatal(err)
	}
	defer res.Body.Close()
	
	// Status code will be 400 as defined in mock
	if res.StatusCode != expectedStatus {
		t.Fatal("got wrong status")
	}
	
	// body will contain "empty request body" as defined in mock
	data, err := ioutil.ReadAll(res.Body)
	if err != nil {
		t.Fatal(err)
	}
	
	if string(data) != expectedBody {
		t.Fatal("returned body doesn't match expected")
	}
}

Contributions

Feel free to open a pull request. Note, if you wish to contribute an extension to public (exported methods or types) - please open an issue before, to discuss whether these changes can be accepted. All backward incompatible changes are and will be treated cautiously

License

The GNU General Public License V3.0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Expectation

type Expectation interface {
	// Check will give the expectation the incoming request to be validated
	Check(req *http.Request)

	// Met will check if the check has passed
	Met() bool

	// Message is the msg passed to error.New if the expectation wasn't met
	Message() string
}

Expectation is an interface for an expectation check for the request that will be made

type ExpectedBody

type ExpectedBody struct {
	// contains filtered or unexported fields
}

ExpectedBody verifies that the request body matches the expected body

func (*ExpectedBody) Check

func (e *ExpectedBody) Check(req *http.Request)

Check will verify the body in the request matches the expected body

func (*ExpectedBody) Message

func (e *ExpectedBody) Message() string

Message returns the error message or "" if none

func (*ExpectedBody) Met

func (e *ExpectedBody) Met() bool

Met returns whether or not the expectation has been met

type ExpectedHeader

type ExpectedHeader struct {
	// contains filtered or unexported fields
}

ExpectedHeader will verify the sent header matches the expected header

func (*ExpectedHeader) Check

func (e *ExpectedHeader) Check(req *http.Request)

Check will verify the headers match the expected

func (*ExpectedHeader) Message

func (e *ExpectedHeader) Message() string

Message returns the error message or "" if none

func (*ExpectedHeader) Met

func (e *ExpectedHeader) Met() bool

Met returns whether or not the expectations were met

type ExpectedMethod

type ExpectedMethod struct {
	// contains filtered or unexported fields
}

ExpectedMethod will verify that the request method matches the expected method

func (*ExpectedMethod) Check

func (m *ExpectedMethod) Check(req *http.Request)

Check will verify the *http.Request method matches the expected method

func (*ExpectedMethod) Message

func (m *ExpectedMethod) Message() string

Message returns the error message or "" if none

func (*ExpectedMethod) Met

func (m *ExpectedMethod) Met() bool

Met returns if methods matched

type Mock

type Mock interface {
	// ExpectMethod sets the method expected on the incoming request
	ExpectMethod(method string) Mock
	// ExpectBody sets the body expected on the incoming request
	ExpectBody(body string) Mock
	// ExpectHeader sets the expected headers on the incoming request
	ExpectHeader(header http.Header) Mock
	// Expectation allows for any custom expectation that follows the Expectation interface
	Expectation(exp Expectation) Mock
	// ExpectationsMet will return an error if expectations weren't met
	ExpectationsMet() error
	// ReturnStatus will set the status on returned http.Response
	ReturnStatus(status int) Mock
	// ReturnBody will set the body on the returned http.Response to the given io.Reader
	ReturnBody(body io.Reader) Mock
	// ReturnHeader will set the headers on the returned http.Response to the given headers
	ReturnHeader(header http.Header) Mock
	// ReturnError will return an error instead of a response object
	ReturnError(err error) Mock
}

Mock interface serves to create expectations for any kind of http.Client action in order to mock and test real http.Client behavior.

func NewClient

func NewClient() (*http.Client, Mock, error)

NewClient create new mockable http.Client

type SetBody

type SetBody struct {
	// contains filtered or unexported fields
}

SetBody implements the Setter interface and sets the status code

func (*SetBody) Set

func (s *SetBody) Set(response *http.Response)

Set will set the response body with the returning body

type SetHeader

type SetHeader struct {
	// contains filtered or unexported fields
}

SetHeader implements the Setter interface and sets the response headers

func (*SetHeader) Set

func (s *SetHeader) Set(response *http.Response)

Set sets the response headers on the request

type SetStatusCode

type SetStatusCode struct {
	// contains filtered or unexported fields
}

SetStatusCode implements the Setter interface and sets the status code

func (*SetStatusCode) Set

func (s *SetStatusCode) Set(resp *http.Response)

Set will set the response status code with the returning status

type Setter

type Setter interface {
	Set(response *http.Response)
}

Setter is an interface that sets a value on an *http.Response

Jump to

Keyboard shortcuts

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