-
Notifications
You must be signed in to change notification settings - Fork 15
/
Interaction.swift
306 lines (278 loc) · 9.52 KB
/
Interaction.swift
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
//
// Created by Marko Justinek on 1/4/20.
// Copyright © 2020 Marko Justinek. All rights reserved.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
// IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
import Foundation
/// Defines the interaction between a consumer and a provider
@objc public class Interaction: NSObject {
var interactionDescription: String?
var providerState: String?
var providerStates: [ProviderState]?
var request: Request?
var response: Response?
internal var encodingErrors = [EncodingError]()
}
extension Interaction: Encodable {
enum CodingKeys: String, CodingKey {
case interactionDescription = "description"
case providerState
case providerStates
case request
case response
}
}
// MARK: - Initialization
extension Interaction {
@objc convenience init(description: String) {
self.init()
self.interactionDescription = description
}
convenience init(description: String, providerState: String, request: Request? = nil, response: Response? = nil) {
self.init()
self.interactionDescription = description
self.providerState = providerState
self.request = request
self.response = response
}
convenience init(description: String, providerStates: [ProviderState], request: Request? = nil, response: Response? = nil) {
self.init()
self.interactionDescription = description
self.providerStates = providerStates
self.request = request
self.response = response
}
}
// MARK: - Interface
extension Interaction {
/// Defines the description of the interaction
///
/// It is important to provide a meaningful description so that
/// developers on the provider side can understand what the
/// intent of the interaction is. This description is also listed in
/// the list of interactions on Pact Broker.
///
/// Example:
/// ```
/// .uponReceiving("A request for a list of users")
/// ```
///
/// - Parameters:
/// - interactionDescription: A `String` describing the interaction
///
@discardableResult
func uponReceiving(_ interactionDescription: String) -> Interaction {
self.interactionDescription = interactionDescription
return self
}
/// Defines the provider state for the given interaction
///
/// It is important to provide a meaningful description with
/// values that help prepare provider Pact tests.
///
/// Example:
/// ```users exist```
///
/// - Parameters:
/// - providerState: Description of the state.
///
@discardableResult
public func given(_ providerState: String) -> Interaction {
self.providerState = providerState
return self
}
/// Defines the provider states with parameters for the given interaction
///
/// It is important to provide a meaningful description with
/// values that help prepare provider side Pact tests.
///
/// Example:
/// ```
/// .given([
/// ProviderState(
/// description: "user exists",
/// params: ["id": "1"]
/// )
/// ])
/// ```
///
/// - Parameters:
/// - providerStates: A list of provider states
///
@discardableResult
public func given(_ providerStates: [ProviderState]) -> Interaction {
self.providerStates = providerStates
return self
}
/// Defines the provider state for the given interaction
///
/// It is important to provide a meaningful description with
/// values that help prepare provider side Pact tests.
///
/// Example:
/// ```
/// .given(
/// ProviderState(
/// description: "user exists",
/// params: ["id": "1", "name": "Mary"]
/// ),
/// ProviderState(
/// description: "user has children",
/// params: ["count": "2"]
/// )
/// )
/// ```
///
/// - Parameters:
/// - providerStates: A list of provider states
///
public func given(_ providerStates: ProviderState...) -> Interaction {
given(providerStates)
}
/// Defines the expected request for the interaction
///
/// At a minimum the `method` and `path` are required to define an interaction.
///
/// `query` expects a dictionary where the value is an array conforming
/// to `String` or a string `Matcher`.
///
/// ```
/// // Verbatim matching for '?states=VIC,NSW,ACT'
/// query: ["states": ["ACT", "NSW", "VIC"]]
///
/// // Matching using a string Matcher for
/// query: ["states": [Matcher.SomethingLike("VIC")]] // or
/// query: ["states": [Matcher.IncludesLike("VIC")]
/// ```
///
/// - Parameters:
/// - method: The HTTP method of the request
/// - path: The URL path of the request
/// - query: The query parameters of the request
/// - headers: The header parameters of the request
/// - body: The body of the request
///
@discardableResult
public func withRequest(method: PactHTTPMethod, path: PactPathParameter, query: [String: [Any]]? = nil, headers: [String: Any]? = nil, body: Any? = nil) -> Interaction {
do {
self.request = try Request(method: method, path: path, query: query, headers: headers, body: body)
} catch let requestError {
encodingErrors.append(requestError as! EncodingError)
Logger.log(message: "Can not prepare a Request with non-encodable data! \(requestError)")
}
return self
}
/// Defines the expected response for the interaction. It defines the
/// values `MockService` will respond with when it receives the expected
/// request as defined in this interaction.
///
/// At a minimum the `status` is required to test an API response.
/// By not providing a value for `headers` or `body` it is understood
/// that the presence of those values in the response is _not required_
/// but they can be present.
///
/// - Parameters:
/// - status: The response status code
/// - headers: The response headers
/// - body: The response body
///
@discardableResult
public func willRespondWith(status: Int, headers: [String: Any]? = nil, body: Any? = nil) -> Interaction {
do {
self.response = try Response(statusCode: status, headers: headers, body: body)
} catch let responseError {
encodingErrors.append(responseError as! EncodingError)
Logger.log(message: "Can not prepare a Response with non-encodable data! \(responseError)")
}
return self
}
// MARK: - Objective-C -
/// Defines the description of the interaction
///
/// It is important to provide a meaningful description so that
/// developers on the provider side can understand what the
/// intent of the interaction is. This description is also listed in
/// the list of interactions on Pact Broker.
///
/// - Parameters:
/// - interactionDescription: A `String` describing the interaction
///
@discardableResult
@objc(uponReceivingARequestWithDescription:)
func objCUponReceiving(_ interactionDescription: String) -> Interaction {
uponReceiving(interactionDescription)
}
/// Defines the provider state for the given interaction
///
/// It is important to provide a meaningful description with
/// values that help prepare provider Pact tests.
///
/// - Parameters:
/// - providerState: Description of the state.
///
@discardableResult
@objc(givenProviderState:)
public func objCGiven(_ providerState: String) -> Interaction {
given(providerState)
}
/// Defines the provider states with parameters for the given interaction
///
/// It is important to provide a meaningful description with
/// values that help prepare provider side Pact tests.
///
/// - Parameters:
/// - providerStates: A list of provider states
///
@discardableResult
@objc(givenProviderStates:)
public func objCGiven(_ providerStates: [ObjCProviderState]) -> Interaction {
self.providerStates = providerStates.map { $0.state }
return self
}
/// Defines the expected request for the interaction
///
/// - Parameters:
/// - method: The HTTP method of the request
/// - path: The URL path of the request
/// - query: The query parameters of the request
/// - headers: The header parameters of the request
/// - body: The body of the request
///
@discardableResult
@objc(withRequestHTTPMethod: path: query: headers: body:)
public func objCWithRequest(method: PactHTTPMethod, path: String, query: [String: [Any]]? = nil, headers: [String: Any]? = nil, body: Any? = nil) -> Interaction {
withRequest(method: method, path: path, query: query, headers: headers, body: body)
return self
}
/// Defines the expected response for the interaction. It defines the
/// values `MockService` will respond with when it receives the expected
/// request as defined in this interaction.
///
/// At a minimum the `status` is required to test an API response.
/// By not providing a value for `headers` or `body` it is understood
/// that the presence of those values in the response is _not required_
/// but they can be present.
///
/// - Parameters:
/// - status: The response status code
/// - headers: The response headers
/// - body: The response body
///
@discardableResult
@objc(willRespondWithStatus: headers: body:)
public func objCWillRespondWith(status: Int, headers: [String: Any]? = nil, body: Any? = nil) -> Interaction {
willRespondWith(status: status, headers: headers, body: body)
return self
}
}