-
Notifications
You must be signed in to change notification settings - Fork 1
/
MockOverrides.kt
74 lines (54 loc) · 2.42 KB
/
MockOverrides.kt
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
import Foundation
import OHHTTPStubs
public class MockNetworkRequestManager {
public static var sharedManager = MockNetworkRequestManager()
var awwwStub: OHHTTPStubsDescriptor!
var films_2Stub: OHHTTPStubsDescriptor!
// MARK: Helper Functions
init() {
OHHTTPStubs.onStubActivation { (request: URLRequest, stub: OHHTTPStubsDescriptor, response: OHHTTPStubsResponse) in
print("[OHHTTPStubs] Request to \(String(describing: request.url)) has been stubbed with \(String(describing: stub.name))")
}
}
public func stopAllMockEndpoints() {
OHHTTPStubs.removeAllStubs()
}
func getHTTPBodyFromURLRequest(_ urlRequest: URLRequest) -> [String: AnyObject]? {
var jsonDict: [String: AnyObject]?
if let body = (urlRequest as NSURLRequest).ohhttpStubs_HTTPBody() {
do {
jsonDict = try JSONSerialization.jsonObject(with: body, options: []) as? [String: AnyObject]
} catch let error {
assert(false, error.localizedDescription)
}
return jsonDict
}
return nil
}
}
public extension MockNetworkRequestManager {
func startAllMockEndpoints() {
// MARK: - awww
awwwStub = stub(condition: { urlRequest in
return (urlRequest.url?.absoluteString.containsString("Awww.json")) ?? false
}, response: { urlRequest in
guard let stubPath = OHPathForFile("awww.json", type(of: self)) else {
return OHHTTPStubsResponse()
}
let headers: Dictionary = ["Accept-Language": "en-US","Domain": "TEST","Country-Code": "US","Channel-Type": "Web","Customer-IP-Address": "127.0.0.1","Content-Type": "application/json",]
let stubResponse = OHHTTPStubsResponse(fileAtPath: stubPath, statusCode: 200, headers: headers)
return stubResponse
})
// MARK: - films_2
films_2Stub = stub(condition: { urlRequest in
return (urlRequest.url?.absoluteString.containsString("films,2")) ?? false
}, response: { urlRequest in
guard let stubPath = OHPathForFile("films_2.json", type(of: self)) else {
return OHHTTPStubsResponse()
}
let headers: Dictionary = []
let stubResponse = OHHTTPStubsResponse(fileAtPath: stubPath, statusCode: 200, headers: headers)
return stubResponse
})
}
}