Skip to content

Commit

Permalink
Migrate to localhost tests (#100)
Browse files Browse the repository at this point in the history
* Migrate to localhost tests

* Use automatic port picking
  • Loading branch information
0xTim authored Aug 18, 2021
1 parent a2d26b3 commit bf01327
Showing 1 changed file with 49 additions and 31 deletions.
80 changes: 49 additions & 31 deletions Tests/WebSocketKitTests/WebSocketKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,20 @@ import NIOWebSocket

final class WebSocketKitTests: XCTestCase {
func testWebSocketEcho() throws {
let server = try ServerBootstrap.webSocket(on: self.elg) { req, ws in
ws.onText { ws, text in
ws.send(text)
}
}.bind(host: "localhost", port: 0).wait()

guard let port = server.localAddress?.port else {
XCTFail("couldn't get port from \(server.localAddress.debugDescription)")
return
}

let promise = elg.next().makePromise(of: String.self)
let closePromise = elg.next().makePromise(of: Void.self)
WebSocket.connect(to: "ws://echo.websocket.org", on: elg) { ws in
WebSocket.connect(to: "ws://localhost:\(port)", on: elg) { ws in
ws.send("hello")
ws.onText { ws, string in
promise.succeed(string)
Expand All @@ -17,27 +28,14 @@ final class WebSocketKitTests: XCTestCase {
}.cascadeFailure(to: promise)
try XCTAssertEqual(promise.futureResult.wait(), "hello")
XCTAssertNoThrow(try closePromise.futureResult.wait())
}

func testWebSocketWithTLSEcho() throws {
let promise = elg.next().makePromise(of: String.self)
WebSocket.connect(to: "wss://echo.websocket.org", on: elg) { ws in
ws.send("hello")
ws.onText { ws, string in
promise.succeed(string)
ws.close(promise: nil)
}
}.cascadeFailure(to: promise)
try XCTAssertEqual(promise.futureResult.wait(), "hello")
try server.close(mode: .all).wait()
}

func testBadHost() throws {
XCTAssertThrowsError(try WebSocket.connect(host: "asdf", on: elg) { _ in }.wait())
}

func testServerClose() throws {
let port = Int.random(in: 8000..<9000)

let sendPromise = self.elg.next().makePromise(of: Void.self)
let serverClose = self.elg.next().makePromise(of: Void.self)
let clientClose = self.elg.next().makePromise(of: Void.self)
Expand All @@ -47,7 +45,12 @@ final class WebSocketKitTests: XCTestCase {
ws.close(promise: serverClose)
}
}
}.bind(host: "localhost", port: port).wait()
}.bind(host: "localhost", port: 0).wait()

guard let port = server.localAddress?.port else {
XCTFail("couldn't get port from \(server.localAddress.debugDescription)")
return
}

WebSocket.connect(to: "ws://localhost:\(port)", on: self.elg) { ws in
ws.send("close", promise: sendPromise)
Expand All @@ -61,8 +64,6 @@ final class WebSocketKitTests: XCTestCase {
}

func testClientClose() throws {
let port = Int.random(in: 8000..<9000)

let sendPromise = self.elg.next().makePromise(of: Void.self)
let serverClose = self.elg.next().makePromise(of: Void.self)
let clientClose = self.elg.next().makePromise(of: Void.self)
Expand All @@ -71,7 +72,12 @@ final class WebSocketKitTests: XCTestCase {
ws.send(text)
}
ws.onClose.cascade(to: serverClose)
}.bind(host: "localhost", port: port).wait()
}.bind(host: "localhost", port: 0).wait()

guard let port = server.localAddress?.port else {
XCTFail("couldn't get port from \(server.localAddress.debugDescription)")
return
}

WebSocket.connect(to: "ws://localhost:\(port)", on: self.elg) { ws in
ws.send("close", promise: sendPromise)
Expand All @@ -89,16 +95,19 @@ final class WebSocketKitTests: XCTestCase {
}

func testImmediateSend() throws {
let port = Int.random(in: 8000..<9000)

let promise = self.elg.next().makePromise(of: String.self)
let server = try ServerBootstrap.webSocket(on: self.elg) { req, ws in
ws.send("hello")
ws.onText { ws, string in
promise.succeed(string)
ws.close(promise: nil)
}
}.bind(host: "localhost", port: port).wait()
}.bind(host: "localhost", port: 0).wait()

guard let port = server.localAddress?.port else {
XCTFail("couldn't get port from \(server.localAddress.debugDescription)")
return
}

WebSocket.connect(to: "ws://localhost:\(port)", on: self.elg) { ws in
ws.onText { ws, string in
Expand All @@ -112,14 +121,17 @@ final class WebSocketKitTests: XCTestCase {
}

func testWebSocketPong() throws {
let port = Int.random(in: 8000..<9000)

let pongPromise = self.elg.next().makePromise(of: String.self)
let server = try ServerBootstrap.webSocket(on: self.elg) { req, ws in
ws.onPing { ws in
ws.close(promise: nil)
}
}.bind(host: "localhost", port: port).wait()
}.bind(host: "localhost", port: 0).wait()

guard let port = server.localAddress?.port else {
XCTFail("couldn't get port from \(server.localAddress.debugDescription)")
return
}

WebSocket.connect(to: "ws://localhost:\(port)", on: self.elg) { ws in
ws.send(raw: Data(), opcode: .ping)
Expand All @@ -134,13 +146,16 @@ final class WebSocketKitTests: XCTestCase {
}

func testErrorCode() throws {
let port = Int.random(in: 8000..<9000)

let promise = self.elg.next().makePromise(of: WebSocketErrorCode.self)

let server = try ServerBootstrap.webSocket(on: self.elg) { req, ws in
ws.close(code: .normalClosure, promise: nil)
}.bind(host: "localhost", port: port).wait()
}.bind(host: "localhost", port: 0).wait()

guard let port = server.localAddress?.port else {
XCTFail("couldn't get port from \(server.localAddress.debugDescription)")
return
}

WebSocket.connect(to: "ws://localhost:\(port)", on: self.elg) { ws in
ws.onText { ws, string in
Expand All @@ -157,14 +172,17 @@ final class WebSocketKitTests: XCTestCase {
}

func testHeadersAreSent() throws {
let port = Int.random(in: 8000..<9000)

let promise = self.elg.next().makePromise(of: String.self)

let server = try ServerBootstrap.webSocket(on: self.elg) { req, ws in
promise.succeed(req.headers.first(name: "Auth")!)
ws.close(promise: nil)
}.bind(host: "localhost", port: port).wait()
}.bind(host: "localhost", port: 0).wait()

guard let port = server.localAddress?.port else {
XCTFail("couldn't get port from \(server.localAddress.debugDescription)")
return
}

WebSocket.connect(
to: "ws://localhost:\(port)",
Expand Down

0 comments on commit bf01327

Please sign in to comment.