mock

package module
v0.0.0-...-2fd9363 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2021 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Int64   = "int64"
	Float64 = "float64"
	Slice   = "slice"
	String  = "string"
	Bool    = "bool"
	Invalid = "invalid"
)

Reflection consts

View Source
const DefaultDirectory = "../../testdata/http_mock/"

DefaultDirectory defines the main mock directory

Variables

This section is empty.

Functions

func CheckJSON

func CheckJSON(data interface{}, excluded *Exclusion) (interface{}, error)

CheckJSON recursively parses json data to retract keywords, quite intensive.

func CheckResponsePayload

func CheckResponsePayload(data []byte) ([]byte, error)

CheckResponsePayload checks to see if there are any response body variables that should not be there.

func DeriveURLValsFromJSONMap

func DeriveURLValsFromJSONMap(payload []byte) (url.Values, error)

DeriveURLValsFromJSONMap gets url vals from a map[string]string encoded JSON body

func GetFilteredHeader

func GetFilteredHeader(res *http.Response) (http.Header, error)

GetFilteredHeader filters excluded http headers for insertion into a mock test file

func GetFilteredURLVals

func GetFilteredURLVals(vals url.Values) (string, error)

GetFilteredURLVals filters excluded url value variables for insertion into a mock test file

func HTTPRecord

func HTTPRecord(res *http.Response, service string, respContents []byte) error

HTTPRecord will record the request and response to a default JSON file for mocking purposes

func IsExcluded

func IsExcluded(key string, excludedVars []string) bool

IsExcluded cross references the key with the excluded variables

func MatchAndGetResponse

func MatchAndGetResponse(mockData []HTTPResponse, requestVals url.Values, isQueryData bool) (json.RawMessage, error)

MatchAndGetResponse matches incoming request values with mockdata response values and returns the payload

func MatchURLVals

func MatchURLVals(v1, v2 url.Values) bool

MatchURLVals matches url.Value query strings

func MessageWriteJSON

func MessageWriteJSON(w http.ResponseWriter, status int, data interface{})

MessageWriteJSON writes JSON to a connection

func NewVCRServer

func NewVCRServer(path string) (string, *http.Client, error)

NewVCRServer starts a new VCR server for replaying HTTP requests for testing purposes and returns the server connection details

func RegisterHandler

func RegisterHandler(pattern string, mock map[string][]HTTPResponse, mux *http.ServeMux)

RegisterHandler registers a generalised mock response logic for specific routes

Types

type Exclusion

type Exclusion struct {
	Headers   []string `json:"headers"`
	Variables []string `json:"variables"`
}

Exclusion defines a list of items to be excluded from the main mock output this attempts a catch all approach and needs to be updated per exchange basis

func GetExcludedItems

func GetExcludedItems() (Exclusion, error)

GetExcludedItems checks to see if the variable is in the exclusion list as to not display secure items in mock file generator output

type HTTPResponse

type HTTPResponse struct {
	Data        json.RawMessage     `json:"data"`
	QueryString string              `json:"queryString"`
	BodyParams  string              `json:"bodyParams"`
	Headers     map[string][]string `json:"headers"`
}

HTTPResponse defines expected response from the end point including request data for pathing on the VCR server

type VCRMock

type VCRMock struct {
	Routes map[string]map[string][]HTTPResponse `json:"routes"`
}

VCRMock defines the main mock JSON file and attributes

Jump to

Keyboard shortcuts

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