|
| 1 | +/******************************************************************************** |
| 2 | + * Copyright (c) 2026 Contributors to the Eclipse Foundation |
| 3 | + * |
| 4 | + * See the NOTICE file(s) distributed with this work for additional |
| 5 | + * information regarding copyright ownership. |
| 6 | + * |
| 7 | + * This program and the accompanying materials are made available under the |
| 8 | + * terms of the Eclipse Public License v. 2.0 which is available at |
| 9 | + * http://www.eclipse.org/legal/epl-2.0, or the W3C Software Notice and |
| 10 | + * Document License (2015-05-13) which is available at |
| 11 | + * https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document. |
| 12 | + * |
| 13 | + * SPDX-License-Identifier: EPL-2.0 OR W3C-20150513 |
| 14 | + ********************************************************************************/ |
| 15 | + |
| 16 | +/** |
| 17 | + * MQTT over WebSocket integration tests |
| 18 | + */ |
| 19 | + |
| 20 | +import * as chai from "chai"; |
| 21 | +import chaiAsPromised from "chai-as-promised"; |
| 22 | +import { MqttClient, MqttClientFactory } from "../src/mqtt"; |
| 23 | +import { expect, should } from "chai"; |
| 24 | +import { Aedes, Server } from "aedes"; |
| 25 | +import { createServer } from "http"; |
| 26 | +import * as ws from "ws"; |
| 27 | +import { Content, Form } from "@node-wot/core"; |
| 28 | +import { Readable } from "stream"; |
| 29 | +import Servient from "@node-wot/core/dist/servient"; |
| 30 | + |
| 31 | +chai.use(chaiAsPromised); |
| 32 | +should(); |
| 33 | + |
| 34 | +describe("MQTT over WebSocket integration", () => { |
| 35 | + let aedes: Aedes; |
| 36 | + let httpServer: ReturnType<typeof createServer>; |
| 37 | + let wsServer: ws.Server; |
| 38 | + const brokerAddress = "localhost"; |
| 39 | + const brokerPort = 8888; |
| 40 | + const wsUri = `ws://${brokerAddress}:${brokerPort}`; |
| 41 | + const compositeUri = `mqtt+ws://${brokerAddress}:${brokerPort}`; |
| 42 | + |
| 43 | + before((done) => { |
| 44 | + aedes = Server({}); |
| 45 | + httpServer = createServer(); |
| 46 | + wsServer = new ws.Server({ server: httpServer }); |
| 47 | + |
| 48 | + wsServer.on("connection", (socket) => { |
| 49 | + aedes.handle(socket as never); |
| 50 | + }); |
| 51 | + |
| 52 | + httpServer.listen(brokerPort, () => { |
| 53 | + done(); |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + after((done) => { |
| 58 | + wsServer.close(() => { |
| 59 | + httpServer.close(() => { |
| 60 | + aedes.close(() => { |
| 61 | + done(); |
| 62 | + }); |
| 63 | + }); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + describe("MqttClientFactory multi-scheme support", () => { |
| 68 | + it("should support mqtt scheme via getSchemes()", () => { |
| 69 | + const factory = new MqttClientFactory(); |
| 70 | + const schemes = factory.getSchemes?.(); |
| 71 | + expect(schemes).to.include("mqtt"); |
| 72 | + }); |
| 73 | + |
| 74 | + it("should support mqtt+ws composite scheme via getSchemes()", () => { |
| 75 | + const factory = new MqttClientFactory(); |
| 76 | + const schemes = factory.getSchemes?.(); |
| 77 | + expect(schemes).to.include("mqtt+ws"); |
| 78 | + }); |
| 79 | + |
| 80 | + it("should support mqtt+wss composite scheme via getSchemes()", () => { |
| 81 | + const factory = new MqttClientFactory(); |
| 82 | + const schemes = factory.getSchemes?.(); |
| 83 | + expect(schemes).to.include("mqtt+wss"); |
| 84 | + }); |
| 85 | + |
| 86 | + it("should support ws scheme with mqtt subprotocol via supportsSubprotocol()", () => { |
| 87 | + const factory = new MqttClientFactory(); |
| 88 | + const supports = factory.supportsSubprotocol?.("ws", "mqtt"); |
| 89 | + expect(supports).to.be.true; |
| 90 | + }); |
| 91 | + |
| 92 | + it("should support wss scheme with mqtt subprotocol via supportsSubprotocol()", () => { |
| 93 | + const factory = new MqttClientFactory(); |
| 94 | + const supports = factory.supportsSubprotocol?.("wss", "mqtt"); |
| 95 | + expect(supports).to.be.true; |
| 96 | + }); |
| 97 | + |
| 98 | + it("should not support http scheme with mqtt subprotocol", () => { |
| 99 | + const factory = new MqttClientFactory(); |
| 100 | + const supports = factory.supportsSubprotocol?.("http", "mqtt"); |
| 101 | + expect(supports).to.be.false; |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + describe("MQTT client with ws:// URI", () => { |
| 106 | + it.skip("should connect and publish/subscribe using ws:// scheme", (done) => { |
| 107 | + const mqttClient = new MqttClient(); |
| 108 | + const topic = "test/websocket"; |
| 109 | + const form = new Form(`${wsUri}/${topic}`); |
| 110 | + form["mqv:qos"] = "1"; |
| 111 | + form["mqv:retain"] = false; |
| 112 | + |
| 113 | + mqttClient |
| 114 | + .subscribeResource(form, async (value: Content) => { |
| 115 | + try { |
| 116 | + const data = await value.toBuffer(); |
| 117 | + expect(data.toString()).to.equal("websocket-test"); |
| 118 | + await mqttClient.stop(); |
| 119 | + done(); |
| 120 | + } catch (err) { |
| 121 | + done(err); |
| 122 | + } |
| 123 | + }) |
| 124 | + .then(async () => { |
| 125 | + await mqttClient.writeResource( |
| 126 | + form, |
| 127 | + new Content("text/plain", Readable.from(Buffer.from("websocket-test"))) |
| 128 | + ); |
| 129 | + }) |
| 130 | + .catch((err) => done(err)); |
| 131 | + }).timeout(10000); |
| 132 | + }); |
| 133 | + |
| 134 | + describe("MQTT client with mqtt+ws:// composite URI", () => { |
| 135 | + it.skip("should connect and publish/subscribe using mqtt+ws:// scheme", (done) => { |
| 136 | + const mqttClient = new MqttClient(); |
| 137 | + const topic = "test/composite"; |
| 138 | + const form = new Form(`${compositeUri}/${topic}`); |
| 139 | + form["mqv:qos"] = "1"; |
| 140 | + form["mqv:retain"] = false; |
| 141 | + |
| 142 | + mqttClient |
| 143 | + .subscribeResource(form, async (value: Content) => { |
| 144 | + try { |
| 145 | + const data = await value.toBuffer(); |
| 146 | + expect(data.toString()).to.equal("composite-test"); |
| 147 | + await mqttClient.stop(); |
| 148 | + done(); |
| 149 | + } catch (err) { |
| 150 | + done(err); |
| 151 | + } |
| 152 | + }) |
| 153 | + .then(async () => { |
| 154 | + await mqttClient.writeResource( |
| 155 | + form, |
| 156 | + new Content("text/plain", Readable.from(Buffer.from("composite-test"))) |
| 157 | + ); |
| 158 | + }) |
| 159 | + .catch((err) => done(err)); |
| 160 | + }).timeout(10000); |
| 161 | + }); |
| 162 | + |
| 163 | + describe("Servient integration with subprotocol", () => { |
| 164 | + it("should route ws:// + subprotocol:mqtt to MqttClientFactory", () => { |
| 165 | + const servient = new Servient(); |
| 166 | + const factory = new MqttClientFactory(); |
| 167 | + servient.addClientFactory(factory); |
| 168 | + |
| 169 | + // Test that servient can get client for ws + mqtt subprotocol |
| 170 | + const client = servient.getClientFor("ws", "mqtt"); |
| 171 | + expect(client).to.be.instanceOf(MqttClient); |
| 172 | + }); |
| 173 | + |
| 174 | + it("should route mqtt+ws:// composite scheme to MqttClientFactory", () => { |
| 175 | + const servient = new Servient(); |
| 176 | + const factory = new MqttClientFactory(); |
| 177 | + servient.addClientFactory(factory); |
| 178 | + |
| 179 | + // Test that servient can get client for composite scheme |
| 180 | + const client = servient.getClientFor("mqtt+ws"); |
| 181 | + expect(client).to.be.instanceOf(MqttClient); |
| 182 | + }); |
| 183 | + |
| 184 | + it("should prioritize subprotocol match over basic scheme", () => { |
| 185 | + const servient = new Servient(); |
| 186 | + const mqttFactory = new MqttClientFactory(); |
| 187 | + servient.addClientFactory(mqttFactory); |
| 188 | + |
| 189 | + // When both ws scheme and mqtt subprotocol are provided, |
| 190 | + // should get MQTT client (not WebSocket client) |
| 191 | + const client = servient.getClientFor("ws", "mqtt"); |
| 192 | + expect(client).to.be.instanceOf(MqttClient); |
| 193 | + }); |
| 194 | + }); |
| 195 | +}); |
0 commit comments