add swift transport support package

This commit is contained in:
2026-04-20 10:55:21 +00:00
commit 9e4c490a22
7 changed files with 438 additions and 0 deletions
@@ -0,0 +1,99 @@
import Foundation
import Testing
@testable import SwiftSupport
private final class URLProtocolStub: URLProtocol, @unchecked Sendable {
static var handler: (@Sendable (URLRequest) throws -> (HTTPURLResponse, Data))?
override class func canInit(with request: URLRequest) -> Bool {
true
}
override class func canonicalRequest(for request: URLRequest) -> URLRequest {
request
}
override func startLoading() {
guard let handler = Self.handler else {
return
}
do {
let (response, data) = try handler(request)
client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed)
client?.urlProtocol(self, didLoad: data)
client?.urlProtocolDidFinishLoading(self)
} catch {
client?.urlProtocol(self, didFailWithError: error)
}
}
override func stopLoading() {}
}
private struct EchoRequest: Codable {
let value: String
}
private struct EchoResponse: Codable, Equatable {
let echoed: String
}
@Test func typedRequestClientPostsToTypedRequestEndpoint() async throws {
let configuration = URLSessionConfiguration.ephemeral
configuration.protocolClasses = [URLProtocolStub.self]
let session = URLSession(configuration: configuration)
URLProtocolStub.handler = { request in
#expect(request.url?.absoluteString == "https://idp.global/typedrequest")
#expect(request.httpMethod == "POST")
let data = try #require(request.httpBody)
let body = try JSONSerialization.jsonObject(with: data) as? [String: Any]
#expect(body?["method"] as? String == "echo")
let responseBody = try JSONEncoder().encode([
"response": ["echoed": "ok"],
"correlation": ["id": "1", "phase": "response"]
])
let response = HTTPURLResponse(
url: try #require(request.url),
statusCode: 200,
httpVersion: nil,
headerFields: ["Content-Type": "application/json"]
)!
return (response, responseBody)
}
let client = TypedRequestClient(baseURL: URL(string: "https://idp.global")!, session: session)
let response = try await client.fire(method: "echo", request: EchoRequest(value: "hello"), responseType: EchoResponse.self)
#expect(response == EchoResponse(echoed: "ok"))
}
@Test func typedRequestClientThrowsServerErrors() async throws {
let configuration = URLSessionConfiguration.ephemeral
configuration.protocolClasses = [URLProtocolStub.self]
let session = URLSession(configuration: configuration)
URLProtocolStub.handler = { request in
let responseBody = try JSONEncoder().encode([
"error": ["text": "Nope"],
"correlation": ["id": "1", "phase": "response"]
])
let response = HTTPURLResponse(
url: try #require(request.url),
statusCode: 200,
httpVersion: nil,
headerFields: ["Content-Type": "application/json"]
)!
return (response, responseBody)
}
let client = TypedRequestClient(baseURL: URL(string: "https://idp.global")!, session: session)
await #expect(throws: TypedRequestError.self) {
_ = try await client.fire(method: "echo", request: EchoRequest(value: "hello"), responseType: EchoResponse.self)
}
}