Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix action handling for no contents #1042

Merged
merged 6 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
30 changes: 30 additions & 0 deletions packages/binding-http/test/http-server-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,36 @@ class HttpServerTest {
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);
danielpeintner marked this conversation as resolved.
Show resolved Hide resolved

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) {
JKRhb marked this conversation as resolved.
Show resolved Hide resolved
// 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
Loading