Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash when request download with fileURL #3318

Merged
merged 2 commits into from Oct 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions Source/URLRequest+Alamofire.swift
Expand Up @@ -32,6 +32,11 @@ public extension URLRequest {
}

func validate() throws {
if let url = url, url.isFileURL {
// This should become another urlRequestValidationFailed error in Alamofire 6.
throw AFError.invalidURL(url: url)
}

if method == .get, let bodyData = httpBody {
throw AFError.urlRequestValidationFailed(reason: .bodyDataInGETRequest(bodyData))
}
Expand Down
65 changes: 65 additions & 0 deletions Tests/RequestTests.swift
Expand Up @@ -1181,3 +1181,68 @@ final class RequestLifetimeTests: BaseTestCase {
XCTAssertNotNil(task)
}
}

// MARK: -

class RequestInvalidURLTestCase: BaseTestCase {
#if !SWIFT_PACKAGE
func testThatDataRequestWithFileURLThrowsError() {
// Given
let fileURL = url(forResource: "valid_data", withExtension: "json")
let expectation = self.expectation(description: "Request should fail with invalid URL error.")
var response: DataResponse<Data?, AFError>?

// When
AF.request(fileURL)
.response { resp in
response = resp
expectation.fulfill()
}

waitForExpectations(timeout: timeout)

// Then
XCTAssertEqual(response?.error?.isInvalidURLError, true)
}

func testThatDownloadRequestWithFileURLThrowsError() {
// Given
let fileURL = url(forResource: "valid_data", withExtension: "json")
let expectation = self.expectation(description: "Request should fail with invalid URL error.")
var response: DownloadResponse<URL?, AFError>?

// When
AF.download(fileURL)
.response { resp in
response = resp
expectation.fulfill()
}

waitForExpectations(timeout: timeout)

// Then
XCTAssertEqual(response?.error?.isInvalidURLError, true)
}

func testThatDataStreamRequestWithFileURLThrowsError() {
// Given
let fileURL = url(forResource: "valid_data", withExtension: "json")
let expectation = self.expectation(description: "Request should fail with invalid URL error.")
var response: DataStreamRequest.Completion?

// When
AF.streamRequest(fileURL)
.responseStream { stream in
guard case let .complete(completion) = stream.event else { return }

response = completion
expectation.fulfill()
}

waitForExpectations(timeout: timeout)

// Then
XCTAssertEqual(response?.error?.isInvalidURLError, true)
}
#endif
}