generated from pixel-foundry/swift-package
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
142 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import Foundation | ||
#if canImport(FoundationNetworking) | ||
import FoundationNetworking | ||
#endif | ||
|
||
public protocol URLRequestHandler: Sendable { | ||
|
||
func data(for request: URLRequest) async throws -> (Data, URLResponse) | ||
|
||
} | ||
|
||
extension URLSession: URLRequestHandler {} | ||
|
||
#if canImport(FoundationNetworking) | ||
enum URLRequestHandlerError: Error { | ||
case noResponse | ||
} | ||
|
||
extension URLSession { | ||
|
||
public func data(for request: URLRequest) async throws -> (Data, URLResponse) { | ||
try await withCheckedThrowingContinuation { continuation in | ||
self.dataTask(with: request) { data, response, error in | ||
guard let data = data, let response = response else { | ||
continuation.resume(throwing: error ?? URLRequestHandlerError.noResponse) | ||
return | ||
} | ||
continuation.resume(returning: (data, response)) | ||
}.resume() | ||
} | ||
} | ||
|
||
} | ||
#endif |
77 changes: 77 additions & 0 deletions
77
Tests/EndpointBuilderURLSessionTests/EndpointBuilderURLSessionTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import EndpointBuilder | ||
@testable import EndpointBuilderURLSession | ||
import HTTPTypes | ||
import RoutingKit | ||
import XCTest | ||
#if canImport(FoundationNetworking) | ||
import FoundationNetworking | ||
#endif | ||
|
||
struct MockURLSession: URLRequestHandler { | ||
|
||
let requestHandler: @Sendable (URLRequest) -> Data | ||
|
||
func data(for request: URLRequest) async throws -> (Data, URLResponse) { | ||
let data = requestHandler(request) | ||
return ( | ||
data, | ||
URLResponse() | ||
) | ||
} | ||
|
||
} | ||
|
||
final class EndpointBuilderURLSessionTests: XCTestCase { | ||
|
||
@Endpoint | ||
struct EndpointWithNoResponse { | ||
static let path: [PathComponent] = ["blank"] | ||
static let httpMethod = HTTPRequest.Method.get | ||
} | ||
|
||
@Endpoint | ||
struct EndpointWithStringResponseAndPathComponent { | ||
static let path: [PathComponent] = ["echo", ":id"] | ||
static let httpMethod = HTTPRequest.Method.post | ||
static let responseType = String.self | ||
let body: String | ||
} | ||
|
||
struct APIClient: EndpointBuilderURLSession { | ||
|
||
let serverBaseURL = URL(string: "https://api.shipyard.studio")! | ||
let urlSession: @Sendable () -> URLRequestHandler | ||
|
||
init(mockSession: MockURLSession) { | ||
let session: @Sendable () -> URLRequestHandler = { mockSession } | ||
self.urlSession = session | ||
} | ||
|
||
} | ||
|
||
func testEndpointWithNoResponse() async throws { | ||
let endpoint = EndpointWithNoResponse() | ||
let client = APIClient(mockSession: MockURLSession(requestHandler: { urlRequest in | ||
XCTAssertEqual(urlRequest.url?.pathComponents, ["/", "blank"]) | ||
XCTAssertEqual(urlRequest.httpMethod, "GET") | ||
return Data() | ||
})) | ||
try await client.request(endpoint) | ||
} | ||
|
||
func testEndpointWithStringResponse() async throws { | ||
let endpoint = EndpointWithStringResponseAndPathComponent( | ||
body: "hello", | ||
pathParameters: EndpointWithStringResponseAndPathComponent.PathParameters(id: "my-ids") | ||
) | ||
let client = APIClient(mockSession: MockURLSession(requestHandler: { urlRequest in | ||
XCTAssertEqual(urlRequest.url?.pathComponents, ["/", "echo", "my-ids"]) | ||
XCTAssertEqual(urlRequest.httpMethod, "POST") | ||
XCTAssertEqual(urlRequest.httpBody, try? JSONEncoder().encode("hello")) | ||
return (try? JSONEncoder().encode("world")) ?? Data() | ||
})) | ||
let response = try await client.request(endpoint) | ||
XCTAssertEqual(response, "world") | ||
} | ||
|
||
} |