From 42acf0d77b2c20bbad756db29c461c2434533c5e Mon Sep 17 00:00:00 2001 From: Omar Stefan Evans Date: Mon, 1 Jul 2019 11:46:08 -0400 Subject: [PATCH] Adds port property to HTTPServer. --- Sources/HTTPKit/Server/HTTPServer.swift | 9 +++++- Tests/HTTPKitTests/HTTPServerTests.swift | 39 +++++++++++++++++++++--- Tests/HTTPKitTests/XCTestManifests.swift | 1 + 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/Sources/HTTPKit/Server/HTTPServer.swift b/Sources/HTTPKit/Server/HTTPServer.swift index 2f4b1591..19233860 100644 --- a/Sources/HTTPKit/Server/HTTPServer.swift +++ b/Sources/HTTPKit/Server/HTTPServer.swift @@ -113,7 +113,14 @@ public final class HTTPServer { public let configuration: Configuration public let eventLoopGroup: EventLoopGroup - + + // MARK: Properties + + /// The port the `HTTPServer` is bound to. + /// + /// Nil if `HTTPServer` is not bound to an IP port (eg. serving from a Unix socket or some other exotic channel). + public var port: Int? { return channel?.localAddress?.port } + private var channel: Channel? private var quiesce: ServerQuiescingHelper? diff --git a/Tests/HTTPKitTests/HTTPServerTests.swift b/Tests/HTTPKitTests/HTTPServerTests.swift index 9aaa107a..89f9f58c 100644 --- a/Tests/HTTPKitTests/HTTPServerTests.swift +++ b/Tests/HTTPKitTests/HTTPServerTests.swift @@ -15,7 +15,7 @@ class HTTPServerTests: XCTestCase { let server = HTTPServer( configuration: .init( hostname: "localhost", - port: 8080, + port: 0, supportVersions: [.one], errorHandler: { error in XCTFail("\(error)") @@ -24,14 +24,45 @@ class HTTPServerTests: XCTestCase { on: self.eventLoopGroup ) try server.start(delegate: LargeResponder()).wait() + defer { + try! server.shutdown().wait() + try! server.onClose.wait() + } - var req = HTTPRequest(method: .GET, url: "http://localhost:8080/") + var req = HTTPRequest(method: .GET, url: "http://localhost:\(server.port!)/") req.headers.replaceOrAdd(name: .connection, value: "close") let res = try HTTPClient(on: self.eventLoopGroup) .send(req).wait() XCTAssertEqual(res.body.count, 2_000_000) - try server.shutdown().wait() - try server.onClose.wait() + } + + func testServerPort() throws { + struct Responder: HTTPServerDelegate { + func respond(to request: HTTPRequest, on channel: Channel) -> EventLoopFuture { + let res = HTTPResponse(status: .ok, body: "OK") + return channel.eventLoop.makeSucceededFuture(res) + } + } + + let server = HTTPServer( + configuration: .init( + hostname: "localhost", + port: 0, + supportVersions: [.one], + errorHandler: { error in + XCTFail("\(error)") + } + ), + on: self.eventLoopGroup + ) + try server.start(delegate: Responder()).wait() + defer { + try! server.shutdown().wait() + try! server.onClose.wait() + } + + XCTAssertNotNil(server.port) + XCTAssertGreaterThan(server.port!, 0) } func testRFC1123Flip() throws { diff --git a/Tests/HTTPKitTests/XCTestManifests.swift b/Tests/HTTPKitTests/XCTestManifests.swift index 7289ad0d..2cf5c156 100644 --- a/Tests/HTTPKitTests/XCTestManifests.swift +++ b/Tests/HTTPKitTests/XCTestManifests.swift @@ -51,6 +51,7 @@ extension HTTPServerTests { static let __allTests__HTTPServerTests = [ ("testLargeResponseClose", testLargeResponseClose), ("testRFC1123Flip", testRFC1123Flip), + ("testServerPort", testServerPort), ] }