httpmockserver

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2018 License: MIT Imports: 11 Imported by: 0

README

Go Report Card GitHub license

HTTP Mock Server for golang

HTTP Mock Server provides a way to easily mock externel http systems in your integration tests. Often the code under test access external resources that are not available during test or should not be used.

Sometimes it is not enough to mock the client that access these external resources because one is also interested in the validation of the correct http call to this external resources.

You are able to verify the correctness of the HTTP request that arrives at the external (mocked) system and you are able to set a response for every request.

That could be:

  • check if the auth header is set
  • check if the request method was GET / POST ...
  • check if path was correct
  • check if body was provided correctly

Installation

go get -u github.com/ybbus/httpmockserver

Getting started

Let's say we want to test (a part of) our application with an integration or system test. We know that the application makes use of an external REST service API. Of course we could mock the part that does the REST calls away, but we want to achieve two things with our test:

  • test the system as-is, so do not mock any internals. as you will see this will be much easier
  • check if the external resource would have been queried in the correct way (e.g. the authentication header is set correctly)
func TestIntegration(t *testing.T) {
    // start our mock-server
    mockServer := httpmockserver.New(t)

    // set expectations (anywhere) in your tests, but of course before the actual call
    // first we expect a GET to path person /person
    mockServer.EXPECT().Get("/person")

    // second we expect a Post to any path
    mockServer.EXPECT().POST()

    // third we expect a GET to /persons/{id}
    mockServer.EXPECT().Get().PathRegex("/persons/.*").Response(200).StringBody(`{"name": "Alex"}`)

    // do your actual tests that internally should satisfy our expectations
    aClient := NewClient(mockServer.getURL())
    myTestComponent := NewComponent(aClient)

    // http calls should happen here internally
    // so we do not mock the client but check if the calls work as expected
    myTestComponent.DoSomething()

    // tell the mockserver we are done after the tests so it can check for missing calls
    mockServer.Finish

    // shutdown the mock-server after the tests
    defer mockServer.Shutdown()
}

In detail

TODO

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IncomingRequest

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

type MockResponse

type MockResponse struct {
	Code    int
	Headers map[string]string
	Body    []byte
}

type MockServer

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

func New

func New(t *testing.T) *MockServer

func NewWithOpts added in v0.2.0

func NewWithOpts(t *testing.T, opts Opts) *MockServer

func (*MockServer) DEFAULT added in v0.2.0

func (s *MockServer) DEFAULT() RequestExpectation

func (*MockServer) EVERY

func (s *MockServer) EVERY() RequestExpectation

TODO: response expectation makes no sense here

func (*MockServer) EXPECT

func (s *MockServer) EXPECT() RequestExpectation

func (*MockServer) Finish

func (s *MockServer) Finish()

func (*MockServer) ServeHTTP

func (s *MockServer) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*MockServer) Shutdown

func (s *MockServer) Shutdown()

func (*MockServer) URL added in v0.2.0

func (s *MockServer) URL() string

type Opts added in v0.2.0

type Opts struct {
	Port   string
	UseSSL bool
}

type RequestExpectation

type RequestExpectation interface {
	AnyRequest() RequestExpectation

	AnyTimes() RequestExpectation
	Times(n int) RequestExpectation
	MinTimes(n int) RequestExpectation
	MaxTimes(n int) RequestExpectation

	Request(method string, path string) RequestExpectation
	Method(method string) RequestExpectation
	Path(path string) RequestExpectation
	PathRegex(pathRegex string) RequestExpectation

	GET() RequestExpectation
	POST() RequestExpectation
	PUT() RequestExpectation
	DELETE() RequestExpectation

	Get(path string) RequestExpectation
	Post(path string) RequestExpectation
	Put(path string) RequestExpectation
	Delete(path string) RequestExpectation

	Header(key, value string) RequestExpectation
	Headers(map[string]string) RequestExpectation

	BasicAuth(user, password string) RequestExpectation

	Custom(validation RequestValidationFunc, description string) RequestExpectation

	JsonBody(object interface{}) RequestExpectation
	StringBody(body string) RequestExpectation
	Body(body []byte) RequestExpectation
	BodyFunc(func(body []byte) error) RequestExpectation

	// switch to responseExpectations
	Response(code int) ResponseExpectation
}

type RequestValidationFunc

type RequestValidationFunc func(r *IncomingRequest) error

type ResponseExpectation

type ResponseExpectation interface {
	Header(key, value string) ResponseExpectation
	Headers(headers map[string]string) ResponseExpectation
	StringBody(body string) ResponseExpectation
	JsonBody(object interface{}) ResponseExpectation
	Body(data []byte) ResponseExpectation
}

Jump to

Keyboard shortcuts

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