mockserver

package module
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: May 16, 2024 License: MIT Imports: 5 Imported by: 0

README

License Tag Go Report Card

Mock Server Client - a Go package

Client for the mock-server.com product.

The focus of this library is to use within integration tests in order to verify if a specific request was made in a mocked dependency (the mock-server).

It is still work in progress.

Installation

Using go get

go get -u github.com/stalko/mock-server-client

Using go.mod file

require github.com/stalko/mock-server-client

Usage

Import the package

import (
    "github.com/stalko/mock-server-client"
)
Verifying a request

The folowing verifies if the mock-server received requests matching the following filters:

  • The method was POST
  • The endpoint was /api/categories
  • Having a header Environment: Development
  • The body was a JSON and the body field name had the value "Tools"
  • The request was made once and only once
ms := mockserver.NewClient("localhost", 8080)
err := ms.Verify(
    mockserver.RequestMatcher{
        Method: http.MethodPost,
        Path:   "/api/categories"}.
        WithHeader("Environment", "Development").
        WithJsonFields(map[string]interface{}{
            "name": "Tools",
        }),
    mockserver.Once())
Clearing requests

You can clear the mock-server requests in the log that matches an specific RequestMatcher using the method MockClient.Verify.

err := ms.Verify(mockserver.RequestMatcher{
    Method: http.MethodPost,
    Path:   "/api/categories"}.
    WithHeader("Environment", "Development").
    WithJsonFields(map[string]interface{}{
        "name": "Tools",
    }))

Notice that the cardinality is not specified in this request for this method. All matching requests will be erased from the log.

Verify and clear at the same time

You can also verify and clear matching requests at once by using the method MockClient.VerifyAndClear().

Documentation

Index

Constants

View Source
const (
	StrictMatch   = "STRICT"
	TolerantMatch = "ONLY_MATCHING_FIELDS"
)
View Source
const (
	MatchBodyJSON = "JSON"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type BodyMatcher

type BodyMatcher struct {
	Type      string                 `json:"type,omitempty"`
	JSON      map[string]interface{} `json:"json,omitempty"`
	MatchType string                 `json:"matchType,omitempty"`
}

type Expectation

type Expectation struct {
	Request  RequestMatcher `json:"httpRequest,omitempty"`
	Response Response       `json:"httpResponse,omitempty"`
	Priority int32          `json:"priority,omitempty"`
}

func NewExpectation

func NewExpectation(request RequestMatcher) *Expectation

func (Expectation) WithPriority

func (e Expectation) WithPriority(priority int32) Expectation

func (Expectation) WithResponse

func (e Expectation) WithResponse(response Response) Expectation

type MockServerClient

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

func NewClient

func NewClient(host string, port int) MockServerClient

NewClient creates a new client provided its host and port

func NewClientURL

func NewClientURL(url string) MockServerClient

NewClientURL creates a new client provided its URL

func (MockServerClient) Clear

func (c MockServerClient) Clear(matcher RequestMatcher) error

Clear erases from the mock server all the requests matching the matcher.

func (MockServerClient) RegisterExpectation

func (c MockServerClient) RegisterExpectation(expectation Expectation) error

Set a new Expectation in mock server with request and response

func (MockServerClient) SetDebug

func (c MockServerClient) SetDebug(d bool) MockServerClient

SetDebug enables or disables the debug

func (MockServerClient) Verify

func (c MockServerClient) Verify(matcher RequestMatcher, times Times) error

Verify checks if the mock server received requests matching the matcher.

func (MockServerClient) VerifyAndClear

func (c MockServerClient) VerifyAndClear(matcher RequestMatcher, times Times) error

VerifyAndClear checks if the mock server received requests matching the matcher and then erases from the logs the requests matching the matcher.

func (MockServerClient) VerifyAndClearByHeader

func (c MockServerClient) VerifyAndClearByHeader(headerName, headerValue string, matcher RequestMatcher, times Times) error

VerifyAndClearByHeader checks if the mock server received requests matching the matcher and having the specified header name and value. It then erases from the logs the requests matching the same header name and value.

type RequestMatcher

type RequestMatcher struct {
	Method    string              `json:"method,omitempty"`
	Path      string              `json:"path,omitempty"`
	Body      BodyMatcher         `json:"body,omitempty"`
	Headers   map[string][]string `json:"headers,omitempty"`
	KeepAlive bool                `json:"keepAlive,omitempty"`
	Secure    bool                `json:"secure"`
}

func (RequestMatcher) WithHeader

func (m RequestMatcher) WithHeader(key, value string) RequestMatcher

func (RequestMatcher) WithJSONFields

func (m RequestMatcher) WithJSONFields(json map[string]interface{}) RequestMatcher

type Response

type Response struct {
	Body       map[string]interface{} `json:"body,omitempty"`
	Headers    map[string][]string    `json:"headers,omitempty"`
	StatusCode int32                  `json:"statusCode,omitempty"`
}

func NewResponseOK

func NewResponseOK() *Response

func (Response) WithHeader

func (e Response) WithHeader(key, value string) Response

func (Response) WithJSONBody

func (e Response) WithJSONBody(json map[string]interface{}) Response

type Times

type Times struct {
	AtLeast int `json:"atLeast,omitempty"`
	AtMost  int `json:"atMost,omitempty"`
}

func Exactly

func Exactly(n int) Times

func Once

func Once() Times

Jump to

Keyboard shortcuts

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