Skip to content

Commit

Permalink
Fix action handling for no contents (eclipse-thingweb#1042)
Browse files Browse the repository at this point in the history
* fix(binding-http/server/routes/action): return 204 when no content is returned

See eclipse-thingweb#1041

* fix(core/exposed-thing): allow returning 0 from action handler

See eclipse-thingweb#1041

* style(binding-http/test/http-server-test): use compact arrow function

* refctor(binding-http/test/http-server-test): remove unwanted double await

* test(binding-http/htt-server-test): check for action response code
  • Loading branch information
relu91 authored Aug 1, 2023
1 parent ca0e15c commit 0f6c5f8
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/binding-http/src/routes/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export default async function actionRoute(
res.writeHead(200);
output.body.pipe(res);
} else {
res.writeHead(200);
res.writeHead(204);
res.end();
}
} catch (err) {
Expand Down
38 changes: 37 additions & 1 deletion packages/binding-http/test/http-server-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,15 +204,51 @@ class HttpServerTest {
resp = await (await fetch(uri + "properties/test")).text();
expect(resp).to.equal('"on"');

resp = await (await fetch(uri + "actions/try", { method: "POST", body: "toggle" })).text();
let actionHttpResponse = await fetch(uri + "actions/try", { method: "POST", body: "toggle" });
resp = await actionHttpResponse.text();

expect(actionHttpResponse.status).to.equal(200);
expect(resp).to.equal('"TEST"');

actionHttpResponse = await fetch(uri + "actions/try", { method: "POST", body: undefined });
resp = await (await fetch(uri + "actions/try", { method: "POST", body: undefined })).text();

expect(actionHttpResponse.status).to.equal(200);
expect(resp).to.equal('"TEST"');

return httpServer.stop();
}

@test async "should return 204 when action has not output"() {
const httpServer = new HttpServer({ port: 0 });

await httpServer.start(null);

const testThing = new ExposedThing(null, {
title: "Test",
actions: {
noOutput: {
output: { type: "string" },
forms: [],
},
},
});

testThing.setActionHandler("noOutput", async () => undefined);

await httpServer.expose(testThing);

const uri = `http://localhost:${httpServer.getPort()}/test/`;

debug(`Testing ${uri}`);

const resp = await fetch(uri + "actions/noOutput", { method: "POST" });

expect(resp.status).to.equal(204);

return httpServer.stop();
}

@test async "should check uriVariables consistency"() {
const httpServer = new HttpServer({ port: 0 });

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/exposed-thing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ export default class ExposedThing extends TD.Thing implements WoT.ExposedThing {
new InteractionOutput(inputContent, form, this.actions[name].input),
options
);
if (result) {
if (result !== undefined) {
// TODO: handle form.response.contentType
return ContentManager.valueToContent(result, this.actions[name].output, form.contentType);
}
Expand Down
33 changes: 33 additions & 0 deletions packages/core/test/ServerTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,39 @@ class WoTServerTest {
callback.should.have.been.called();
}

@test async "should return content when returning 0 for action handler"() {
const thing = await WoTServerTest.WoT.produce({
title: "The Machine",
actions: {
test: {
output: {
type: "number",
},
forms: [
{
href: "http://example.org/test",
op: ["invokeaction"],
},
],
},
},
});
const callback = spy(async (params: InteractionOutput) => {
return 0;
});

thing.setActionHandler("test", callback);

const result = await (<ExposedThing>thing).handleInvokeAction(
"test",
new Content("application/json", Readable.from(Buffer.from(""))),
{ formIndex: 0 }
);

callback.should.have.been.called();
expect(result).to.be.instanceOf(Content);
}

@test async "should fail due to wrong uriVariable"() {
const thing = await WoTServerTest.WoT.produce({
title: "The Machine",
Expand Down

0 comments on commit 0f6c5f8

Please sign in to comment.