Skip to content

Commit

Permalink
Add optional port to TestClientProtocol
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler committed Apr 24, 2024
1 parent f1e6d1c commit f216d1b
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Sources/HummingbirdTesting/ApplicationTester.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public protocol TestClientProtocol: Sendable {
headers: HTTPFields,
body: ByteBuffer?
) async throws -> TestResponse
// Port to connect to if test client is connecting to a live server
var port: Int? { get }
}

extension TestClientProtocol {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ final class AsyncHTTPClientTestFramework<App: ApplicationProtocol>: ApplicationT
struct Client: TestClientProtocol {
let client: HTTPClient
let urlPrefix: String
let port: Int?
let timeout: TimeAmount

/// Send request and call test callback on the response returned
Expand Down Expand Up @@ -75,7 +76,7 @@ final class AsyncHTTPClientTestFramework<App: ApplicationProtocol>: ApplicationT
eventLoopGroupProvider: .singleton,
configuration: .init(tlsConfiguration: tlsConfiguration)
)
let client = Client(client: httpClient, urlPrefix: "\(self.scheme)://localhost:\(port)", timeout: self.timeout)
let client = Client(client: httpClient, urlPrefix: "\(self.scheme)://localhost:\(port)", port: port, timeout: self.timeout)
do {
let value = try await test(client)
await serviceGroup.triggerGracefulShutdown()
Expand Down
2 changes: 2 additions & 0 deletions Sources/HummingbirdTesting/LiveTestFramework.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ final class LiveTestFramework<App: ApplicationProtocol>: ApplicationTestFramewor
let response = try await client.execute(request)
return .init(head: response.head, body: response.body ?? ByteBuffer(), trailerHeaders: response.trailerHeaders)
}

var port: Int? { self.client.port }
}

init(app: App) {
Expand Down
2 changes: 2 additions & 0 deletions Sources/HummingbirdTesting/RouterTestFramework.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ struct RouterTestFramework<Responder: HTTPResponder>: ApplicationTestFramework w
return try await group.next()!
}
}

var port: Int? { nil }
}

final class RouterResponseWriter: ResponseBodyWriter {
Expand Down

0 comments on commit f216d1b

Please sign in to comment.