import Alamofire
import Foundation
import XCTest
final class ResponseTestCase: BaseTestCase {
@MainActor
func testThatResponseReturnsSuccessResultWithValidData() {
let session = stored(Session())
let expectation = expectation(description: "request should succeed")
var response: DataResponse<Data?, AFError>?
session.request(.default, parameters: ["foo": "bar"]).response { resp in
response = resp
expectation.fulfill()
}
waitForExpectations(timeout: timeout)
XCTAssertNotNil(response?.request)
XCTAssertNotNil(response?.response)
XCTAssertNotNil(response?.data)
XCTAssertNil(response?.error)
XCTAssertNotNil(response?.metrics)
}
@MainActor
func testThatResponseReturnsFailureResultWithOptionalDataAndError() {
let session = stored(Session())
let urlString = String.invalidURL
let expectation = expectation(description: "request should fail with invalid URL error")
var response: DataResponse<Data?, AFError>?
session.request(urlString, parameters: ["foo": "bar"]).response { resp in
response = resp
expectation.fulfill()
}
waitForExpectations(timeout: timeout)
XCTAssertNotNil(response?.request)
XCTAssertNil(response?.response)
XCTAssertNil(response?.data)
XCTAssertNotNil(response?.error)
XCTAssertEqual(response?.error?.isSessionTaskError, true)
XCTAssertNotNil(response?.metrics)
}
}
final class ResponseDataTestCase: BaseTestCase {
@MainActor
func testThatResponseDataReturnsSuccessResultWithValidData() {
let session = stored(Session())
let expectation = expectation(description: "request should succeed")
var response: DataResponse<Data, AFError>?
session.request(.default, parameters: ["foo": "bar"]).responseData { resp in
response = resp
expectation.fulfill()
}
waitForExpectations(timeout: timeout)
XCTAssertNotNil(response?.request)
XCTAssertNotNil(response?.response)
XCTAssertNotNil(response?.data)
XCTAssertNotNil(response?.data)
XCTAssertEqual(response?.result.isSuccess, true)
XCTAssertNotNil(response?.metrics)
}
@MainActor
func testThatResponseDataReturnsFailureResultWithOptionalDataAndError() {
let session = stored(Session())
let urlString = String.invalidURL
let expectation = expectation(description: "request should fail with invalid URL error")
var response: DataResponse<Data, AFError>?
session.request(urlString, parameters: ["foo": "bar"]).responseData { resp in
response = resp
expectation.fulfill()
}
waitForExpectations(timeout: timeout)
XCTAssertNotNil(response?.request)
XCTAssertNil(response?.response)
XCTAssertNil(response?.data)
XCTAssertEqual(response?.result.isFailure, true)
XCTAssertEqual(response?.error?.isSessionTaskError, true)
XCTAssertNotNil(response?.metrics)
}
}
final class ResponseStringTestCase: BaseTestCase {
@MainActor
func testThatResponseStringReturnsSuccessResultWithValidString() {
let session = stored(Session())
let expectation = expectation(description: "request should succeed")
var response: DataResponse<String, AFError>?
session.request(.default, parameters: ["foo": "bar"]).responseString { resp in
response = resp
expectation.fulfill()
}
waitForExpectations(timeout: timeout)
XCTAssertNotNil(response?.request)
XCTAssertNotNil(response?.response)
XCTAssertNotNil(response?.data)
XCTAssertNotNil(response?.data)
XCTAssertEqual(response?.result.isSuccess, true)
XCTAssertNotNil(response?.metrics)
}
@MainActor
func testThatResponseStringReturnsFailureResultWithOptionalDataAndError() {
let session = stored(Session())
let urlString = String.invalidURL
let expectation = expectation(description: "request should fail with invalid URL error")
var response: DataResponse<String, AFError>?
session.request(urlString, parameters: ["foo": "bar"]).responseString { resp in
response = resp
expectation.fulfill()
}
waitForExpectations(timeout: timeout)
XCTAssertNotNil(response?.request)
XCTAssertNil(response?.response)
XCTAssertNil(response?.data)
XCTAssertEqual(response?.result.isFailure, true)
XCTAssertEqual(response?.error?.isSessionTaskError, true)
XCTAssertNotNil(response?.metrics)
}
}
@available(*, deprecated)
final class ResponseJSONTestCase: BaseTestCase {
@MainActor
func testThatResponseJSONReturnsSuccessResultWithValidJSON() {
let session = stored(Session())
let expectation = expectation(description: "request should succeed")
var response: DataResponse<Any, AFError>?
session.request(.default, parameters: ["foo": "bar"]).responseJSON { resp in
response = resp
expectation.fulfill()
}
waitForExpectations(timeout: timeout)
XCTAssertNotNil(response?.request)
XCTAssertNotNil(response?.response)
XCTAssertNotNil(response?.data)
XCTAssertNotNil(response?.data)
XCTAssertEqual(response?.result.isSuccess, true)
XCTAssertNotNil(response?.metrics)
}
@MainActor
func testThatResponseStringReturnsFailureResultWithOptionalDataAndError() {
let session = stored(Session())
let urlString = String.invalidURL
let expectation = expectation(description: "request should fail")
var response: DataResponse<Any, AFError>?
session.request(urlString, parameters: ["foo": "bar"]).responseJSON { resp in
response = resp
expectation.fulfill()
}
waitForExpectations(timeout: timeout)
XCTAssertNotNil(response?.request)
XCTAssertNil(response?.response)
XCTAssertNil(response?.data)
XCTAssertEqual(response?.result.isFailure, true)
XCTAssertEqual(response?.error?.isSessionTaskError, true)
XCTAssertNotNil(response?.metrics)
}
@MainActor
func testThatResponseJSONReturnsSuccessResultForGETRequest() {
let session = stored(Session())
let expectation = expectation(description: "request should succeed")
var response: DataResponse<Any, AFError>?
session.request(.default, parameters: ["foo": "bar"]).responseJSON { resp in
response = resp
expectation.fulfill()
}
waitForExpectations(timeout: timeout)
XCTAssertNotNil(response?.request)
XCTAssertNotNil(response?.response)
XCTAssertNotNil(response?.data)
XCTAssertNotNil(response?.data)
XCTAssertEqual(response?.result.isSuccess, true)
XCTAssertNotNil(response?.metrics)
if
let responseDictionary = response?.result.success as? [String: Any],
let args = responseDictionary["args"] as? [String: String] {
XCTAssertEqual(args, ["foo": "bar"], "args should match parameters")
} else {
XCTFail("args should not be nil")
}
}
@MainActor
func testThatResponseJSONReturnsSuccessResultForPOSTRequest() {
let session = stored(Session())
let expectation = expectation(description: "request should succeed")
var response: DataResponse<Any, AFError>?
session.request(.method(.post), parameters: ["foo": "bar"]).responseJSON { resp in
response = resp
expectation.fulfill()
}
waitForExpectations(timeout: timeout)
XCTAssertNotNil(response?.request)
XCTAssertNotNil(response?.response)
XCTAssertNotNil(response?.data)
XCTAssertNotNil(response?.data)
XCTAssertEqual(response?.result.isSuccess, true)
XCTAssertNotNil(response?.metrics)
if
let responseDictionary = response?.result.success as? [String: Any],
let form = responseDictionary["form"] as? [String: String] {
XCTAssertEqual(form, ["foo": "bar"], "form should match parameters")
} else {
XCTFail("form should not be nil")
}
}
}
final class ResponseJSONDecodableTestCase: BaseTestCase {
@MainActor
func testThatResponseDecodableReturnsSuccessResultWithValidJSON() {
let session = stored(Session())
let url = Endpoint().url
let expectation = expectation(description: "request should succeed")
var response: DataResponse<TestResponse, AFError>?
session.request(url, parameters: [:]).responseDecodable(of: TestResponse.self) { resp in
response = resp
expectation.fulfill()
}
waitForExpectations(timeout: timeout)
XCTAssertNotNil(response?.request)
XCTAssertNotNil(response?.response)
XCTAssertNotNil(response?.data)
XCTAssertEqual(response?.result.isSuccess, true)
XCTAssertEqual(response?.result.success?.url, url.absoluteString)
XCTAssertNotNil(response?.metrics)
}
@MainActor
func testThatResponseDecodableWithPassedTypeReturnsSuccessResultWithValidJSON() {
let session = stored(Session())
let url = Endpoint().url
let expectation = expectation(description: "request should succeed")
var response: DataResponse<TestResponse, AFError>?
session.request(url, parameters: [:]).responseDecodable(of: TestResponse.self) {
response = $0
expectation.fulfill()
}
waitForExpectations(timeout: timeout)
XCTAssertNotNil(response?.request)
XCTAssertNotNil(response?.response)
XCTAssertNotNil(response?.data)
XCTAssertEqual(response?.result.isSuccess, true)
XCTAssertEqual(response?.result.success?.url, url.absoluteString)
XCTAssertNotNil(response?.metrics)
}
@MainActor
func testThatResponseStringReturnsFailureResultWithOptionalDataAndError() {
let session = stored(Session())
let urlString = String.invalidURL
let expectation = expectation(description: "request should fail")
var response: DataResponse<TestResponse, AFError>?
session.request(urlString, parameters: [:]).responseDecodable(of: TestResponse.self) { resp in
response = resp
expectation.fulfill()
}
waitForExpectations(timeout: timeout)
XCTAssertNotNil(response?.request)
XCTAssertNil(response?.response)
XCTAssertNil(response?.data)
XCTAssertEqual(response?.result.isFailure, true)
XCTAssertEqual(response?.error?.isSessionTaskError, true)
XCTAssertNotNil(response?.metrics)
}
}
final class ResponseMapTestCase: BaseTestCase {
@MainActor
func testThatMapTransformsSuccessValue() {
let session = stored(Session())
let expectation = expectation(description: "request should succeed")
var response: DataResponse<String, AFError>?
session.request(.default, parameters: ["foo": "bar"]).responseDecodable(of: TestResponse.self) { resp in
response = resp.map { response in
response.args["foo"] ?? "invalid"
}
expectation.fulfill()
}
waitForExpectations(timeout: timeout)
XCTAssertNotNil(response?.request)
XCTAssertNotNil(response?.response)
XCTAssertNotNil(response?.data)
XCTAssertEqual(response?.result.isSuccess, true)
XCTAssertEqual(response?.result.success, "bar")
XCTAssertNotNil(response?.metrics)
}
@MainActor
func testThatMapPreservesFailureError() {
let session = stored(Session())
let urlString = String.invalidURL
let expectation = expectation(description: "request should fail with invalid URL error")
var response: DataResponse<String, AFError>?
session.request(urlString, parameters: ["foo": "bar"]).responseData { resp in
response = resp.map { _ in "ignored" }
expectation.fulfill()
}
waitForExpectations(timeout: timeout)
XCTAssertNotNil(response?.request)
XCTAssertNil(response?.response)
XCTAssertNil(response?.data)
XCTAssertEqual(response?.result.isFailure, true)
XCTAssertEqual(response?.error?.isSessionTaskError, true)
XCTAssertNotNil(response?.metrics)
}
}
final class ResponseTryMapTestCase: BaseTestCase {
@MainActor
func testThatTryMapTransformsSuccessValue() {
let session = stored(Session())
let expectation = expectation(description: "request should succeed")
var response: DataResponse<String, any Error>?
session.request(.default, parameters: ["foo": "bar"]).responseDecodable(of: TestResponse.self) { resp in
response = resp.tryMap { response in
response.args["foo"] ?? "invalid"
}
expectation.fulfill()
}
waitForExpectations(timeout: timeout)
XCTAssertNotNil(response?.request)
XCTAssertNotNil(response?.response)
XCTAssertNotNil(response?.data)
XCTAssertEqual(response?.result.isSuccess, true)
XCTAssertEqual(response?.result.success, "bar")
XCTAssertNotNil(response?.metrics)
}
@MainActor
func testThatTryMapCatchesTransformationError() {
let session = stored(Session())
struct TransformError: Error {}
let expectation = expectation(description: "request should succeed")
var response: DataResponse<String, any Error>?
session.request(.default, parameters: ["foo": "bar"]).responseData { resp in
response = resp.tryMap { _ in
throw TransformError()
}
expectation.fulfill()
}
waitForExpectations(timeout: timeout)
XCTAssertNotNil(response?.request)
XCTAssertNotNil(response?.response)
XCTAssertNotNil(response?.data)
XCTAssertEqual(response?.result.isFailure, true)
if let error = response?.result.failure {
XCTAssertTrue(error is TransformError)
} else {
XCTFail("tryMap should catch the transformation error")
}
XCTAssertNotNil(response?.metrics)
}
@MainActor
func testThatTryMapPreservesFailureError() {
let session = stored(Session())
let urlString = String.invalidURL
let expectation = expectation(description: "request should fail with invalid URL error")
var response: DataResponse<String, any Error>?
session.request(urlString, parameters: ["foo": "bar"]).responseData { resp in
response = resp.tryMap { _ in "ignored" }
expectation.fulfill()
}
waitForExpectations(timeout: timeout)
XCTAssertNotNil(response?.request)
XCTAssertNil(response?.response)
XCTAssertNil(response?.data)
XCTAssertEqual(response?.result.isFailure, true)
XCTAssertEqual(response?.error?.asAFError?.isSessionTaskError, true)
XCTAssertNotNil(response?.metrics)
}
}
enum TestError: Error {
case error(error: AFError)
}
enum TransformationError: Error {
case error
func alwaysFails() throws -> TestError {
throw TransformationError.error
}
}
final class ResponseMapErrorTestCase: BaseTestCase {
@MainActor
func testThatMapErrorTransformsFailureValue() {
let session = stored(Session())
let urlString = String.invalidURL
let expectation = expectation(description: "request should not succeed")
var response: DataResponse<TestResponse, TestError>?
session.request(urlString).responseDecodable(of: TestResponse.self) { resp in
response = resp.mapError { error in
TestError.error(error: error)
}
expectation.fulfill()
}
waitForExpectations(timeout: timeout)
XCTAssertNotNil(response?.request)
XCTAssertNil(response?.response)
XCTAssertNil(response?.data)
XCTAssertEqual(response?.result.isFailure, true)
guard let error = response?.error, case .error = error else { XCTFail(); return }
XCTAssertNotNil(response?.metrics)
}
@MainActor
func testThatMapErrorPreservesSuccessValue() {
let session = stored(Session())
let expectation = expectation(description: "request should succeed")
var response: DataResponse<Data, TestError>?
session.request(.default).responseData { resp in
response = resp.mapError { TestError.error(error: $0) }
expectation.fulfill()
}
waitForExpectations(timeout: timeout)
XCTAssertNotNil(response?.request)
XCTAssertNotNil(response?.response)
XCTAssertNotNil(response?.data)
XCTAssertEqual(response?.result.isSuccess, true)
XCTAssertNotNil(response?.metrics)
}
}
final class ResponseTryMapErrorTestCase: BaseTestCase {
@MainActor
func testThatTryMapErrorPreservesSuccessValue() {
let session = stored(Session())
let expectation = expectation(description: "request should succeed")
var response: DataResponse<Data, any Error>?
session.request(.default).responseData { resp in
response = resp.tryMapError { TestError.error(error: $0) }
expectation.fulfill()
}
waitForExpectations(timeout: timeout)
XCTAssertNotNil(response?.request)
XCTAssertNotNil(response?.response)
XCTAssertNotNil(response?.data)
XCTAssertEqual(response?.result.isSuccess, true)
XCTAssertNotNil(response?.metrics)
}
@MainActor
func testThatTryMapErrorCatchesTransformationError() {
let session = stored(Session())
let urlString = String.invalidURL
let expectation = expectation(description: "request should fail")
var response: DataResponse<Data, any Error>?
session.request(urlString).responseData { resp in
response = resp.tryMapError { _ in try TransformationError.error.alwaysFails() }
expectation.fulfill()
}
waitForExpectations(timeout: timeout)
XCTAssertNotNil(response?.request)
XCTAssertNil(response?.response)
XCTAssertNil(response?.data)
XCTAssertEqual(response?.result.isFailure, true)
if let error = response?.result.failure {
XCTAssertTrue(error is TransformationError)
} else {
XCTFail("tryMapError should catch the transformation error")
}
XCTAssertNotNil(response?.metrics)
}
@MainActor
func testThatTryMapErrorTransformsError() {
let session = stored(Session())
let urlString = String.invalidURL
let expectation = expectation(description: "request should fail")
var response: DataResponse<Data, any Error>?
session.request(urlString).responseData { resp in
response = resp.tryMapError { TestError.error(error: $0) }
expectation.fulfill()
}
waitForExpectations(timeout: timeout)
XCTAssertNotNil(response?.request)
XCTAssertNil(response?.response)
XCTAssertNil(response?.data)
XCTAssertEqual(response?.result.isFailure, true)
XCTAssertNotNil(response?.metrics)
}
}