Skip to content
This repository was archived by the owner on Apr 7, 2022. It is now read-only.

Commit 42acf0d

Browse files
committed
Adds port property to HTTPServer.
1 parent 3e49ea0 commit 42acf0d

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

Sources/HTTPKit/Server/HTTPServer.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,14 @@ public final class HTTPServer {
113113

114114
public let configuration: Configuration
115115
public let eventLoopGroup: EventLoopGroup
116-
116+
117+
// MARK: Properties
118+
119+
/// The port the `HTTPServer` is bound to.
120+
///
121+
/// Nil if `HTTPServer` is not bound to an IP port (eg. serving from a Unix socket or some other exotic channel).
122+
public var port: Int? { return channel?.localAddress?.port }
123+
117124
private var channel: Channel?
118125
private var quiesce: ServerQuiescingHelper?
119126

Tests/HTTPKitTests/HTTPServerTests.swift

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class HTTPServerTests: XCTestCase {
1515
let server = HTTPServer(
1616
configuration: .init(
1717
hostname: "localhost",
18-
port: 8080,
18+
port: 0,
1919
supportVersions: [.one],
2020
errorHandler: { error in
2121
XCTFail("\(error)")
@@ -24,14 +24,45 @@ class HTTPServerTests: XCTestCase {
2424
on: self.eventLoopGroup
2525
)
2626
try server.start(delegate: LargeResponder()).wait()
27+
defer {
28+
try! server.shutdown().wait()
29+
try! server.onClose.wait()
30+
}
2731

28-
var req = HTTPRequest(method: .GET, url: "http://localhost:8080/")
32+
var req = HTTPRequest(method: .GET, url: "http://localhost:\(server.port!)/")
2933
req.headers.replaceOrAdd(name: .connection, value: "close")
3034
let res = try HTTPClient(on: self.eventLoopGroup)
3135
.send(req).wait()
3236
XCTAssertEqual(res.body.count, 2_000_000)
33-
try server.shutdown().wait()
34-
try server.onClose.wait()
37+
}
38+
39+
func testServerPort() throws {
40+
struct Responder: HTTPServerDelegate {
41+
func respond(to request: HTTPRequest, on channel: Channel) -> EventLoopFuture<HTTPResponse> {
42+
let res = HTTPResponse(status: .ok, body: "OK")
43+
return channel.eventLoop.makeSucceededFuture(res)
44+
}
45+
}
46+
47+
let server = HTTPServer(
48+
configuration: .init(
49+
hostname: "localhost",
50+
port: 0,
51+
supportVersions: [.one],
52+
errorHandler: { error in
53+
XCTFail("\(error)")
54+
}
55+
),
56+
on: self.eventLoopGroup
57+
)
58+
try server.start(delegate: Responder()).wait()
59+
defer {
60+
try! server.shutdown().wait()
61+
try! server.onClose.wait()
62+
}
63+
64+
XCTAssertNotNil(server.port)
65+
XCTAssertGreaterThan(server.port!, 0)
3566
}
3667

3768
func testRFC1123Flip() throws {

Tests/HTTPKitTests/XCTestManifests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ extension HTTPServerTests {
5151
static let __allTests__HTTPServerTests = [
5252
("testLargeResponseClose", testLargeResponseClose),
5353
("testRFC1123Flip", testRFC1123Flip),
54+
("testServerPort", testServerPort),
5455
]
5556
}
5657

0 commit comments

Comments
 (0)