Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash related to setting nil hostname and port #2479

Merged
merged 1 commit into from Aug 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions Sources/Vapor/HTTP/Server/HTTPServer.swift
Expand Up @@ -20,7 +20,7 @@ public final class HTTPServer: Server {
public static let defaultHostname = "127.0.0.1"
public static let defaultPort = 8080

/// Address the server will bind to.
/// Address the server will bind to. Configuring an address using a hostname with a nil host or port will use the default hostname or port respectively.
public var address: BindAddress

/// Host name the server will bind to.
Expand Down Expand Up @@ -255,7 +255,7 @@ public final class HTTPServer: Server {
let addressDescription: String
switch configuration.address {
case .hostname(let hostname, let port):
addressDescription = "\(scheme)://\(hostname!):\(port!)" // will never be nil because we set them above to non-optional values
addressDescription = "\(scheme)://\(hostname ?? configuration.hostname):\(port ?? configuration.port)"
case .unixDomainSocket(let socketPath):
addressDescription = "\(scheme)+unix: \(socketPath)"
}
Expand Down
24 changes: 24 additions & 0 deletions Tests/VaporTests/ServerTests.swift
Expand Up @@ -543,6 +543,30 @@ final class ServerTests: XCTestCase {

XCTAssertThrowsError(try app.start())
}

func testStartWithDefaultHostnameConfiguration() throws {
let app = Application(.testing)
app.http.server.configuration.address = .hostname(nil, port: nil)
defer { app.shutdown() }

XCTAssertNoThrow(try app.start())
}

func testStartWithDefaultHostname() throws {
let app = Application(.testing)
app.http.server.configuration.address = .hostname(nil, port: 8008)
defer { app.shutdown() }

XCTAssertNoThrow(try app.start())
}

func testStartWithDefaultPort() throws {
let app = Application(.testing)
app.http.server.configuration.address = .hostname("0.0.0.0", port: nil)
defer { app.shutdown() }

XCTAssertNoThrow(try app.start())
}

func testAddressConfigurations() throws {
var configuration = HTTPServer.Configuration()
Expand Down