Skip to content

Commit 24a81ee

Browse files
committed
call willSendRequest for multipart requests.
1 parent dded3ad commit 24a81ee

File tree

4 files changed

+32
-9
lines changed

4 files changed

+32
-9
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# Change Log
22
All notable changes to this project will be documented in this file.
33

4+
## [0.4.1](https://github.com/MLSDev/TRON/releases/tag/0.4.1)
5+
6+
### Fixed
7+
8+
* Plugins are now correctly notified with "willSendRequest(_:)" method call for multipart requests.
9+
410
## [0.4.0](https://github.com/MLSDev/TRON/releases/tag/0.4.0)
511

612
### Breaking

Source/Core/APIRequest.swift

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -182,26 +182,23 @@ public class APIRequest<Model: ResponseParseable, ErrorModel: ResponseParseable>
182182
guard let manager = tronDelegate?.manager else {
183183
fatalError("Manager cannot be nil while performing APIRequest")
184184
}
185-
let alamofireRequest = manager.request(method, urlBuilder.urlForPath(path),
185+
let request = manager.request(method, urlBuilder.urlForPath(path),
186186
parameters: parameters,
187187
encoding: encoding,
188188
headers: headerBuilder.headersForAuthorization(authorizationRequirement, headers: headers))
189189

190190
// Notify plugins about new network request
191-
tronDelegate?.plugins.forEach {
192-
$0.willSendRequest(alamofireRequest.request)
193-
}
194-
plugins.forEach {
195-
$0.willSendRequest(alamofireRequest.request)
196-
}
197191
let allPlugins = plugins + (tronDelegate?.plugins ?? [])
198-
alamofireRequest.validate().handleResponse(success,
192+
allPlugins.forEach {
193+
$0.willSendRequest(request.request)
194+
}
195+
request.validate().handleResponse(success,
199196
failure: failure,
200197
dispatcher : dispatcher,
201198
responseBuilder: responseBuilder,
202199
errorBuilder: errorBuilder,
203200
plugins: allPlugins)
204-
return alamofireRequest
201+
return request
205202
}
206203
}
207204

Source/Core/MultipartAPIRequest.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ public class MultipartAPIRequest<Model: ResponseParseable, ErrorModel: ResponseP
105105
failure?(apiError)
106106
} else if case .Success(let request, _, _) = completion {
107107
let allPlugins = self.plugins + (self.tronDelegate?.plugins ?? [])
108+
allPlugins.forEach {
109+
$0.willSendRequest(request.request)
110+
}
108111
request.progress(progress).validate().handleResponse(success, failure: failure, dispatcher: self.dispatcher, responseBuilder: self.responseBuilder, errorBuilder: self.errorBuilder, plugins: allPlugins)
109112
cancellableCallback(request)
110113
}

Tests/PluginTestCase.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,21 @@ class PluginTestCase: XCTestCase {
5050

5151
}
5252

53+
func testMultipartRequestsCallGlobalAndLocalPlugins() {
54+
let globalPluginTester = PluginTester()
55+
let localPluginTester = PluginTester()
56+
57+
let tron = TRON(baseURL: "http://httpbin.org")
58+
let request: MultipartAPIRequest<String,Int> = tron.multipartRequest(path: "status/200")
59+
request.plugins.append(localPluginTester)
60+
tron.plugins.append(globalPluginTester)
61+
62+
request.performWithSuccess( { _ in }, failure: nil, progress: { (_) in }, cancellableCallback: { _ in })
63+
64+
expect(localPluginTester.willSendCalled).toEventually(equal(true))
65+
expect(globalPluginTester.willSendCalled).toEventually(equal(true))
66+
expect(localPluginTester.didReceiveResponseCalled).toEventually(equal(true))
67+
expect(globalPluginTester.didReceiveResponseCalled).toEventually(equal(true))
68+
}
69+
5370
}

0 commit comments

Comments
 (0)