simplemockserver

package module
v0.0.0-...-c171d8b Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2022 License: MIT Imports: 12 Imported by: 0

README

simplemockserver

The intention behind this library was to create a simple REST Server which use predefined endpoints with the possibility to return different responses depending on filter queries on URL parameters or json body content for (POST, PUT)

Getting started

import (
    "github.com/stretchr/testify/require"
    "github.com/daolis/simplemockserver"
)

func TestNewMockServer(t *testing.T) {
	// use default configuration with config file 'testfiles/mock.json' and random free port.
	mockServer, err := NewMockServer() 
	require.NoError(t, err)
	defer mockServer.Stop()
    
	// get the mock server URL (e.g. http://localhost:12345) 
	mockURL := mockServer.GetURL()
	_ = mockURL
	
	// use custom file and fixed port 112233
	mockServer, err := NewMockServer(WithFile("customFile.json"), WithFixedPort(112233))
	require.NoError(t, err)
}

Configure endpoints

Example configuration

{
    "/endpoint2": {
        "method": "POST",
        "responses": [
            {
                "requestQuery": {
                    "body": "name = 'John'"
                },
                "response": {
                    "status": 200,
                    "body": {
                        "testKey1": "testValue1",
                        "testKey2": "testValue2"
                    }
                }
            },
            {
                "requestQuery": {
                    "body": "name = 'Doe'"
                },
                "response": {
                    "status": 200,
                    "body": {
                        "name": "Doe",
                        "age": 125,
                        "other": "test"
                    }
                }
            }
        ]
    }
}

Here we configure one endpoint /endpoint2 white method POST which can return two different responses. The requestQuery parameter defines which response will be sent to the client.

  1. "body": "name = 'John'": The body has to be JSON will be chosen if the body contains a attribute name with the value John.
    e.g.
    {
        "test": "lala",
        "name": "John"
    }
    
  2. same with name Doe

The response with the first matching query will be returned! If no response is matching it will return a 404 NOT FOUND.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CustomEndpoints

type CustomEndpoints map[string]Endpoint

func NewCustomEndpoints

func NewCustomEndpoints(endpoints CustomResponseEndpoint) CustomEndpoints

type CustomResponseEndpoint

type CustomResponseEndpoint map[string]struct {
	Method    string
	Responses []CustomResponseEntry
}

type CustomResponseEntry

type CustomResponseEntry struct {
	RequestQueryFn RequestQueryFn `json:"-"`
	ResponseFn     ResponseFn     `json:"-"`
}

type Endpoint

type Endpoint map[string][]ResponseEntry

type ErrorResponse

type ErrorResponse struct {
	Message string `json:"message"`
}

type MockFile

type MockFile map[string]Endpoint

type MockServer

type MockServer interface {
	Stop()
	GetURL() string
}

func NewMockServer

func NewMockServer(properties ...MockServerProperty) (MockServer, error)

type MockServerError

type MockServerError struct {
	StatusCode int
	Message    string
}

func NewMockServerError

func NewMockServerError(statuscode int, format string, a ...interface{}) *MockServerError

func (MockServerError) Error

func (m MockServerError) Error() string

type MockServerProperty

type MockServerProperty func(*mockServer)

func WithCustomEndpoints

func WithCustomEndpoints(endpoints CustomEndpoints) MockServerProperty

func WithFile

func WithFile(file string) MockServerProperty

func WithFixedPort

func WithFixedPort(port int) MockServerProperty

type RequestQuery

type RequestQuery struct {
	Body *string `json:"body" yaml:"body"`
	URL  *string `json:"url" yaml:"url"`
}

type RequestQueryFn

type RequestQueryFn func(r *http.Request) bool

type Response

type Response struct {
	StatusCode int `json:"status" yaml:"status"`
	Body       any `json:"body" yaml:"body"`
}

type ResponseEntry

type ResponseEntry struct {
	RequestQuery   *RequestQuery  `json:"requestQuery" yaml:"requestQuery"`
	RequestQueryFn RequestQueryFn `json:"-" yaml:"-"`
	Response       Response       `json:"response" yaml:"response"`
	ResponseFn     ResponseFn     `json:"-" yaml:"-"`
}

type ResponseFn

type ResponseFn func(w http.ResponseWriter, r *http.Request) error

Jump to

Keyboard shortcuts

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