Skip to content

Commit

Permalink
Add test for WebSocketInitialRequestHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler committed Jun 4, 2023
1 parent 492fc2c commit b3fa243
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions Tests/MQTTNIOTests/MQTTNIOTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,46 @@ final class MQTTNIOTests: XCTestCase {
try client.disconnect().wait()
}

func testWebSocketInitialRequest() throws {
let el = EmbeddedEventLoop()
defer { XCTAssertNoThrow(try el.syncShutdownGracefully()) }
let promise = el.makePromise(of: Void.self)
let initialRequestHandler = WebSocketInitialRequestHandler(
host: "test.mosquitto.org",
urlPath: "/mqtt",
additionalHeaders: ["Test": "Value"],
upgradePromise: promise
)
let channel = EmbeddedChannel(handler: initialRequestHandler, loop: el)
try channel.connect(to: SocketAddress(ipAddress: "127.0.0.1", port: 0)).wait()
let requestHead = try channel.readOutbound(as: HTTPClientRequestPart.self)
let requestBody = try channel.readOutbound(as: HTTPClientRequestPart.self)
let requestEnd = try channel.readOutbound(as: HTTPClientRequestPart.self)
switch requestHead {
case .head(let head):
XCTAssertEqual(head.uri, "/mqtt")
XCTAssertEqual(head.headers["host"].first, "test.mosquitto.org")
XCTAssertEqual(head.headers["Sec-WebSocket-Protocol"].first, "mqtt")
XCTAssertEqual(head.headers["Test"].first, "Value")
default:
XCTFail("Did not expect \(String(describing: requestHead))")
}
switch requestBody {
case .body(let data):
XCTAssertEqual(data, .byteBuffer(ByteBuffer()))
default:
XCTFail("Did not expect \(String(describing: requestBody))")
}
switch requestEnd {
case .end(nil):
break
default:
XCTFail("Did not expect \(String(describing: requestEnd))")
}
_ = try channel.finish()
promise.succeed()
}

// MARK: Helper variables and functions

func createClient(identifier: String, configuration: MQTTClient.Configuration = .init()) -> MQTTClient {
Expand Down

0 comments on commit b3fa243

Please sign in to comment.