Skip to content

Commit

Permalink
Merge pull request #1094 from JKRhb/coap-server-await
Browse files Browse the repository at this point in the history
test(binding-coap): `await` all calls of `coapServer.stop()`
  • Loading branch information
relu91 authored Sep 21, 2023
2 parents f995042 + 7a3538a commit 27970b0
Showing 1 changed file with 44 additions and 36 deletions.
80 changes: 44 additions & 36 deletions packages/binding-coap/test/coap-server-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ class CoapServerTest {
const resp = await coapClient.readResource(new TD.Form(uri + "properties/test?id=testId&globalVarTest=test1"));
expect((await resp.toBuffer()).toString()).to.equal('"testValue"');

return coapServer.stop();
await coapServer.stop();
}

@test async "should support /.well-known/core"() {
Expand All @@ -307,7 +307,7 @@ class CoapServerTest {
'</test1>;rt="wot.thing";ct="50 432",</test2>;rt="wot.thing";ct="50 432"'
);

return coapServer.stop();
await coapServer.stop();
}

@test async "should support TD Content-Format negotiation"() {
Expand All @@ -323,7 +323,6 @@ class CoapServerTest {
await coapServer.expose(testThing);

const uri = `coap://localhost:${coapServer.getPort()}/test`;
let responseCounter = 0;

registerFormat("application/foobar", 65000);

Expand All @@ -337,31 +336,36 @@ class CoapServerTest {
null,
];

for (const contentFormat of contentFormats) {
const req = request(uri);

if (contentFormat != null) {
req.setHeader("Accept", contentFormat);
}

req.on("response", (res: IncomingMessage) => {
const requestContentFormat = res.headers["Content-Format"];
const promises = contentFormats.map(
(contentFormat) =>
new Promise<void>((resolve) => {
const req = request(uri);

if (contentFormat != null) {
req.setHeader("Accept", contentFormat);
}

req.on("response", async (res: IncomingMessage) => {
const requestContentFormat = res.headers["Content-Format"];

if (contentFormat === unsupportedContentFormat) {
expect(res.code).to.equal("4.06");
expect(res.payload.toString()).to.equal(
`Content-Format ${unsupportedContentFormat} is not supported by this resource.`
);
} else {
expect(requestContentFormat).to.equal(contentFormat ?? defaultContentFormat);
}

resolve();
});
req.end();
})
);

if (contentFormat === unsupportedContentFormat) {
expect(res.code).to.equal("4.06");
expect(res.payload.toString()).to.equal(
`Content-Format ${unsupportedContentFormat} is not supported by this resource.`
);
} else {
expect(requestContentFormat).to.equal(contentFormat ?? defaultContentFormat);
}
await Promise.all(promises);

if (++responseCounter >= contentFormats.length) {
coapServer.stop();
}
});
req.end();
}
await coapServer.stop();
}

@test async "should supply Size2 option when fetching a TD"() {
Expand All @@ -377,17 +381,21 @@ class CoapServerTest {

await coapServer.expose(testThing);

const req = request({
host: "localhost",
pathname: "test",
port: coapServer.getPort(),
});
req.setOption("Size2", 0);
req.on("response", (res) => {
expect(res.headers.Size2).to.equal(JSON.stringify(testThing.getThingDescription()).length);
coapServer.stop();
await new Promise<void>((resolve) => {
const req = request({
host: "localhost",
pathname: "test",
port: coapServer.getPort(),
});
req.setOption("Size2", 0);
req.on("response", (res) => {
expect(res.headers.Size2).to.equal(JSON.stringify(testThing.getThingDescription()).length);
resolve();
});
req.end();
});
req.end();

await coapServer.stop();
}

@test async "should check uriVariables consistency"() {
Expand Down

0 comments on commit 27970b0

Please sign in to comment.