mockserver

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: May 29, 2020 License: MIT Imports: 6 Imported by: 2

README

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/BraspagDevelopers/mock-server-client

Using go.mod file

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

Usage

Import the package

import (
    "github.com/BraspagDevelopers/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 MockClient

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

func NewClient

func NewClient(host string, port int) MockClient

func (MockClient) Clear

func (c MockClient) Clear(matcher RequestMatcher) error

func (MockClient) Verify

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

func (MockClient) VerifyAndClear

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

func (MockClient) VerifyAndClearByHeader

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

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"`
}

func (RequestMatcher) WithHeader

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

func (RequestMatcher) WithJsonFields

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

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