|
14 | 14 | import OpenAPIRuntime |
15 | 15 | import HTTPTypes |
16 | 16 |
|
| 17 | +/// A test implementation of the `ServerTransport` protocol for simulating server-side API handling. |
| 18 | +/// |
| 19 | +/// The `TestServerTransport` class allows you to define custom operations and handlers that |
| 20 | +/// simulate server-side API handling. This is useful for testing and verifying the behavior of |
| 21 | +/// your server-related code without the need for actual network interactions. |
| 22 | +/// |
| 23 | +/// Example usage: |
| 24 | +/// ```swift |
| 25 | +/// let testTransport = TestServerTransport() |
| 26 | +/// try testTransport.register { request, metadata in |
| 27 | +/// // Simulate server response logic here |
| 28 | +/// return Response(...) |
| 29 | +/// } |
| 30 | +/// |
| 31 | +/// let server = MyServer(transport: testTransport) |
| 32 | +/// ``` |
17 | 33 | public final class TestServerTransport: ServerTransport { |
18 | | - |
| 34 | + /// Represents the input parameters for an API operation. |
19 | 35 | public struct OperationInputs: Equatable { |
| 36 | + /// The HTTP method of the operation. |
20 | 37 | public var method: HTTPRequest.Method |
| 38 | + /// The path components of the operation's route. |
21 | 39 | public var path: String |
22 | 40 |
|
| 41 | + /// Initializes a new instance of `OperationInputs`. |
| 42 | + /// |
| 43 | + /// - Parameters: |
| 44 | + /// - method: The HTTP method of the operation. |
| 45 | + /// - path: The path components of the operation's route. |
23 | 46 | public init(method: HTTPRequest.Method, path: String) { |
24 | 47 | self.method = method |
25 | 48 | self.path = path |
26 | 49 | } |
27 | 50 | } |
28 | 51 |
|
| 52 | + /// A typealias representing a handler closure for processing server requests. |
29 | 53 | public typealias Handler = @Sendable (HTTPRequest, HTTPBody?, ServerRequestMetadata) async throws -> ( |
30 | 54 | HTTPResponse, HTTPBody? |
31 | 55 | ) |
32 | 56 |
|
| 57 | + /// Represents an operation with its inputs and associated handler. |
33 | 58 | public struct Operation { |
| 59 | + /// The input parameters for the API operation. |
34 | 60 | public var inputs: OperationInputs |
| 61 | + /// The closure representing the server operation logic. |
35 | 62 | public var closure: Handler |
36 | 63 |
|
| 64 | + /// Initializes a new instance of `Operation`. |
| 65 | + /// |
| 66 | + /// - Parameters: |
| 67 | + /// - inputs: The input parameters for the API operation. |
| 68 | + /// - closure: The closure representing the server operation logic |
37 | 69 | public init(inputs: OperationInputs, closure: @escaping Handler) { |
38 | 70 | self.inputs = inputs |
39 | 71 | self.closure = closure |
40 | 72 | } |
41 | 73 | } |
42 | 74 |
|
| 75 | + /// Initializes a new instance of `TestServerTransport`. |
43 | 76 | public init() {} |
| 77 | + |
| 78 | + /// The array of registered operations. |
44 | 79 | public private(set) var registered: [Operation] = [] |
45 | 80 |
|
| 81 | + /// Registers a new API operation handler with specific parameters. |
| 82 | + /// |
| 83 | + /// - Parameters: |
| 84 | + /// - handler: The closure representing the server operation logic. |
| 85 | + /// - method: The HTTP method of the operation. |
| 86 | + /// - path: The path components of the operation. |
| 87 | + /// - Throws: An error if there's an issue registering the operation. |
46 | 88 | public func register( |
47 | 89 | _ handler: @Sendable @escaping (HTTPRequest, HTTPBody?, ServerRequestMetadata) async throws -> ( |
48 | 90 | HTTPResponse, HTTPBody? |
|
0 commit comments