testserver

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2022 License: Apache-2.0 Imports: 8 Imported by: 0

README

testserver

testserver is a framework for mocking http server. It integrates with Go's built-in testing.

Installation

After go mod init <your_project_name> and go mod tidy just run:

go get github.com/peliseev/testserver

Building Mocks

The common way to use this framework is as follows:

  1. Create new instance of TestServer
  2. Add some expectations
  3. Start TestServer
  4. Create an instance of the object under test
  5. Call the methods under test
  6. Check the results

For example:

func TestFunc(t *testing.T) {
    // Create new instance
    ts := testserver.New(t)
    
    // Add some expectations
    ts.Add(EXPECT().Method("GET").Path("/api/profile").
		RespWithStatus(200).
		RespWithBody(profile))
	
    ts.Add(EXPECT().Method("POST").Path("/api/order").
		RespWithStatus(200).
		RespWithBody(done).Times(2))
    ...
    
    // Start test Server
    ts.Start()
    defer ts.Stop()
    
    // Create an instance of the object under test
    service := order.New(ts.URL)
    
    // Call the method under test
    gotRs, gotErr := service.CreateOrder(args...)
    
    // Check the results
    if wantRs != gotRs {
        t.Error()
    }
    if wantErr != gotErr {
        t.Error()
    }
}

You can find more examples of usage here

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ExpectationBuilder

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

ExpectationBuilder help to build expectations for http request and response

func EXPECT

func EXPECT() *ExpectationBuilder

EXPECT initiate ExpectationBuilder object

func (*ExpectationBuilder) Method

func (eb *ExpectationBuilder) Method(method string) *ExpectationBuilder

Method allows you to set method for Path

func (*ExpectationBuilder) Path

Path allows you to set request path in terms of http.ServeMux Path is required

func (*ExpectationBuilder) ReqBody

func (eb *ExpectationBuilder) ReqBody(b interface{}) *ExpectationBuilder

ReqBody allows you to set the request body to which the actual request body should correspond exactly. You can use 2 time of args:

  1. []byte type allow to set byte slice that will return to the response body
  2. An any object that can be marshall to json

func (*ExpectationBuilder) ReqBodyContains

func (eb *ExpectationBuilder) ReqBodyContains(s string) *ExpectationBuilder

ReqBodyContains allows you to specify the string that should be found in the request body

func (*ExpectationBuilder) ReqHeader

func (eb *ExpectationBuilder) ReqHeader(k, v string) *ExpectationBuilder

ReqHeader allows you to set request headers expectation in key value format it can be called multiple times.

func (*ExpectationBuilder) ReqPathParam

func (eb *ExpectationBuilder) ReqPathParam(k, v string) *ExpectationBuilder

ReqPathParam allows you to specify the params of request path.

For example if you use builder like this:

expectation := EXPECT().Method("GET").Path("/api/v1/client/{client_id}")

then

expectation.ReqParam("client_id", "123") ts.Add(expectation)

allow you to check that the request path parameter client_id is 123

func (*ExpectationBuilder) ReqQueryParam

func (eb *ExpectationBuilder) ReqQueryParam(k, v string) *ExpectationBuilder

ReqQueryParam allows you to specify the params of request query string or request body form data. POST and PUT body parameters take precedence over URL query string values.

func (*ExpectationBuilder) RespWithBody

func (eb *ExpectationBuilder) RespWithBody(b interface{}) *ExpectationBuilder

RespWithBody allows you to set response body. You can use 2 time of args:

  1. []byte type allow to set byte slice that will return to the response body
  2. An any object that can be marshall to json

func (*ExpectationBuilder) RespWithHeader

func (eb *ExpectationBuilder) RespWithHeader(k, v string) *ExpectationBuilder

RespWithHeader allows you to set response headers in key value format it can be called multiple times.

For example:

EXPECT().Method("Get").Path("/api"). RespWithHeader("x-request-id", "123"). RespWithHeader("Server" "Apache")

func (*ExpectationBuilder) RespWithStatus

func (eb *ExpectationBuilder) RespWithStatus(s int) *ExpectationBuilder

RespWithStatus specify http response status

func (*ExpectationBuilder) Times

Times allows you to set how many times endpoint should be called with specified parameters by default EXPECT is waiting for 1 call

type TestServer

type TestServer struct {
	*httptest.Server
	// contains filtered or unexported fields
}

TestServer is a structure that contains a set of expectations and a server from the httptest package. You should use a new instance of the test server in each test. The algorithm for using the test server is as follows:

  func TestFunc(t *testing.T) {
    // Create new instance
    ts := testserver.New(t)

	// Add some expectations
    ts.Add(EXPECT().Method("GET").Path("/api/profile").RespWithStatus(200).RespWithBody(profile))
    ts.Add(EXPECT().Method("POST").Path("/api/order").RespWithStatus(200).RespWithBody(done).Times(2))
    ...

	// Start test Server
    ts.Start()
    defer ts.Stop()

	// Create an instance of the object under test
    service := order.New(ts.URL)

	// Call the method under test
	gotRs, gotErr := service.CreateOrder(args...)

	// Check the results
	if wantRs != gotRs {
		t.Error()
	}
	if wantErr != gotErr {
		t.Error()
	}
  }

func New

func New(t *testing.T) *TestServer

New create new instance of TestServer

func (*TestServer) Add

func (ts *TestServer) Add(eb *ExpectationBuilder)

Add new expectation on TestServer instance

func (*TestServer) Start

func (ts *TestServer) Start()

Start TestServer

func (*TestServer) Stop

func (ts *TestServer) Stop()

Stop TestServer and check expectations

Jump to

Keyboard shortcuts

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