mockserver

package
v0.0.0-...-5ea965e Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2024 License: MIT Imports: 3 Imported by: 0

README

Description

This folder contains utility methods to mock API calls made by connectors to provider APIs. It offers several types of mock servers to simulate different scenarios by evaluating incoming requests and returning appropriate responses.

Mock Servers

The package provides multiple mock server types:

  • Dummy: always returns a predefined status.
  • Fixed: returns a fixed response regardless of the request.
  • Conditional: (equivalent to if) returns responses based on request conditions.
  • Switch: matches requests against multiple conditions and selects the first matching response.

Without this package

A typical mock server setup without using this package would look like:

Server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(http.StatusOK)
	_, _ = w.Write(data)
})),

For more complex connectors, this approach becomes unwieldy. A connector may make multiple API calls, each requiring specific request conditions to be met. This package simplifies such scenarios.

Usage Examples

Dummy Mock Server

This mock server always returns StatusTeapot. It’s useful for tests that don’t yet need to handle API calls.

Server: mockserver.Dummy()

Fixed Mock Server

This server will always return a 200 status code with a predefined response (in this case, customers). It’s ideal for simple tests that only need a static response.

Server: mockserver.Fixed{
	Setup:  mockserver.ContentJSON(),
	Always: mockserver.Response(http.StatusOK, customers),
}.Server(),

Conditional Mock Server

This mock server returns a 200 status code along with response data (e.g., indicating a job was created) only if the conditions are met. The conditions in this example require that both the URL path and the request body match expected values. If the conditions are not met, an optional Else clause can specify a default response (otherwise the server defaults to a 500 status with a failure message). This is highly useful to ensure that requests are constructed properly by the connector with respect to provider APIs.

Server: mockserver.Conditional{
	Setup: mockserver.ContentJSON(),
	If: mockcond.And{
		mockcond.PathSuffix("/services/data/v59.0/jobs/ingest"),
		mockcond.Body(bodyRequest),
	},
	Then: mockserver.Response(http.StatusOK, responseCreateJob),
    Else: mockserver.Response(http.StatusOK, []byte{})
}.Server(),

Switch Mock Server

This mock server behaves like a Go switch statement, evaluating multiple cases and returning the response for the first matching condition. In this example, it selects the response based on the request URL path, with a default response if no cases match. This is particularly useful when a connector makes multiple API calls, each requiring a different response.

Server: mockserver.Switch{
	Setup: mockserver.ContentJSON(),
	Cases: []mockserver.Case{{
		If:   mockcond.PathSuffix("EntityDefinitions(LogicalName='account')"),
		Then: mockserver.Response(http.StatusOK, responseContactsSchema),
	}, {
		If:   mockcond.PathSuffix("EntityDefinitions(LogicalName='account')/Attributes"),
		Then: mockserver.ResponseString(http.StatusOK, `{"value":[]}`),
	}},
	Default: mockserver.Response(http.StatusOK, []byte{}),
}.Server(),

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContentHTML

func ContentHTML() http.HandlerFunc

ContentHTML is a setup handler, which configures server to use HTML.

func ContentJSON

func ContentJSON() http.HandlerFunc

ContentJSON is a setup handler, which configures server to use JSON.

func ContentMIME

func ContentMIME(mediaType string) http.HandlerFunc

ContentMIME is a setup handler, which configures custom media type.

func ContentXML

func ContentXML() http.HandlerFunc

ContentXML is a setup handler, which configures server to use XML.

func Dummy

func Dummy() *httptest.Server

Dummy server only talks about having a cup of tea. Acknowledges requests and does nothing else.

func NewServer

func NewServer(handler http.HandlerFunc) *httptest.Server

NewServer is syntactic sugar for creating test server.

func Response

func Response(status int, data ...[]byte) http.HandlerFunc

Response is used to configure server response with HTTP status and body data. Data is optional.

func ResponseString

func ResponseString(status int, data string) http.HandlerFunc

Types

type Case

type Case struct {
	// If, may consist of nested Or, And clauses allowing sophisticated logic.
	If mockcond.Condition
	// Then will be called when If evaluates to true.
	Then http.HandlerFunc
}

Case is one possible route a mock server can take if condition is satisfied.

type Conditional

type Conditional struct {
	// Setup is optional handler, where common http.ResponseWrite configuration takes place.
	Setup http.HandlerFunc
	// If, may consist of nested Or, And clauses allowing sophisticated logic.
	If mockcond.Condition
	// Then will be called when If evaluates to true.
	Then http.HandlerFunc
	// Else will be called when If evaluates to false.
	Else http.HandlerFunc
}

Conditional is a server recipe that describes how mock server should react when conditions are met. It is equivalent to If() Then{} Else{}.

func (Conditional) Server

func (re Conditional) Server() *httptest.Server

Server creates mock server that will produce different response based on conditionals.

type Fixed

type Fixed struct {
	// Setup is optional handler, where common http.ResponseWrite configuration takes place.
	Setup http.HandlerFunc
	// Always represents server handler that should implement how server should respond.
	Always http.HandlerFunc
}

Fixed is a server recipe that responds the same way regardless of the input.

func (Fixed) Server

func (f Fixed) Server() *httptest.Server

Server creates mock server.

type Switch

type Switch struct {
	// Setup is optional handler, where common http.ResponseWrite configuration takes place.
	Setup http.HandlerFunc
	// Cases is an ordered list of possible pathways a mock server could take.
	// The first case that satisfies a condition will be picked.
	Cases []Case
	// Default will be called only if all case had failed.
	Default http.HandlerFunc
}

Switch is a server recipe that describes multiple path a server may take. It is equivalent to Switch() {Case->Then...Case->Then} Default{}.

func (Switch) Server

func (c Switch) Server() *httptest.Server

Server creates mock server that will produce different response based on conditionals.

Jump to

Keyboard shortcuts

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