Skip to content

Commit

Permalink
Merge pull request Moya#1591 from Moya/bugfix/multipart-validation
Browse files Browse the repository at this point in the history
Fix Alamofire validation not being performed on uploadMultipart
  • Loading branch information
sunshinejr authored Feb 24, 2018
2 parents 77cd637 + 77f8622 commit 6d57da7
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 12 deletions.
5 changes: 5 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Next

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

# [11.0.0] - 2018-02-07
- No changes

# [11.0.0-beta.2] - 2018-01-27
## Changed
Expand Down
4 changes: 3 additions & 1 deletion Sources/Moya/MoyaProvider+Internal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,9 @@ private extension MoyaProvider {
self.cancelCompletion(completion, target: target)
return
}
cancellable.innerCancellable = self.sendAlamofireRequest(alamoRequest, target: target, callbackQueue: callbackQueue, progress: progress, completion: completion)
let validationCodes = target.validationType.statusCodes
let validatedRequest = validationCodes.isEmpty ? alamoRequest : alamoRequest.validate(statusCode: validationCodes)
cancellable.innerCancellable = self.sendAlamofireRequest(validatedRequest, target: target, callbackQueue: callbackQueue, progress: progress, completion: completion)
case .failure(let error):
completion(.failure(MoyaError.underlying(error as NSError, nil)))
}
Expand Down
49 changes: 49 additions & 0 deletions Tests/MoyaProviderIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,55 @@ final class MoyaProviderIntegrationTests: QuickSpec {
}
}
}

// Resolves ValidationType not working with multipart upload #1590
describe("a provider performing a multipart upload with Alamofire validation") {
let provider = MoyaProvider<HTTPBin>()
let formData = HTTPBin.createTestMultipartFormData()

it("returns an error for status code different than 287") {
let target = HTTPBin.validatedUploadMultipart(formData, nil, [287])
var receievedResponse: Response?
var receivedError: Error?

waitUntil(timeout: 5.0) { done in
provider.request(target) { result in
switch result {
case .success(let response):
receievedResponse = response
case .failure(let error):
receivedError = error
}
done()
}
}

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

it("returns a valid response for .succesCodes") {
let successCodes = ValidationType.successCodes.statusCodes
let target = HTTPBin.validatedUploadMultipart(formData, nil, successCodes)
var receievedResponse: Response?
var receivedError: Error?

waitUntil(timeout: 5.0) { done in
provider.request(target) { result in
switch result {
case .success(let response):
receievedResponse = response
case .failure(let error):
receivedError = error
}
done()
}
}

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

Expand Down
9 changes: 2 additions & 7 deletions Tests/MoyaProviderSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -841,13 +841,8 @@ final class MoyaProviderSpec: QuickSpec {

it("tracks progress of multipart request") {

let url = Bundle(for: MoyaProviderSpec.self).url(forResource: "testImage", withExtension: "png")!
let string = "some data"
guard let data = string.data(using: .utf8) else { fatalError("Failed creating Data from String \(string)") }
let target: HTTPBin = .uploadMultipart([
MultipartFormData(provider: .file(url), name: "file", fileName: "testImage"),
MultipartFormData(provider: .data(data), name: "data")
], nil)
let formData = HTTPBin.createTestMultipartFormData()
let target = HTTPBin.uploadMultipart(formData, nil)

var progressObjects: [Progress?] = []
var progressValues: [Double] = []
Expand Down
36 changes: 32 additions & 4 deletions Tests/TestHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,14 @@ enum HTTPBin: TargetType {
case post
case upload(file: URL)
case uploadMultipart([MultipartFormData], [String: Any]?)
case validatedUploadMultipart([MultipartFormData], [String: Any]?, [Int])

var baseURL: URL { return URL(string: "http://httpbin.org")! }
var path: String {
switch self {
case .basicAuth:
return "/basic-auth/user/passwd"
case .post, .upload, .uploadMultipart:
case .post, .upload, .uploadMultipart, .validatedUploadMultipart:
return "/post"
}
}
Expand All @@ -89,7 +90,7 @@ enum HTTPBin: TargetType {
switch self {
case .basicAuth:
return .get
case .post, .upload, .uploadMultipart:
case .post, .upload, .uploadMultipart, .validatedUploadMultipart:
return .post
}
}
Expand All @@ -100,7 +101,7 @@ enum HTTPBin: TargetType {
return .requestParameters(parameters: [:], encoding: URLEncoding.default)
case .upload(let fileURL):
return .uploadFile(fileURL)
case .uploadMultipart(let data, let urlParameters):
case .uploadMultipart(let data, let urlParameters), .validatedUploadMultipart(let data, let urlParameters, _):
if let urlParameters = urlParameters {
return .uploadCompositeMultipart(data, urlParameters: urlParameters)
} else {
Expand All @@ -113,14 +114,23 @@ enum HTTPBin: TargetType {
switch self {
case .basicAuth:
return "{\"authenticated\": true, \"user\": \"user\"}".data(using: String.Encoding.utf8)!
case .post, .upload, .uploadMultipart:
case .post, .upload, .uploadMultipart, .validatedUploadMultipart:
return "{\"args\": {}, \"data\": \"\", \"files\": {}, \"form\": {}, \"headers\": { \"Connection\": \"close\", \"Content-Length\": \"0\", \"Host\": \"httpbin.org\" }, \"json\": null, \"origin\": \"198.168.1.1\", \"url\": \"https://httpbin.org/post\"}".data(using: String.Encoding.utf8)!
}
}

var headers: [String: String]? {
return nil
}

var validationType: ValidationType {
switch self {
case .validatedUploadMultipart(_, _, let codes):
return .customCodes(codes)
default:
return .none
}
}
}

public enum GitHubUserContent {
Expand Down Expand Up @@ -171,6 +181,24 @@ extension GitHubUserContent: TargetType {
}
}

// MARK: - Upload Multipart Helpers

extension HTTPBin {
static func createTestMultipartFormData() -> [MultipartFormData] {
guard let url = Bundle(for: MoyaProviderSpec.self).url(forResource: "testImage", withExtension: "png") else {
fatalError("Resource testImage.png could not be found in bundle")
}
let string = "some data"
guard let data = string.data(using: .utf8) else {
fatalError("Failed creating Data from String \(string)")
}
return [
MultipartFormData(provider: .file(url), name: "file", fileName: "testImage"),
MultipartFormData(provider: .data(data), name: "data")
]
}
}

// MARK: - String Helpers
extension String {
var urlEscaped: String {
Expand Down

0 comments on commit 6d57da7

Please sign in to comment.