-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
/
TestHelpers.swift
408 lines (349 loc) · 14.3 KB
/
TestHelpers.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//
// HTTPBin.swift
//
// Copyright (c) 2014-2018 Alamofire Software Foundation (http://alamofire.org/)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import Alamofire
import Foundation
extension String {
static let invalidURL = "invalid"
static let nonexistentDomain = "https://nonexistent-domain.org"
}
extension URL {
static let nonexistentDomain = URL(string: .nonexistentDomain)!
}
struct Endpoint {
enum Scheme: String {
case http, https
var port: Int {
switch self {
case .http: return 80
case .https: return 443
}
}
}
enum Host: String {
case localhost = "127.0.0.1"
case httpBin = "httpbin.org"
func port(for scheme: Scheme) -> Int {
switch self {
case .localhost: return 8080
case .httpBin: return scheme.port
}
}
}
enum Path {
case basicAuth(username: String, password: String)
case bytes(count: Int)
case cache
case chunked(count: Int)
case compression(Compression)
case delay(interval: Int)
case digestAuth(qop: String = "auth", username: String, password: String)
case download(count: Int)
case hiddenBasicAuth(username: String, password: String)
case image(Image)
case ip
case method(HTTPMethod)
case payloads(count: Int)
case redirect(count: Int)
case redirectTo
case responseHeaders
case status(Int)
case stream(count: Int)
case xml
var string: String {
switch self {
case let .basicAuth(username: username, password: password):
return "/basic-auth/\(username)/\(password)"
case let .bytes(count):
return "/bytes/\(count)"
case .cache:
return "/cache"
case let .chunked(count):
return "/chunked/\(count)"
case let .compression(compression):
return "/\(compression.rawValue)"
case let .delay(interval):
return "/delay/\(interval)"
case let .digestAuth(qop, username, password):
return "/digest-auth/\(qop)/\(username)/\(password)"
case let .download(count):
return "/download/\(count)"
case let .hiddenBasicAuth(username, password):
return "/hidden-basic-auth/\(username)/\(password)"
case let .image(type):
return "/image/\(type.rawValue)"
case .ip:
return "/ip"
case let .method(method):
return "/\(method.rawValue.lowercased())"
case let .payloads(count):
return "/payloads/\(count)"
case let .redirect(count):
return "/redirect/\(count)"
case .redirectTo:
return "/redirect-to"
case .responseHeaders:
return "/response-headers"
case let .status(code):
return "/status/\(code)"
case let .stream(count):
return "/stream/\(count)"
case .xml:
return "/xml"
}
}
}
enum Image: String {
case jpeg
}
enum Compression: String {
case brotli, gzip, deflate
}
static var get: Endpoint { method(.get) }
static func basicAuth(forUser user: String = "user", password: String = "password") -> Endpoint {
Endpoint(path: .basicAuth(username: user, password: password))
}
static func bytes(_ count: Int) -> Endpoint {
Endpoint(path: .bytes(count: count))
}
static let cache: Endpoint = .init(path: .cache)
static func chunked(_ count: Int) -> Endpoint {
Endpoint(path: .chunked(count: count))
}
static func compression(_ compression: Compression) -> Endpoint {
Endpoint(path: .compression(compression))
}
static var `default`: Endpoint { .get }
static func delay(_ interval: Int) -> Endpoint {
Endpoint(path: .delay(interval: interval))
}
static func digestAuth(forUser user: String = "user", password: String = "password") -> Endpoint {
Endpoint(path: .digestAuth(username: user, password: password))
}
static func download(_ count: Int = 10_000, produceError: Bool = false) -> Endpoint {
Endpoint(path: .download(count: count), queryItems: [.init(name: "shouldProduceError",
value: "\(produceError)")])
}
static func hiddenBasicAuth(forUser user: String = "user", password: String = "password") -> Endpoint {
Endpoint(path: .hiddenBasicAuth(username: user, password: password),
headers: [.authorization(username: user, password: password)])
}
static func image(_ type: Image) -> Endpoint {
Endpoint(path: .image(type))
}
static var ip: Endpoint {
Endpoint(path: .ip)
}
static func method(_ method: HTTPMethod) -> Endpoint {
Endpoint(path: .method(method), method: method)
}
static func payloads(_ count: Int) -> Endpoint {
Endpoint(path: .payloads(count: count))
}
static func redirect(_ count: Int) -> Endpoint {
Endpoint(path: .redirect(count: count))
}
static func redirectTo(_ url: String, code: Int? = nil) -> Endpoint {
var items = [URLQueryItem(name: "url", value: url)]
items = code.map { items + [.init(name: "statusCode", value: "\($0)")] } ?? items
return Endpoint(path: .redirectTo, queryItems: items)
}
static func redirectTo(_ endpoint: Endpoint, code: Int? = nil) -> Endpoint {
var items = [URLQueryItem(name: "url", value: endpoint.url.absoluteString)]
items = code.map { items + [.init(name: "statusCode", value: "\($0)")] } ?? items
return Endpoint(path: .redirectTo, queryItems: items)
}
static var responseHeaders: Endpoint {
Endpoint(path: .responseHeaders)
}
static func status(_ code: Int) -> Endpoint {
Endpoint(path: .status(code))
}
static func stream(_ count: Int) -> Endpoint {
Endpoint(path: .stream(count: count))
}
static var xml: Endpoint {
Endpoint(path: .xml, headers: [.contentType("application/xml")])
}
var scheme = Scheme.http
var port: Int { host.port(for: scheme) }
var host = Host.localhost
var path = Path.method(.get)
var method: HTTPMethod = .get
var headers: HTTPHeaders = .init()
var timeout: TimeInterval = 60
var queryItems: [URLQueryItem] = []
var cachePolicy: URLRequest.CachePolicy = .useProtocolCachePolicy
func modifying<T>(_ keyPath: WritableKeyPath<Endpoint, T>, to value: T) -> Endpoint {
var copy = self
copy[keyPath: keyPath] = value
return copy
}
}
extension Endpoint: URLRequestConvertible {
var urlRequest: URLRequest { try! asURLRequest() }
func asURLRequest() throws -> URLRequest {
var request = try URLRequest(url: asURL())
request.method = method
request.headers = headers
request.timeoutInterval = timeout
request.cachePolicy = cachePolicy
return request
}
}
extension Endpoint: URLConvertible {
var url: URL { try! asURL() }
func asURL() throws -> URL {
var components = URLComponents()
components.scheme = scheme.rawValue
components.port = port
components.host = host.rawValue
components.path = path.string
if !queryItems.isEmpty {
components.queryItems = queryItems
}
return try components.asURL()
}
}
extension Session {
func request(_ endpoint: Endpoint,
parameters: Parameters? = nil,
encoding: ParameterEncoding = URLEncoding.default,
headers: HTTPHeaders? = nil,
interceptor: RequestInterceptor? = nil,
requestModifier: RequestModifier? = nil) -> DataRequest {
request(endpoint as URLConvertible,
method: endpoint.method,
parameters: parameters,
encoding: encoding,
headers: headers,
interceptor: interceptor,
requestModifier: requestModifier)
}
func request<Parameters: Encodable>(_ endpoint: Endpoint,
parameters: Parameters? = nil,
encoder: ParameterEncoder = URLEncodedFormParameterEncoder.default,
headers: HTTPHeaders? = nil,
interceptor: RequestInterceptor? = nil,
requestModifier: RequestModifier? = nil) -> DataRequest {
request(endpoint as URLConvertible,
method: endpoint.method,
parameters: parameters,
encoder: encoder,
headers: headers,
interceptor: interceptor,
requestModifier: requestModifier)
}
func request(_ endpoint: Endpoint, interceptor: RequestInterceptor? = nil) -> DataRequest {
request(endpoint as URLRequestConvertible, interceptor: interceptor)
}
func streamRequest(_ endpoint: Endpoint,
headers: HTTPHeaders? = nil,
automaticallyCancelOnStreamError: Bool = false,
interceptor: RequestInterceptor? = nil,
requestModifier: RequestModifier? = nil) -> DataStreamRequest {
streamRequest(endpoint as URLConvertible,
method: endpoint.method,
headers: headers,
automaticallyCancelOnStreamError: automaticallyCancelOnStreamError,
interceptor: interceptor,
requestModifier: requestModifier)
}
func streamRequest(_ endpoint: Endpoint,
automaticallyCancelOnStreamError: Bool = false,
interceptor: RequestInterceptor? = nil) -> DataStreamRequest {
streamRequest(endpoint as URLRequestConvertible,
automaticallyCancelOnStreamError: automaticallyCancelOnStreamError,
interceptor: interceptor)
}
func download<Parameters: Encodable>(_ endpoint: Endpoint,
parameters: Parameters? = nil,
encoder: ParameterEncoder = URLEncodedFormParameterEncoder.default,
headers: HTTPHeaders? = nil,
interceptor: RequestInterceptor? = nil,
requestModifier: RequestModifier? = nil,
to destination: DownloadRequest.Destination? = nil) -> DownloadRequest {
download(endpoint as URLConvertible,
method: endpoint.method,
parameters: parameters,
encoder: encoder,
headers: headers,
interceptor: interceptor,
requestModifier: requestModifier,
to: destination)
}
func download(_ endpoint: Endpoint,
parameters: Parameters? = nil,
encoding: ParameterEncoding = URLEncoding.default,
headers: HTTPHeaders? = nil,
interceptor: RequestInterceptor? = nil,
requestModifier: RequestModifier? = nil,
to destination: DownloadRequest.Destination? = nil) -> DownloadRequest {
download(endpoint as URLConvertible,
method: endpoint.method,
parameters: parameters,
encoding: encoding,
headers: headers,
interceptor: interceptor,
requestModifier: requestModifier,
to: destination)
}
func download(_ endpoint: Endpoint,
interceptor: RequestInterceptor? = nil,
to destination: DownloadRequest.Destination? = nil) -> DownloadRequest {
download(endpoint as URLRequestConvertible, interceptor: interceptor, to: destination)
}
func upload(_ data: Data,
to endpoint: Endpoint,
headers: HTTPHeaders? = nil,
interceptor: RequestInterceptor? = nil,
fileManager: FileManager = .default,
requestModifier: RequestModifier? = nil) -> UploadRequest {
upload(data, to: endpoint as URLConvertible,
method: endpoint.method,
headers: headers,
interceptor: interceptor,
fileManager: fileManager,
requestModifier: requestModifier)
}
}
extension Data {
var asString: String {
String(decoding: self, as: UTF8.self)
}
func asJSONObject() throws -> Any {
try JSONSerialization.jsonObject(with: self, options: .allowFragments)
}
}
struct TestResponse: Decodable {
let headers: [String: String]
let origin: String
let url: String?
let data: String?
let form: [String: String]?
let args: [String: String]?
}
struct TestParameters: Encodable {
static let `default` = TestParameters(property: "property")
let property: String
}