-
Notifications
You must be signed in to change notification settings - Fork 9
/
imposter.go
209 lines (173 loc) · 7.04 KB
/
imposter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright (c) 2018 Senseye Ltd. All rights reserved.
// Use of this source code is governed by the MIT License that can be found in the LICENSE file.
package mbgo
import (
"net"
"net/http"
"net/url"
)
// HTTPRequest describes an incoming HTTP request received by an
// Imposter of the "http" protocol.
//
// See more information about HTTP requests in mountebank at:
// http://www.mbtest.org/docs/protocols/http.
type HTTPRequest struct {
// RequestFrom is the originating address of the incoming request.
RequestFrom net.IP
// Method is the HTTP request method.
Method string
// Path is the path of the request, without the query parameters.
Path string
// Query contains the URL query parameters of the request.
Query url.Values
// Headers contains the HTTP headers of the request.
Headers http.Header
// Body is the body of the request.
Body interface{}
// Timestamp is the timestamp of the request.
Timestamp string
}
// TCPRequest describes incoming TCP data received by an Imposter of
// the "tcp" protocol.
//
// See more information about TCP requests in mountebank at:
// http://www.mbtest.org/docs/protocols/tcp.
type TCPRequest struct {
// RequestFrom is the originating address of the incoming request.
RequestFrom net.IP
// Data is the data in the request as plaintext.
Data string
}
// JSONPath is a predicate parameter used to narrow the scope of a tested value
// to one found at the specified path in the response JSON.
//
// See more information about the JSONPath parameter at:
// http://www.mbtest.org/docs/api/jsonpath.
type JSONPath struct {
// Selector is the JSON path of the value tested against the predicate.
Selector string `json:"selector"`
}
// Predicate represents conditional behaviour attached to a Stub in order
// for it to match or not match an incoming request.
//
// The supported operations for a Predicate are listed at:
// http://www.mbtest.org/docs/api/predicates.
type Predicate struct {
// Operator is the conditional or logical operator of the Predicate.
Operator string
// Request is the request value challenged against the Operator;
// either of type HTTPRequest or TCPRequest.
Request interface{}
// JSONPath is the predicate parameter for narrowing the scope of JSON
// comparison; leave nil to disable functionality.
JSONPath *JSONPath
// CaseSensitive determines if the match is case sensitive or not.
CaseSensitive bool
}
// HTTPResponse is a Response.Value used to respond to a matched HTTPRequest.
//
// See more information about HTTP responses in mountebank at:
// http://www.mbtest.org/docs/protocols/http.
type HTTPResponse struct {
// StatusCode is the HTTP status code of the response.
StatusCode int
// Headers are the HTTP headers in the response.
Headers http.Header
// Body is the body of the response. It will be JSON encoded before sending to mountebank
Body interface{}
// Mode is the mode of the response; either "text" or "binary".
// Defaults to "text" if excluded.
Mode string
}
// TCPResponse is a Response.Value to a matched incoming TCPRequest.
//
// See more information about TCP responses in mountebank at:
// http://www.mbtest.org/docs/protocols/tcp.
type TCPResponse struct {
// Data is the data in the data contained in the response.
// An empty string does not respond with data, but does send
// the FIN bit.
Data string
}
// Behaviors defines the possible response behaviors for a stub.
//
// See more information on stub behaviours in mountebank at:
// http://www.mbtest.org/docs/api/behaviors.
type Behaviors struct {
// Wait adds latency to a response by waiting a specified number of milliseconds before sending the response.
Wait int `json:"wait,omitempty"`
}
// Response defines a networked response sent by a Stub whenever an
// incoming Request matches one of its Predicates. Each Response is
// has a Type field that defines its behaviour. Its currently supported
// values are:
// is - Merges the specified Response fields with the defaults.
// proxy - Proxies the request to the specified destination and returns the response.
// inject - Creates the Response object based on the injected Javascript.
//
// See more information on stub responses in mountebank at:
// http://www.mbtest.org/docs/api/stubs.
type Response struct {
// Type is the type of the Response; one of "is", "proxy" or "inject".
Type string
// Value is the value of the Response; either of type HTTPResponse or TCPResponse.
Value interface{}
// Behaviors is an optional field allowing the user to define response behavior.
Behaviors *Behaviors
}
// Stub adds behaviour to Imposters where one or more registered Responses
// will be returned if an incoming request matches all of the registered
// Predicates. Any Stub value without Predicates always matches and returns
// its next Response. Note that the Responses slice acts as a circular-queue
// type structure, where every time the Stub matches an incoming request, the
// first Response is moved to the end of the slice. This allows for test cases
// to define and handle a sequence of Responses.
//
// See more information about stubs in mountebank at:
// http://www.mbtest.org/docs/api/stubs.
type Stub struct {
// Predicates are the list of Predicates associated with the Stub,
// which are logically AND'd together if more than one exists.
Predicates []Predicate
// Responses are the circular queue of Responses used to respond to
// incoming matched requests.
Responses []Response
}
// Imposter is the primary mountebank resource, representing a server/service
// that listens for networked traffic of a specified protocol and port, with the
// ability to match incoming requests and respond to them based on the behaviour
// of any attached Stub values.
//
// See one of the following links below for details on Imposter creation
// parameters, which varies by protocol:
//
// http://www.mbtest.org/docs/protocols/http
//
// http://www.mbtest.org/docs/protocols/tcp
type Imposter struct {
// Port is the listening port of the Imposter; required.
Port int
// Proto is the listening protocol of the Imposter; required.
Proto string
// Name is the name of the Imposter.
Name string
// RecordRequests adds mock verification support to the Imposter
// by having it remember any requests made to it, which can later
// be retrieved and examined by the testing environment.
RecordRequests bool
// Requests are the list of recorded requests, or nil if RecordRequests == false.
// Note that the underlying type will be HTTPRequest or TCPRequest depending on
// the protocol of the Imposter.
Requests []interface{}
// RequestCount is the number of matched requests received by the Imposter.
// Note that this value is only used/set when receiving Imposter data
// from the mountebank server.
RequestCount int
// AllowCORS will allow all CORS pre-flight requests on the Imposter.
AllowCORS bool
// DefaultResponse is the default response to send if no predicate matches.
// Only used by HTTP and TCP Imposters; should be one of HTTPResponse or TCPResponse.
DefaultResponse interface{}
// Stubs contains zero or more valid Stubs associated with the Imposter.
Stubs []Stub
}