-
Notifications
You must be signed in to change notification settings - Fork 2k
/
NetworkLoggerPluginSpec.swift
236 lines (176 loc) · 9.6 KB
/
NetworkLoggerPluginSpec.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
import Quick
import Nimble
import Moya
import Foundation
import enum Alamofire.AFError
final class NetworkLoggerPluginSpec: QuickSpec {
override func spec() {
var log = ""
let customLoggerOutput: NetworkLoggerPlugin.Configuration.OutputType = { log += $1.joined() }
let plugin: NetworkLoggerPlugin = {
let plugin = NetworkLoggerPlugin()
plugin.configuration.output = customLoggerOutput
plugin.configuration.logOptions = .verbose
return plugin
}()
beforeEach {
log = ""
}
it("outputs all request fields with body") {
plugin.willSend(TestBodyRequest(), target: GitHub.zen)
let possibleHeaders = ["Request Headers: [\"Accept-Language\": \"en-US\", \"Content-Type\": \"application/json\"]",
"Request Headers: [\"Content-Type\": \"application/json\", \"Accept-Language\": \"en-US\"]"]
expect(log).to(contain("Request: https://api.github.com/zen"))
expect(log).to(containOne(of: possibleHeaders))
expect(log).to(contain("HTTP Request Method: GET"))
expect(log).to(contain("Request Body: cool body"))
}
it("outputs all request fields with stream") {
plugin.willSend(TestStreamRequest(), target: GitHub.zen)
let possibleHeaders = ["Request Headers: [\"Accept-Language\": \"en-US\", \"Content-Type\": \"application/json\"]",
"Request Headers: [\"Content-Type\": \"application/json\", \"Accept-Language\": \"en-US\"]"]
expect(log).to(contain("Request: https://api.github.com/zen"))
expect(log).to(containOne(of: possibleHeaders))
expect(log).to(contain("HTTP Request Method: GET"))
expect(log).to(contain("Request Body Stream:"))
}
it("will output invalid request when reguest is nil") {
plugin.willSend(TestNilRequest(), target: GitHub.zen)
expect(log).to(contain("Request: (invalid request)"))
}
it("outputs the response data") {
let response = Response(statusCode: 200, data: "cool body".data(using: .utf8)!, response: HTTPURLResponse(url: URL(string: url(GitHub.zen))!, mimeType: nil, expectedContentLength: 0, textEncodingName: nil))
let result: Result<Moya.Response, MoyaError> = .success(response)
plugin.didReceive(result, target: GitHub.zen)
expect(log).to(contain("Response:"))
expect(log).to(contain("{ URL: https://api.github.com/zen }"))
expect(log).to(contain("Response Body: cool body"))
}
it("outputs a validation error message") {
let response = Response(statusCode: 500, data: "Internal error".data(using: .utf8)!, response: HTTPURLResponse(url: URL(string: url(GitHub.zen))!, mimeType: nil, expectedContentLength: 0, textEncodingName: nil))
let validationResponseError = AFError.responseValidationFailed(reason: .unacceptableStatusCode(code: 500))
let result: Result<Moya.Response, MoyaError> = .failure(.underlying(validationResponseError, response))
plugin.didReceive(result, target: GitHub.zen)
expect(log).to( contain("Response:") )
expect(log).to( contain("{ URL: https://api.github.com/zen }") )
expect(log).to( contain("Internal error") )
}
it("outputs a serialization error message") {
let emptyResponseError = AFError.responseSerializationFailed(reason: .inputFileNil)
let result: Result<Moya.Response, MoyaError> = .failure(.underlying(emptyResponseError, nil))
plugin.didReceive(result, target: GitHub.zen)
expect(log).to( contain("Error calling zen : underlying(Alamofire.AFError.responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.inputFileNil), nil)") )
}
it("outputs cURL representation of request") {
let pluginWithCurl: NetworkLoggerPlugin = {
let plugin = NetworkLoggerPlugin()
plugin.configuration.output = customLoggerOutput
plugin.configuration.logOptions = [.formatRequestAscURL]
return plugin
}()
pluginWithCurl.willSend(TestCurlBodyRequest(), target: GitHub.zen)
expect(log).to(contain("$ curl -i"))
expect(log).to(contain("-H \"Content-Type: application/json\""))
expect(log).to(contain("-d \"cool body\""))
expect(log).to(contain("\"https://api.github.com/zen\""))
}
it("outputs the formatted request data") {
let pluginWithRequestDataFormatter: NetworkLoggerPlugin = {
let plugin = NetworkLoggerPlugin()
plugin.configuration.output = customLoggerOutput
plugin.configuration.logOptions = .verbose
plugin.configuration.formatter.requestData = { _ in "formatted request body" }
return plugin
}()
pluginWithRequestDataFormatter.willSend(TestBodyRequest(), target: GitHub.zen)
expect(log).to(contain("Request: https://api.github.com/zen"))
expect(log).to(contain("Body: formatted request body"))
}
it("outputs the customized entries") {
let pluginWithEntryFormatter: NetworkLoggerPlugin = {
let plugin = NetworkLoggerPlugin()
plugin.configuration.output = customLoggerOutput
plugin.configuration.formatter.entry = { _, _, _ in "formatted log entry" }
return plugin
}()
pluginWithEntryFormatter.willSend(TestBodyRequest(), target: GitHub.zen)
expect(log).to(contain("formatted log entry"))
}
it("outputs the formatted response data") {
let response = Response(statusCode: 200, data: "cool body".data(using: .utf8)!, response: HTTPURLResponse(url: URL(string: url(GitHub.zen))!, mimeType: nil, expectedContentLength: 0, textEncodingName: nil))
let result: Result<Moya.Response, MoyaError> = .success(response)
let pluginWithResponseDataFormatter: NetworkLoggerPlugin = {
let plugin = NetworkLoggerPlugin()
plugin.configuration.output = customLoggerOutput
plugin.configuration.formatter.responseData = { _ in "formatted response body" }
plugin.configuration.logOptions = .verbose
return plugin
}()
pluginWithResponseDataFormatter.didReceive(result, target: GitHub.zen)
expect(log).to(contain("Response:"))
expect(log).to(contain("{ URL: https://api.github.com/zen }"))
expect(log).to(contain("Response Body: formatted response body"))
}
}
}
private class TestStreamRequest: RequestType {
var request: URLRequest? {
var request = URLRequest(url: URL(string: url(GitHub.zen))!)
request.allHTTPHeaderFields = ["Content-Type": "application/json"]
request.httpBodyStream = InputStream(data: "cool body".data(using: .utf8)!)
return request
}
var sessionHeaders: [String: String] {
["Content-Type": "application/badJson", "Accept-Language": "en-US"]
}
func authenticate(username user: String, password: String, persistence: URLCredential.Persistence) -> Self { self }
func authenticate(with credential: URLCredential) -> Self { self }
func cURLDescription(calling handler: @escaping (String) -> Void) -> Self {
handler("")
return self
}
}
private class TestBodyRequest: RequestType {
var request: URLRequest? {
var request = URLRequest(url: URL(string: url(GitHub.zen))!)
request.allHTTPHeaderFields = ["Content-Type": "application/json"]
request.httpBody = "cool body".data(using: .utf8)
return request
}
var sessionHeaders: [String: String] {
["Content-Type": "application/badJson", "Accept-Language": "en-US"]
}
func authenticate(username user: String, password: String, persistence: URLCredential.Persistence) -> Self { self }
func authenticate(with credential: URLCredential) -> Self { self }
func cURLDescription(calling handler: @escaping (String) -> Void) -> Self {
handler("")
return self
}
}
private class TestCurlBodyRequest: RequestType {
var request: URLRequest? {
var request = URLRequest(url: URL(string: url(GitHub.zen))!)
request.allHTTPHeaderFields = ["Content-Type": "application/json"]
request.httpBody = "cool body".data(using: .utf8)
return request
}
var sessionHeaders: [String: String] {
["Content-Type": "application/badJson", "Accept-Language": "en-US"]
}
func authenticate(username user: String, password: String, persistence: URLCredential.Persistence) -> Self { self }
func authenticate(with credential: URLCredential) -> Self { self }
func cURLDescription(calling handler: @escaping (String) -> Void) -> Self {
handler(["$ curl -i", "-H \"Content-Type: application/json\"", "-d \"cool body\"", "\"https://api.github.com/zen\""].joined(separator: " \\\n\t"))
return self
}
}
private class TestNilRequest: RequestType {
var request: URLRequest? { nil }
var sessionHeaders: [String: String] { [:] }
func authenticate(username user: String, password: String, persistence: URLCredential.Persistence) -> Self { self }
func authenticate(with credential: URLCredential) -> Self { self }
func cURLDescription(calling handler: @escaping (String) -> Void) -> Self {
handler("")
return self
}
}