|
|
@@ -32,14 +32,19 @@ class SessionManagerTestCase: BaseTestCase {
|
|
|
|
|
|
private class HTTPMethodAdapter: RequestAdapter {
|
|
|
let method: HTTPMethod
|
|
|
+ let throwsError: Bool
|
|
|
|
|
|
- init(method: HTTPMethod) {
|
|
|
+ init(method: HTTPMethod, throwsError: Bool = false) {
|
|
|
self.method = method
|
|
|
+ self.throwsError = throwsError
|
|
|
}
|
|
|
|
|
|
- func adapt(_ urlRequest: URLRequest) -> URLRequest {
|
|
|
+ func adapt(_ urlRequest: URLRequest) throws -> URLRequest {
|
|
|
+ guard !throwsError else { throw AFError.invalidURLString(urlString: "") }
|
|
|
+
|
|
|
var urlRequest = urlRequest
|
|
|
urlRequest.httpMethod = method.rawValue
|
|
|
+
|
|
|
return urlRequest
|
|
|
}
|
|
|
}
|
|
|
@@ -48,8 +53,11 @@ class SessionManagerTestCase: BaseTestCase {
|
|
|
var adaptedCount = 0
|
|
|
var retryCount = 0
|
|
|
var shouldApplyAuthorizationHeader = false
|
|
|
+ var throwsErrorOnSecondAdapt = false
|
|
|
+
|
|
|
+ func adapt(_ urlRequest: URLRequest) throws -> URLRequest {
|
|
|
+ if throwsErrorOnSecondAdapt && adaptedCount == 1 { throw AFError.invalidURLString(urlString: "") }
|
|
|
|
|
|
- func adapt(_ urlRequest: URLRequest) -> URLRequest {
|
|
|
var urlRequest = urlRequest
|
|
|
|
|
|
adaptedCount += 1
|
|
|
@@ -287,6 +295,159 @@ class SessionManagerTestCase: BaseTestCase {
|
|
|
XCTAssertNil(manager, "manager should be nil")
|
|
|
}
|
|
|
|
|
|
+ // MARK: Tests - Bad Requests
|
|
|
+
|
|
|
+ func testThatDataRequestWithInvalidURLStringThrowsResponseHandlerError() {
|
|
|
+ // Given
|
|
|
+ let sessionManager = SessionManager()
|
|
|
+ let expectation = self.expectation(description: "Request should fail with error")
|
|
|
+
|
|
|
+ var response: DefaultDataResponse?
|
|
|
+
|
|
|
+ // When
|
|
|
+ sessionManager.request("https://httpbin.org/get/äëïöü").response { resp in
|
|
|
+ response = resp
|
|
|
+ expectation.fulfill()
|
|
|
+ }
|
|
|
+
|
|
|
+ waitForExpectations(timeout: timeout, handler: nil)
|
|
|
+
|
|
|
+ // Then
|
|
|
+ XCTAssertNil(response?.request)
|
|
|
+ XCTAssertNil(response?.response)
|
|
|
+ XCTAssertNotNil(response?.data)
|
|
|
+ XCTAssertEqual(response?.data?.count, 0)
|
|
|
+ XCTAssertNotNil(response?.error)
|
|
|
+
|
|
|
+ if let error = response?.error as? AFError {
|
|
|
+ XCTAssertTrue(error.isInvalidURLStringError)
|
|
|
+ XCTAssertEqual(error.urlString?.urlString, "https://httpbin.org/get/äëïöü")
|
|
|
+ } else {
|
|
|
+ XCTFail("error should not be nil")
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func testThatDownloadRequestWithInvalidURLStringThrowsResponseHandlerError() {
|
|
|
+ // Given
|
|
|
+ let sessionManager = SessionManager()
|
|
|
+ let expectation = self.expectation(description: "Download should fail with error")
|
|
|
+
|
|
|
+ var response: DefaultDownloadResponse?
|
|
|
+
|
|
|
+ // When
|
|
|
+ sessionManager.download("https://httpbin.org/get/äëïöü").response { resp in
|
|
|
+ response = resp
|
|
|
+ expectation.fulfill()
|
|
|
+ }
|
|
|
+
|
|
|
+ waitForExpectations(timeout: timeout, handler: nil)
|
|
|
+
|
|
|
+ // Then
|
|
|
+ XCTAssertNil(response?.request)
|
|
|
+ XCTAssertNil(response?.response)
|
|
|
+ XCTAssertNil(response?.temporaryURL)
|
|
|
+ XCTAssertNil(response?.destinationURL)
|
|
|
+ XCTAssertNil(response?.resumeData)
|
|
|
+ XCTAssertNotNil(response?.error)
|
|
|
+
|
|
|
+ if let error = response?.error as? AFError {
|
|
|
+ XCTAssertTrue(error.isInvalidURLStringError)
|
|
|
+ XCTAssertEqual(error.urlString?.urlString, "https://httpbin.org/get/äëïöü")
|
|
|
+ } else {
|
|
|
+ XCTFail("error should not be nil")
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func testThatUploadDataRequestWithInvalidURLStringThrowsResponseHandlerError() {
|
|
|
+ // Given
|
|
|
+ let sessionManager = SessionManager()
|
|
|
+ let expectation = self.expectation(description: "Upload should fail with error")
|
|
|
+
|
|
|
+ var response: DefaultDataResponse?
|
|
|
+
|
|
|
+ // When
|
|
|
+ sessionManager.upload(Data(), to: "https://httpbin.org/get/äëïöü").response { resp in
|
|
|
+ response = resp
|
|
|
+ expectation.fulfill()
|
|
|
+ }
|
|
|
+
|
|
|
+ waitForExpectations(timeout: timeout, handler: nil)
|
|
|
+
|
|
|
+ // Then
|
|
|
+ XCTAssertNil(response?.request)
|
|
|
+ XCTAssertNil(response?.response)
|
|
|
+ XCTAssertNotNil(response?.data)
|
|
|
+ XCTAssertEqual(response?.data?.count, 0)
|
|
|
+ XCTAssertNotNil(response?.error)
|
|
|
+
|
|
|
+ if let error = response?.error as? AFError {
|
|
|
+ XCTAssertTrue(error.isInvalidURLStringError)
|
|
|
+ XCTAssertEqual(error.urlString?.urlString, "https://httpbin.org/get/äëïöü")
|
|
|
+ } else {
|
|
|
+ XCTFail("error should not be nil")
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func testThatUploadFileRequestWithInvalidURLStringThrowsResponseHandlerError() {
|
|
|
+ // Given
|
|
|
+ let sessionManager = SessionManager()
|
|
|
+ let expectation = self.expectation(description: "Upload should fail with error")
|
|
|
+
|
|
|
+ var response: DefaultDataResponse?
|
|
|
+
|
|
|
+ // When
|
|
|
+ sessionManager.upload(URL(fileURLWithPath: "/invalid"), to: "https://httpbin.org/get/äëïöü").response { resp in
|
|
|
+ response = resp
|
|
|
+ expectation.fulfill()
|
|
|
+ }
|
|
|
+
|
|
|
+ waitForExpectations(timeout: timeout, handler: nil)
|
|
|
+
|
|
|
+ // Then
|
|
|
+ XCTAssertNil(response?.request)
|
|
|
+ XCTAssertNil(response?.response)
|
|
|
+ XCTAssertNotNil(response?.data)
|
|
|
+ XCTAssertEqual(response?.data?.count, 0)
|
|
|
+ XCTAssertNotNil(response?.error)
|
|
|
+
|
|
|
+ if let error = response?.error as? AFError {
|
|
|
+ XCTAssertTrue(error.isInvalidURLStringError)
|
|
|
+ XCTAssertEqual(error.urlString?.urlString, "https://httpbin.org/get/äëïöü")
|
|
|
+ } else {
|
|
|
+ XCTFail("error should not be nil")
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func testThatUploadStreamRequestWithInvalidURLStringThrowsResponseHandlerError() {
|
|
|
+ // Given
|
|
|
+ let sessionManager = SessionManager()
|
|
|
+ let expectation = self.expectation(description: "Upload should fail with error")
|
|
|
+
|
|
|
+ var response: DefaultDataResponse?
|
|
|
+
|
|
|
+ // When
|
|
|
+ sessionManager.upload(InputStream(data: Data()), to: "https://httpbin.org/get/äëïöü").response { resp in
|
|
|
+ response = resp
|
|
|
+ expectation.fulfill()
|
|
|
+ }
|
|
|
+
|
|
|
+ waitForExpectations(timeout: timeout, handler: nil)
|
|
|
+
|
|
|
+ // Then
|
|
|
+ XCTAssertNil(response?.request)
|
|
|
+ XCTAssertNil(response?.response)
|
|
|
+ XCTAssertNotNil(response?.data)
|
|
|
+ XCTAssertEqual(response?.data?.count, 0)
|
|
|
+ XCTAssertNotNil(response?.error)
|
|
|
+
|
|
|
+ if let error = response?.error as? AFError {
|
|
|
+ XCTAssertTrue(error.isInvalidURLStringError)
|
|
|
+ XCTAssertEqual(error.urlString?.urlString, "https://httpbin.org/get/äëïöü")
|
|
|
+ } else {
|
|
|
+ XCTFail("error should not be nil")
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
// MARK: Tests - Request Adapter
|
|
|
|
|
|
func testThatSessionManagerCallsRequestAdapterWhenCreatingDataRequest() {
|
|
|
@@ -367,6 +528,26 @@ class SessionManagerTestCase: BaseTestCase {
|
|
|
XCTAssertEqual(request.task?.originalRequest?.httpMethod, adapter.method.rawValue)
|
|
|
}
|
|
|
|
|
|
+ func testThatRequestAdapterErrorThrowsResponseHandlerError() {
|
|
|
+ // Given
|
|
|
+ let adapter = HTTPMethodAdapter(method: .post, throwsError: true)
|
|
|
+
|
|
|
+ let sessionManager = SessionManager()
|
|
|
+ sessionManager.adapter = adapter
|
|
|
+ sessionManager.startRequestsImmediately = false
|
|
|
+
|
|
|
+ // When
|
|
|
+ let request = sessionManager.request("https://httpbin.org/get")
|
|
|
+
|
|
|
+ // Then
|
|
|
+ if let error = request.delegate.error as? AFError {
|
|
|
+ XCTAssertTrue(error.isInvalidURLStringError)
|
|
|
+ XCTAssertEqual(error.urlString?.urlString, "")
|
|
|
+ } else {
|
|
|
+ XCTFail("error should not be nil")
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
// MARK: Tests - Request Retrier
|
|
|
|
|
|
func testThatSessionManagerCallsRequestRetrierWhenRequestEncountersError() {
|
|
|
@@ -423,6 +604,41 @@ class SessionManagerTestCase: BaseTestCase {
|
|
|
XCTAssertEqual(handler.retryCount, 1)
|
|
|
XCTAssertEqual(response?.result.isSuccess, true)
|
|
|
}
|
|
|
+
|
|
|
+ func testThatRequestAdapterErrorThrowsResponseHandlerErrorWhenRequestIsRetried() {
|
|
|
+ // Given
|
|
|
+ let handler = RequestHandler()
|
|
|
+ handler.throwsErrorOnSecondAdapt = true
|
|
|
+
|
|
|
+ let sessionManager = SessionManager()
|
|
|
+ sessionManager.adapter = handler
|
|
|
+ sessionManager.retrier = handler
|
|
|
+
|
|
|
+ let expectation = self.expectation(description: "request should eventually fail")
|
|
|
+ var response: DataResponse<Any>?
|
|
|
+
|
|
|
+ // When
|
|
|
+ sessionManager.request("https://httpbin.org/basic-auth/user/password")
|
|
|
+ .validate()
|
|
|
+ .responseJSON { jsonResponse in
|
|
|
+ response = jsonResponse
|
|
|
+ expectation.fulfill()
|
|
|
+ }
|
|
|
+
|
|
|
+ waitForExpectations(timeout: timeout, handler: nil)
|
|
|
+
|
|
|
+ // Then
|
|
|
+ XCTAssertEqual(handler.adaptedCount, 1)
|
|
|
+ XCTAssertEqual(handler.retryCount, 1)
|
|
|
+ XCTAssertEqual(response?.result.isSuccess, false)
|
|
|
+
|
|
|
+ if let error = response?.result.error as? AFError {
|
|
|
+ XCTAssertTrue(error.isInvalidURLStringError)
|
|
|
+ XCTAssertEqual(error.urlString?.urlString, "")
|
|
|
+ } else {
|
|
|
+ XCTFail("error should not be nil")
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// MARK: -
|