Skip to content

Commit f2d378f

Browse files
test: add test with wrong contentType
1 parent 5ea6d50 commit f2d378f

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

packages/core/test/ClientTest.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import { Readable } from "stream";
3636
import { createLoggers, ProtocolHelpers } from "../src/core";
3737
import { ThingDescription } from "wot-typescript-definitions";
3838
import chaiAsPromised from "chai-as-promised";
39+
import { fail } from "assert";
3940

4041
const { debug } = createLoggers("core", "ClientTest");
4142

@@ -487,6 +488,28 @@ class WoTClientTest {
487488
expect(value).to.equal(42);
488489
}
489490

491+
@test async "call an action with wrong contentType based on TD response"() {
492+
// an action
493+
WoTClientTest.clientFactory.setTrap(async (form: Form, content: Content) => {
494+
const valueData = await content.toBuffer();
495+
expect(valueData.toString()).to.equal("23");
496+
// Note: application/json expected based on TD response
497+
return new Content("text/plain", Readable.from(Buffer.from("42")));
498+
});
499+
const td = (await WoTClientTest.WoTHelpers.fetch("td://foo")) as ThingDescription;
500+
const thing = await WoTClientTest.WoT.consume(td);
501+
502+
expect(thing).to.have.property("title").that.equals("aThing");
503+
expect(thing).to.have.property("actions").that.has.property("anAction");
504+
try {
505+
await thing.invokeAction("anAction", 23);
506+
fail("Should report unexpected content type");
507+
} catch (e) {
508+
const error = e instanceof Error ? e : new Error(JSON.stringify(e));
509+
expect(error.message).to.contain("type");
510+
}
511+
}
512+
490513
@test async "subscribe to event"() {
491514
WoTClientTest.clientFactory.setTrap(() => {
492515
return new Content("application/json", Readable.from(Buffer.from("triggered")));

0 commit comments

Comments
 (0)