Skip to content

Commit

Permalink
fix warnings in TransportTests
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronsky committed Aug 31, 2024
1 parent 38f26e6 commit 78e6a9c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 43 deletions.
88 changes: 45 additions & 43 deletions Tests/AppStoreConnectTests/TransportTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,27 @@ import XCTest
import FoundationNetworking
#endif

private extension URLRequest {
init?(string urlString: String) {
guard let url = URL(string: urlString) else { return nil }

self.init(url: url)
}

static var testSendAsync: Self { .init(string: "https://example.com/test-send-async")! }
static var testSendAsyncError: Self { .init(string: "https://example.com/test-send-async-error")! }
static var testSendClosure: Self { .init(string: "https://example.com/test-send-closure")! }
static var testSendClosureError: Self { .init(string: "https://example.com/test-send-closure-error")! }
static var testDownloadAsync: Self { .init(string: "https://example.com/test-download-async")! }
static var testDownloadAsyncError: Self { .init(string: "https://example.com/test-download-async-error")! }
static var testDownloadClosure: Self { .init(string: "https://example.com/test-download-closure")! }
static var testDownloadClosureError: Self { .init(string: "https://example.com/test-download-closure-error")! }
static var testUploadAsync: Self { .init(string: "https://example.com/test-upload-async")! }
static var testUploadAsyncError: Self { .init(string: "https://example.com/test-upload-async-error")! }
static var testUploadClosure: Self { .init(string: "https://example.com/test-upload-closure")! }
static var testUploadClosureError: Self { .init(string: "https://example.com/test-upload-closure-error")! }
}

