testaservice

package module
v0.0.0-...-7f74d74 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2023 License: MIT Imports: 7 Imported by: 0

README

Testaservice - Convenient HTTP Service assertions based on testify library

This test library provides an HTTP service useful for use in testing allowing full exercise of the HTTP transport in unit tests.

Example

func TestMyHTTPClient(t *testing.T){
    t.Run("some sort of client test", func(t *testing.T){
        ts := NewTestService(t)
        // Do some client things

        var request ClientRequest
        ts.AssertCalled()
        ts.AssertReceivedAs(&request)
        // etc
    })
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TestService

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

TestService is a helpful test utility that allows tests to call a real HTTP service during test runs.

func NewTestService

func NewTestService(t *testing.T) *TestService

NewTestService will return a test service configured for the provided test state.

This init also handles the starting and cleanup of the test service once the test is completed.

func (*TestService) AssertCalled

func (s *TestService) AssertCalled()

AssertCalled allows callers to assert that the service has received a request.

func (*TestService) AssertNotCalled

func (s *TestService) AssertNotCalled()

AssertNotCalled allows callers to assert that the service has not received a request.

func (*TestService) AssertReceivedAs

func (s *TestService) AssertReceivedAs(v interface{})

AssertReceivedAs allows callers to assert that the last request received could be unmarshalled as the value provided.

func (*TestService) AssertReceivedBasicAuth

func (s *TestService) AssertReceivedBasicAuth(user, pass string)

AssertReceivedBasicAuth allows callers to assert that the last request received was authenticated with the provided values.

func (*TestService) AssertReceivedBody

func (s *TestService) AssertReceivedBody(body []byte)

AssertReceivedBody allows callers to assert that the last request received matches the body provided.

func (*TestService) AssertReceivedHeader

func (s *TestService) AssertReceivedHeader(name string, value string)

AssertReceivedHeader allows callers to assert that the last request received contained the header provided.

func (*TestService) AssertReceivedJSON

func (s *TestService) AssertReceivedJSON(expected string)

AssertReceivedBody allows callers to assert that the last request received matches the JSON string provided.

func (*TestService) AssertReceivedMethod

func (s *TestService) AssertReceivedMethod(method string)

AssertReceivedBody allows callers to assert that the last request received matches the HTTP Method provided.

func (*TestService) AssertReceivedParam

func (s *TestService) AssertReceivedParam(name string, value string)

AssertReceivedHeader allows callers to assert that the last request received contained the query param provided.

func (*TestService) AssertReceivedPath

func (s *TestService) AssertReceivedPath(path string)

AssertReceivedPath allows callers to assert that the last request received was for the path provided.

func (*TestService) SetResponseBody

func (s *TestService) SetResponseBody(b interface{})

SetResponseBody will set the provided value as the response body for any requests received by the service.

The method accepts bytes and strings which will be returned directly from the service. Passing structs or other types will result in the response being the JSON encoding of those values.

func (*TestService) SetResponseCode

func (s *TestService) SetResponseCode(code int)

SetResponseCode will set the provided value as the status code the service will return for any requests received by the service.

Jump to

Keyboard shortcuts

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