Skip to content

Commit

Permalink
Merge pull request Moya#1593 from Moya/enhancement/stubbed-validation
Browse files Browse the repository at this point in the history
Perform validation on stubbed responses
  • Loading branch information
sunshinejr authored Feb 25, 2018
2 parents 6d57da7 + 3095767 commit 68a811f
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 10 deletions.
6 changes: 4 additions & 2 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Next

## Fixed
- Fixes Alamofire validation not being performed on `.uploadMultipart` requests.
### Fixed
- Fixed Alamofire validation not being performed on `.uploadMultipart` requests.
[#1591](https://github.com/Moya/Moya/pull/1591) by [@SD10](https://github.com/SD10).
- Fixed Alamofire validation not being performed on stubbed requests.
[#1593](https://github.com/Moya/Moya/pull/1593) by [@SD10](https://github.com/sd10).

# [11.0.0] - 2018-02-07
- No changes
Expand Down
22 changes: 18 additions & 4 deletions Sources/Moya/MoyaProvider+Internal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,29 @@ public extension MoyaProvider {
return
}

let validate = { (response: Moya.Response) -> Result<Moya.Response, MoyaError> in
let validCodes = target.validationType.statusCodes
guard !validCodes.isEmpty else { return .success(response) }
if validCodes.contains(response.statusCode) {
return .success(response)
} else {
let statusError = MoyaError.statusCode(response)
let error = MoyaError.underlying(statusError, response)
return .failure(error)
}
}

switch endpoint.sampleResponseClosure() {
case .networkResponse(let statusCode, let data):
let response = Moya.Response(statusCode: statusCode, data: data, request: request, response: nil)
plugins.forEach { $0.didReceive(.success(response), target: target) }
completion(.success(response))
let result = validate(response)
plugins.forEach { $0.didReceive(result, target: target) }
completion(result)
case .response(let customResponse, let data):
let response = Moya.Response(statusCode: customResponse.statusCode, data: data, request: request, response: customResponse)
plugins.forEach { $0.didReceive(.success(response), target: target) }
completion(.success(response))
let result = validate(response)
plugins.forEach { $0.didReceive(result, target: target) }
completion(result)
case .networkError(let error):
let error = MoyaError.underlying(error, nil)
plugins.forEach { $0.didReceive(.failure(error), target: target) }
Expand Down
81 changes: 77 additions & 4 deletions Tests/MoyaProviderSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -409,10 +409,9 @@ final class MoyaProviderSpec: QuickSpec {
}

it("returns identical sample response") {
let response = HTTPURLResponse(url: URL(string: "http://example.com")!, mimeType: nil, expectedContentLength: 0, textEncodingName: nil)
let response = HTTPURLResponse(url: URL(string: "http://example.com")!, statusCode: 200, httpVersion: nil, headerFields: nil)!
let endpointResolution: MoyaProvider<GitHub>.EndpointClosure = { target in
let url = target.baseURL.appendingPathComponent(target.path).absoluteString
return Endpoint(url: url, sampleResponseClosure: { .response(response, Data()) }, method: target.method, task: target.task, httpHeaderFields: target.headers)
return Endpoint(url: URL(target: target).absoluteString, sampleResponseClosure: { .response(response, target.sampleData) }, method: target.method, task: target.task, httpHeaderFields: target.headers)
}
let provider = MoyaProvider<GitHub>(endpointClosure: endpointResolution, stubClosure: MoyaProvider.immediatelyStub)

Expand Down Expand Up @@ -603,7 +602,7 @@ final class MoyaProviderSpec: QuickSpec {
}
}

expect(dataString) == "sample data"
expect(dataString).to(equal("sample data"))
}

it("uses correct headers") {
Expand Down Expand Up @@ -968,5 +967,79 @@ final class MoyaProviderSpec: QuickSpec {
}
}
}

// Resolves #1592 where validation is not performed on a stubbed request
describe("a provider for stubbed requests with validation") {
var stubbedProvider: MoyaProvider<GitHub>!

context("response contains invalid status code") {
it("returns an error") {
let endpointClosure = { (target: GitHub) -> Endpoint in
return Endpoint(
url: URL(target: target).absoluteString,
sampleResponseClosure: { .networkResponse(400, target.sampleData) },
method: target.method,
task: target.task,
httpHeaderFields: target.headers
)
}

stubbedProvider = MoyaProvider<GitHub>(endpointClosure: endpointClosure, stubClosure: MoyaProvider.immediatelyStub)

var receivedError: Error?
var receivedResponse: Response?

waitUntil { done in
stubbedProvider.request(.zen) { result in
switch result {
case .success(let response):
receivedResponse = response
case .failure(let error):
receivedError = error
}
done()
}
}

expect(receivedResponse).to(beNil())
expect(receivedError).toNot(beNil())
}
}

context("response contains valid status code") {
it("returns a response") {
let endpointClosure = { (target: GitHub) -> Endpoint in
return Endpoint(
url: URL(target: target).absoluteString,
sampleResponseClosure: { .networkResponse(200, target.sampleData) },
method: target.method,
task: target.task,
httpHeaderFields: target.headers
)
}

stubbedProvider = MoyaProvider<GitHub>(endpointClosure: endpointClosure, stubClosure: MoyaProvider.immediatelyStub)

var receivedError: Error?
var receivedResponse: Response?

waitUntil { done in
stubbedProvider.request(.zen) { result in
switch result {
case .success(let response):
receivedResponse = response
case .failure(let error):
receivedError = error
}
done()
}
}

expect(receivedResponse).toNot(beNil())
expect(receivedError).to(beNil())
expect(GitHub.zen.validationType.statusCodes).to(contain(receivedResponse!.statusCode))
}
}
}
}
}

0 comments on commit 68a811f

Please sign in to comment.