Skip to content
This repository was archived by the owner on Nov 23, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions Sources/HTTP/HTTPServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,35 @@
/// abstraction, but the intention is to remove this dependency and reimplement
/// the class using transport APIs provided by the Server APIs working group.
public class HTTPServer {

/// Configuration options for creating HTTPServer
open class Options {
/// HTTPServer to be created on a given `port`
/// Note: For Port=0, the kernel assigns a random port. This will cause HTTPServer.port value
/// to diverge from HTTPServer.Options.port
public let port: Int

/// Create an instance of HTTPServerOptions
public init(onPort: Int = 0) {
port = onPort
}
}
public let options: Options

/// To process incoming requests
public let handler: HTTPRequestHandler

private let server = PoCSocketSimpleServer()

/// Create an instance of the server. This needs to be followed with a call to `start(port:handler:)`
public init() {
public init(with newOptions: Options, requestHandler: @escaping HTTPRequestHandler) {
options = newOptions
handler = requestHandler
}

/// Start the HTTP server on the given `port` number, using a `HTTPRequestHandler` to process incoming requests.
public func start(port: Int = 0, handler: @escaping HTTPRequestHandler) throws {
try server.start(port: port, handler: handler)
public func start() throws {
try server.start(port: options.port, handler: handler)
}

/// Stop the server
Expand Down
30 changes: 18 additions & 12 deletions Tests/HTTPTests/ServerTestsEndToEnd.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ class ServerTestsEndToEnd: XCTestCase {
func testOkEndToEnd() {
let receivedExpectation = self.expectation(description: "Received web response \(#function)")

let server = HTTPServer()
let options = HTTPServer.Options(onPort: 0)
let server = HTTPServer(with: options, requestHandler: OkHandler().handle)
do {
try server.start(port: 0, handler: OkHandler().handle)
try server.start()
let session = URLSession(configuration: .default)
let url = URL(string: "http://localhost:\(server.port)/")!
print("Test \(#function) on port \(server.port)")
Expand All @@ -44,9 +45,10 @@ class ServerTestsEndToEnd: XCTestCase {
func testHelloEndToEnd() {
let receivedExpectation = self.expectation(description: "Received web response \(#function)")

let server = HTTPServer()
let options = HTTPServer.Options(onPort: 0)
let server = HTTPServer(with: options, requestHandler: HelloWorldHandler().handle)
do {
try server.start(port: 0, handler: HelloWorldHandler().handle)
try server.start()
let session = URLSession(configuration: .default)
let url = URL(string: "http://localhost:\(server.port)/helloworld")!
print("Test \(#function) on port \(server.port)")
Expand Down Expand Up @@ -81,9 +83,10 @@ class ServerTestsEndToEnd: XCTestCase {
)
}

let server = HTTPServer()
let options = HTTPServer.Options(onPort: 0)
let server = HTTPServer(with: options, requestHandler: simpleHelloWebApp.handle)
do {
try server.start(port: 0, handler: simpleHelloWebApp.handle)
try server.start()
} catch {
XCTFail("Error listening on port \(0): \(error). Use server.failed(callback:) to handle")
}
Expand Down Expand Up @@ -117,9 +120,10 @@ class ServerTestsEndToEnd: XCTestCase {
let receivedExpectation = self.expectation(description: "Received web response \(#function)")
let testString="This is a test"

let server = HTTPServer()
let options = HTTPServer.Options(onPort: 0)
let server = HTTPServer(with: options, requestHandler: EchoHandler().handle)
do {
try server.start(port: 0, handler: EchoHandler().handle)
try server.start()
let session = URLSession(configuration: .default)
let url = URL(string: "http://localhost:\(server.port)/echo")!
print("Test \(#function) on port \(server.port)")
Expand Down Expand Up @@ -157,9 +161,10 @@ class ServerTestsEndToEnd: XCTestCase {
let testString2="This is a test, too"
let testString3="This is also a test"

let server = HTTPServer()
let options = HTTPServer.Options(onPort: 0)
let server = HTTPServer(with: options, requestHandler: EchoHandler().handle)
do {
try server.start(port: 0, handler: EchoHandler().handle)
try server.start()
let session = URLSession(configuration: .default)
let url = URL(string: "http://localhost:\(server.port)/echo")!
print("Test \(#function) on port \(server.port)")
Expand Down Expand Up @@ -238,9 +243,10 @@ class ServerTestsEndToEnd: XCTestCase {
let testString2="This is a test, too"
let testString3="This is also a test"

let server = HTTPServer()
let options = HTTPServer.Options(onPort: 0)
let server = HTTPServer(with: options, requestHandler: EchoHandler().handle)
do {
try server.start(port: 0, handler: EchoHandler().handle)
try server.start()
let session = URLSession(configuration: .default)
let url1 = URL(string: "http://localhost:\(server.port)/echo")!
print("Test \(#function) on port \(server.port)")
Expand Down