class TransportTests: XCTestCase {
private func createSession() -> URLSession {
let config = URLSessionConfiguration.ephemeral
Expand All @@ -20,25 +41,18 @@ class TransportTests: XCTestCase {
typealias ResponseMaker = @Sendable (URLRequest) throws -> Response<Data>

static let knownRequests: [URLRequest: ResponseMaker] = [
URLRequest(url: URL(string: "https://example.com/test-send-async")!): MockData.mockingSuccessNoContent(
for:),
URLRequest(url: URL(string: "https://example.com/test-send-async-error")!): MockData.mockingError(for:),
URLRequest(url: URL(string: "https://example.com/test-send-closure")!): MockData.mockingSuccessNoContent(
for:),
URLRequest(url: URL(string: "https://example.com/test-send-closure-error")!): MockData.mockingError(for:),
URLRequest(url: URL(string: "https://example.com/test-download-async")!): MockData.mockingSuccessNoContent(
for:),
URLRequest(url: URL(string: "https://example.com/test-download-async-error")!): MockData.mockingError(for:),
URLRequest(url: URL(string: "https://example.com/test-download-closure")!): MockData
.mockingSuccessNoContent(for:),
URLRequest(url: URL(string: "https://example.com/test-download-closure-error")!): MockData.mockingError(
for:),
URLRequest(url: URL(string: "https://example.com/test-upload-async")!): MockData.mockingSuccessNoContent(
for:),
URLRequest(url: URL(string: "https://example.com/test-upload-async-error")!): MockData.mockingError(for:),
URLRequest(url: URL(string: "https://example.com/test-upload-closure")!): MockData.mockingSuccessNoContent(
for:),
URLRequest(url: URL(string: "https://example.com/test-upload-closure-error")!): MockData.mockingError(for:),
.testSendAsync: MockData.mockingSuccessNoContent(for:),
.testSendAsyncError: MockData.mockingError(for:),
.testSendClosure: MockData.mockingSuccessNoContent(for:),
.testSendClosureError: MockData.mockingError(for:),
.testDownloadAsync: MockData.mockingSuccessNoContent(for:),
.testDownloadAsyncError: MockData.mockingError(for:),
.testDownloadClosure: MockData.mockingSuccessNoContent(for:),
.testDownloadClosureError: MockData.mockingError(for:),
.testUploadAsync: MockData.mockingSuccessNoContent(for:),
.testUploadAsyncError: MockData.mockingError(for:),
.testUploadClosure: MockData.mockingSuccessNoContent(for:),
.testUploadClosureError: MockData.mockingError(for:),
]

override class func canInit(with request: URLRequest) -> Bool {
Expand Down Expand Up @@ -66,115 +80,103 @@ class TransportTests: XCTestCase {
}

func testURLSessionSendRequest() async throws {
let request = URLRequest(url: URL(string: "https://example.com/test-send-async")!)
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .custom(decodeISO8601Date(with:))
_ = try await createSession()
.send(request: request, decoder: decoder)
.send(request: .testSendAsync, decoder: decoder)
}

func testURLSessionSendRequestFailure() async throws {
let request = URLRequest(url: URL(string: "https://example.com/test-send-async-error")!)
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .custom(decodeISO8601Date(with:))
try await XCTAssertThrowsError(
await createSession()
.send(request: request, decoder: decoder)
.send(request: .testSendAsyncError, decoder: decoder)
)
}

func testURLSessionSendRequestCompletion() {
let request = URLRequest(url: URL(string: "https://example.com/test-send-closure")!)
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .custom(decodeISO8601Date(with:))
let expectation = XCTestExpectation(description: "test-send-closure")
createSession()
.send(request: request, decoder: decoder) { result in
.send(request: .testSendClosure, decoder: decoder) { result in
XCTAssertNoThrow({ try result.get() })
expectation.fulfill()
}
}

func testURLSessionSendRequestCompletionFailure() {
let request = URLRequest(url: URL(string: "https://example.com/test-send-closure-error")!)
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .custom(decodeISO8601Date(with:))
let expectation = XCTestExpectation(description: "test-send-closure-error")
createSession()
.send(request: request, decoder: decoder) { result in
.send(request: .testSendClosureError, decoder: decoder) { result in
expectation.fulfill()
}
}

func testURLSessionDownloadRequest() async throws {
let request = URLRequest(url: URL(string: "https://example.com/test-download-async")!)
_ = try await createSession()
.download(request: request)
.download(request: .testDownloadAsync)
}

func testURLSessionDownloadRequestFailure() async throws {
let request = URLRequest(url: URL(string: "https://example.com/test-download-async-error")!)
try await XCTAssertThrowsError(
await createSession().download(request: request)
await createSession().download(request: .testDownloadAsyncError)
)
}

func testURLSessionDownloadRequestCompletion() {
let request = URLRequest(url: URL(string: "https://example.com/test-download-closure")!)
let expectation = XCTestExpectation(description: "test-download-closure")
createSession()
.download(request: request) { result in
.download(request: .testDownloadClosure) { result in
XCTAssertNoThrow({ try result.get() })
expectation.fulfill()
}
}

func testURLSessionDownloadRequestCompletionFailure() {
let request = URLRequest(url: URL(string: "https://example.com/test-download-closure-error")!)
let expectation = XCTestExpectation(description: "test-download-closure-error")
createSession()
.download(request: request) { result in
.download(request: .testDownloadClosureError) { result in
expectation.fulfill()
}
}

func testURLSessionUploadRequest() async throws {
let request = URLRequest(url: URL(string: "https://example.com/test-upload-async")!)
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .custom(decodeISO8601Date(with:))
_ = try await createSession()
.upload(request: request, data: Data(), decoder: decoder)
.upload(request: .testUploadAsync, data: Data(), decoder: decoder)
}

func testURLSessionUploadRequestFailure() async throws {
let request = URLRequest(url: URL(string: "https://example.com/test-upload-async-error")!)
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .custom(decodeISO8601Date(with:))
try await XCTAssertThrowsError(
await createSession()
.upload(request: request, data: Data(), decoder: decoder)
.upload(request: .testUploadAsyncError, data: Data(), decoder: decoder)
)
}

func testURLSessionUploadRequestCompletion() {
let request = URLRequest(url: URL(string: "https://example.com/test-upload-closure")!)
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .custom(decodeISO8601Date(with:))
let expectation = XCTestExpectation(description: "test-upload-closure")
createSession()
.upload(request: request, data: Data(), decoder: decoder) { result in
.upload(request: .testUploadClosure, data: Data(), decoder: decoder) { result in
XCTAssertNoThrow({ try result.get() })
expectation.fulfill()
}
}

func testURLSessionUploadRequestCompletionFailure() {
let request = URLRequest(url: URL(string: "https://example.com/test-upload-closure-error")!)
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .custom(decodeISO8601Date(with:))
let expectation = XCTestExpectation(description: "test-upload-closure-error")
createSession()
.upload(request: request, data: Data(), decoder: decoder) { result in
.upload(request: .testUploadClosureError, data: Data(), decoder: decoder) { result in
expectation.fulfill()
}
}
Expand Down
9 changes: 9 additions & 0 deletions Tests/Mocks/MockData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public enum MockData {
}

extension MockData {
@Sendable
public static func mockingSuccess<Content: Codable>(
with content: Content,
url: URL = URL()
Expand All @@ -86,26 +87,32 @@ extension MockData {
return .init(data: data, response: urlResponse(for: url, statusCode: 200), statusCode: 200, rate: nil)
}

@Sendable
public static func mockingSuccessNoContent(url: URL = URL()) -> Response<Data> {
return .init(data: nil, response: urlResponse(for: url, statusCode: 200), statusCode: 200, rate: nil)
}

@Sendable
public static func mockingSuccessNoContent(for request: URLRequest) -> Response<Data> {
return .init(data: nil, response: urlResponse(for: request.url!, statusCode: 200), statusCode: 200, rate: nil)
}

@Sendable
public static func mockingSuccessDownload(to fileURL: URL, for url: URL = URL()) -> Response<URL> {
return .init(fileURL: fileURL, response: urlResponse(for: url, statusCode: 200), statusCode: 200, rate: nil)
}

@Sendable
public static func mockingSuccessUpload(for url: URL = URL()) -> Response<Data> {
return .init(data: nil, response: urlResponse(for: url, statusCode: 200), statusCode: 200, rate: nil)
}

@Sendable
public static func mockingIncompatibleResponse(for url: URL = URL()) -> Response<Data> {
return .init(data: nil, response: urlResponse(for: url, statusCode: -128), statusCode: -128, rate: nil)
}

@Sendable
public static func mockingUnsuccessfulResponse(for url: URL = URL()) -> Response<Data> {
let json = """
{"errors":[{"code":"test","status":400,"title":"test","detail":"test"}]}
Expand All @@ -115,10 +122,12 @@ extension MockData {
return .init(data: json, response: urlResponse(for: url, statusCode: 400), statusCode: 400, rate: nil)
}

@Sendable
public static func mockingError(for request: URLRequest) throws -> Response<Data> {
throw URLError(.badURL)
}

@Sendable
public static func mockingError(_ error: any Error) throws -> Response<Data> {
throw error
}
Expand Down

0 comments on commit 78e6a9c

Please sign in to comment.