Documentation ¶
Overview ¶
Package mock provides basic functionality to mock HTTP responses.
Its main use is the following szenario where a ht.Test is used to test an endpoint on a server. Handling this endpoint requires one or more additional requests to an external backend system which is mocked by a Mock. Like this the server endpoint can be tested without the need for a working backend system and at the same time it is possible to validate the request made by the server.
Suite Test Server Mock | | to test | +---+ | | | | | | | +---+ | +--start backend mock-->| | | | | | | | | | +---+ | | | | +---->| | +---+ | | | | | |--->| | | | | | | | | |--->| | | | | | | |<---| | | | | |<---| | | | | |<----| | +---+ | | | | +---+ | | | | | | | |<--report if called----| | +---+ +---+
Of course Mocks can be used for general mocking too.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ServerShutdownGraceperiode = 250 * time.Millisecond
ServerShutdownGraceperiode is the time given the mock servers to shut down.
Functions ¶
func Analyse ¶
Analyse analyses whether all and only the enabled mocks have been called after the mocks have been started with Provide. Analyse is not idempotent, each ctrl can be analysed only once.
func PrintReport ¶
PrintReport produces a multiline report of the request/response pair in report. It is unsuitable for non-printable body.
func Serve ¶
func Serve(mocks []*Mock, notfound http.Handler, log Log, certFile, keyFile string) (stop chan bool, err error)
Serve the given mocks until something is sent on the stop channel. once the mock servers have shut down stop is closed. The notfound handler is used to catch all request not matching a defined route and log can be used for diagnostic messages, both may be nil.
To handle TLS connections you can provide certFile and keyFile as described in https://golang.org/pkg/net/http/#Server.ListenAndServeTLS. All mocks must use the same certificate/key pair.
This is a low level function: If one just wishes to provide a bunch of mocks services and check whether they were invoked properly one should use Provide and Analyse.
Types ¶
type Control ¶
type Control struct {
// contains filtered or unexported fields
}
Control contains information about provided mocks.
func Provide ¶
Provide starts the given mocks, it returns a control handle which allows to stop the mocks and analyse if all where called properly. Stray calls not hitting a defined and enabled mock will be catched and recorded. The returned control handle must be Analyze'd to stop data collection and prevent goroutine leaks.
type Log ¶
type Log interface {
Printf(format string, a ...interface{})
}
Log is the interface used for logging.
type Mapping ¶
type Mapping struct { // Variables contains (single or multiple) input variable names and // the single output variable name. Variables []string // Table is the mapping table, its len must be an integer multiple // of 3*len(Variables). Table []string }
Mapping allows to set the value of a variable based on some other variable's value. Consider the follwing Mapping:
Variables: []string{ "first", "last", "age" }, Table: []string{ "John", "Smith", "20", "John", "*", "45", "Paul", "Brown", "30", "*", "Brown", "55", "*", "*", "25", }
It would set the variable "age" to 30 if first=="Paul" && last=="Brown". "John Miller" would be 45 years old and "Sue Carter" 25 because "*" matches any value. "John Brown" is 45 because matching happens left to right,
type Mock ¶
type Mock struct { // Name of this mock. Name string // Description of this mock. Description string // Disable can be used to disable this mock. Disable bool // Method for which this mock applies to. Method string // URL this mock applies to. // Schema, Port and Path are considered when deciding if // this mock is appropriate for the current request. // The path can be a Gorilla mux style path template in which // case variables are extracted. URL string // ParseForm allows to parse query- and form-parameters into variables. // If set to true then a request like // curl -d A=1 -d B=2 -d B=3 http://localhost/?C=4 // would extract the following variable/value-pairs: // A 1 // B[0] 2 // B[1] 3 // C 4 ParseForm bool // DataExtraction contains variable extraction definitions which are applied // to the incomming request. DataExtraction ht.ExtractorMap // Checks are applied to to the received HTTP request. This is done // by conveting the request to a HTTP response and populating a synthetic // ht.Test. This implies that several checks are inappropriate here. Checks ht.CheckList // Response to send for this mock. Response Response // Variables contains the default variables/values for this mock. Variables scope.Variables // Map is used to set variable values depending on other variables. // It is executed after DataExtraction but before constructing the // response. Map []Mapping // Monitor is used to report invocations if this mock. // The incomming request and the outgoing mocked response are encoded // in a ht.Test. The optional results of the Checks are stored in the // Test's CheckResult field. // This is nonsensical but is the fastet way to get mocking up running. Monitor chan *ht.Test // Log to report infos to. Log Log // contains filtered or unexported fields }
Mock allows to mock a HTTP response for a certain request.
It reuses functionality from github.com/vdobler/ht/ht, namely Checks to test if the request looks okay and Extractors to extract dat from the request to generate a dynamic response based on the request. Unfortunately both (Checks and Extractors) work on HTTP responses and not on requests. TO make this work the incoming request to a mock is rewritten as a response in the following way:
- The HTTP Status is fixed to "200 OK".
- The response duration is fixed to 1ms.
- The Cookie header is rewriten to Set-Cookie header(s).
type Response ¶
type Response struct { // StatusCode of the response. A value of 0 will result in StatusCode 200. StatusCode int // Header is the HTTP header to send. If Go's default header is okay it // can be empty. Header http.Header // Body of the response. Body may start with "@file:" and "@vfile:" as // explained in detail for ht.FileData. Body string }
Response to send as mocked answer.