Skip to content

Commit 1ebd2e7

Browse files
test(binding-http): add CORS tests for POST and PUT operations
1 parent 943b593 commit 1ebd2e7

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

packages/binding-http/test/http-server-cors-test.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,59 @@ class HttpServerCorsTest {
194194
expect(methods).to.contain("GET");
195195
expect(methods).to.contain("OPTIONS");
196196
}
197+
198+
@test async "should handle CORS for write property (PUT)"() {
199+
this.thing = new ExposedThing(this.servient, {
200+
title: "TestThingWrite",
201+
properties: {
202+
test: {
203+
type: "string",
204+
forms: [],
205+
},
206+
},
207+
});
208+
209+
this.thing.setPropertyWriteHandler("test", () => Promise.resolve(undefined));
210+
211+
await this.httpServer.expose(this.thing);
212+
213+
const uri = `http://localhost:${this.httpServer.getPort()}/testthingwrite/properties/test`;
214+
const response = await fetch(uri, {
215+
method: "PUT",
216+
body: JSON.stringify("new-value"),
217+
headers: {
218+
Origin: "http://example.com",
219+
"Content-Type": "application/json",
220+
},
221+
});
222+
223+
expect(response.status).to.equal(204);
224+
expect(response.headers.get("Access-Control-Allow-Origin")).to.equal("*");
225+
}
226+
227+
@test async "should handle CORS for invoke action (POST)"() {
228+
this.thing = new ExposedThing(this.servient, {
229+
title: "TestThingAction",
230+
actions: {
231+
test: {
232+
forms: [],
233+
},
234+
},
235+
});
236+
237+
this.thing.setActionHandler("test", () => Promise.resolve(undefined));
238+
239+
await this.httpServer.expose(this.thing);
240+
241+
const uri = `http://localhost:${this.httpServer.getPort()}/testthingaction/actions/test`;
242+
const response = await fetch(uri, {
243+
method: "POST",
244+
headers: {
245+
Origin: "http://example.com",
246+
},
247+
});
248+
249+
expect(response.status).to.equal(204); // Action without output returns 204
250+
expect(response.headers.get("Access-Control-Allow-Origin")).to.equal("*");
251+
}
197252
}

0 commit comments

Comments
 (0)