diff --git a/node/td-utils/test/examples/httpAndMqtt.json b/node/td-utils/test/examples/httpAndMqtt.json index 2feb331..9813819 100644 --- a/node/td-utils/test/examples/httpAndMqtt.json +++ b/node/td-utils/test/examples/httpAndMqtt.json @@ -17,17 +17,5 @@ "toggle": { "forms": [{ "href": "mqtt://mylamp.example.com:1234/toggle" }] } - }, - "protocolSchemes": [ - { - "protocol": "http", - "hostname": "mylamp.example.com", - "port": 1234 - }, - { - "protocol": "mqtt", - "hostname": "mylamp.example.com", - "port": 1234 - } - ] + } } diff --git a/node/td-utils/test/examples/noProtocol.json b/node/td-utils/test/examples/noProtocol.json index 2ba85d3..49c57cb 100644 --- a/node/td-utils/test/examples/noProtocol.json +++ b/node/td-utils/test/examples/noProtocol.json @@ -9,6 +9,5 @@ "security": ["basic_sc"], "properties": {}, "actions": {}, - "events": {}, - "protocolSchemes": [] + "events": {} } diff --git a/node/td-utils/test/examples/onlyHttp.json b/node/td-utils/test/examples/onlyHttp.json index 6d7bda2..d8d39f4 100644 --- a/node/td-utils/test/examples/onlyHttp.json +++ b/node/td-utils/test/examples/onlyHttp.json @@ -28,17 +28,5 @@ } ] } - }, - "protocolSchemes": [ - { - "protocol": "http", - "hostname": "mylamp.example.com", - "port": 4212 - }, - { - "protocol": "http", - "hostname": "mylamp.example.com", - "port": 4214 - } - ] + } } diff --git a/node/td-utils/test/examples/onlyMqtt.json b/node/td-utils/test/examples/onlyMqtt.json index e72eea7..2cccee7 100644 --- a/node/td-utils/test/examples/onlyMqtt.json +++ b/node/td-utils/test/examples/onlyMqtt.json @@ -28,11 +28,5 @@ } ] } - }, - "protocolSchemes": [ - { - "protocol": "mqtt", - "hostname": "mylamp.example.com" - } - ] + } } diff --git a/node/td-utils/test/examples/secureProtocols.json b/node/td-utils/test/examples/secureProtocols.json index be9c2cb..4db39a2 100644 --- a/node/td-utils/test/examples/secureProtocols.json +++ b/node/td-utils/test/examples/secureProtocols.json @@ -18,15 +18,5 @@ } ] } - }, - "protocolSchemes": [ - { - "protocol": "https", - "hostname": "mylamp.example.com" - }, - { - "protocol": "mqtts", - "hostname": "mylamp.example.com" - } - ] + } } diff --git a/node/td-utils/test/protocol-detection.test.suite.ts b/node/td-utils/test/protocol-detection.test.suite.ts new file mode 100644 index 0000000..59231d5 --- /dev/null +++ b/node/td-utils/test/protocol-detection.test.suite.ts @@ -0,0 +1,69 @@ +import httpAndMqtt from "./examples/httpAndMqtt.json"; +import noProtocol from "./examples/noProtocol.json"; +import onlyHttp from "./examples/onlyHttp.json"; +import onlyMqtt from "./examples/onlyMqtt.json"; +import secureProtocols from "./examples/secureProtocols.json"; + +export const testSuite = [ + { + name: "httpAndMqtt", + input: httpAndMqtt, + expected: [ + { + protocol: "http", + hostname: "mylamp.example.com", + port: 1234, + }, + { + protocol: "mqtt", + hostname: "mylamp.example.com", + port: 1234, + }, + ], + }, + { + name: "noProtocol", + input: noProtocol, + expected: [], + }, + { + name: "onlyHttp", + input: onlyHttp, + expected: [ + { + protocol: "http", + hostname: "mylamp.example.com", + port: 4212, + }, + { + protocol: "http", + hostname: "mylamp.example.com", + port: 4214, + }, + ], + }, + { + name: "onlyMqtt", + input: onlyMqtt, + expected: [ + { + protocol: "mqtt", + hostname: "mylamp.example.com", + }, + ], + }, + { + name: "secureProtocols", + input: secureProtocols, + expected: [ + { + protocol: "https", + hostname: "mylamp.example.com", + }, + { + protocol: "mqtts", + hostname: "mylamp.example.com", + }, + ], + }, +]; diff --git a/node/td-utils/test/protocol-detection.test.ts b/node/td-utils/test/protocol-detection.test.ts index 926419a..22b4a1e 100644 --- a/node/td-utils/test/protocol-detection.test.ts +++ b/node/td-utils/test/protocol-detection.test.ts @@ -14,41 +14,23 @@ */ import { detectProtocolSchemes, ProtocolScheme } from "../src/detectProtocolSchemes"; import { ThingDescription } from "wot-thing-description-types"; -import httpAndMqtt from "./examples/httpAndMqtt.json"; -import noProtocol from "./examples/noProtocol.json"; -import onlyHttp from "./examples/onlyHttp.json"; -import onlyMqtt from "./examples/onlyMqtt.json"; -import secureProtocols from "./examples/secureProtocols.json"; +import { testSuite } from "./protocol-detection.test.suite"; export type ThingDescriptionTest = ThingDescription & { protocolSchemes: ProtocolScheme[] }; describe("test examples", () => { - it("should test httpAndMqtt", () => { - testTD(httpAndMqtt); - }); - - it("should test noProtocol", () => { - testTD(noProtocol); - }); - - it("should test onlyHttp", () => { - testTD(onlyHttp); - }); - - it("should test onlyMqtt", () => { - testTD(onlyMqtt); - }); - - it("should test secureProtocols", () => { - testTD(secureProtocols); + testSuite.forEach((t) => { + it(`should test ${t.name}`, () => { + testTD(t.input, t.expected); + }); }); }); -const testTD = (td: any) => { +const testTD = (td: any, expected: ProtocolScheme[]) => { const detectedProtocolSchemes = detectProtocolSchemes(JSON.stringify(td)); - expect(detectedProtocolSchemes.length).toBe(td.protocolSchemes.length); - td.protocolSchemes.forEach((s: ProtocolScheme) => { + expect(detectedProtocolSchemes.length).toBe(expected.length); + expected.forEach((s: ProtocolScheme) => { expect(detectedProtocolSchemes).toContainEqual(s); }); };