From 98e82ad78062dc2e9f42c156a68df4a6144c74d0 Mon Sep 17 00:00:00 2001 From: reluc Date: Thu, 9 May 2024 12:43:55 +0200 Subject: [PATCH 1/3] test(binding-http/http-client-test): use correct name for invoking and writing tests --- packages/binding-http/test/http-client-test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/binding-http/test/http-client-test.ts b/packages/binding-http/test/http-client-test.ts index 026008ac2..918e72b4f 100644 --- a/packages/binding-http/test/http-client-test.ts +++ b/packages/binding-http/test/http-client-test.ts @@ -283,7 +283,7 @@ class HttpClientTest1 { await this.client.writeResource(inputVector2.form, new DefaultContent(Readable.from(inputVector2.payload))); } - @test async "should apply form information - read with PUT instead of GET"() { + @test async "should apply form information - invoke with PUT instead of GET"() { // invoke with PUT instead of POST const inputVector3 = { op: ["invokeaction"], @@ -297,7 +297,7 @@ class HttpClientTest1 { await this.client.invokeResource(inputVector3.form, new DefaultContent(Readable.from(inputVector3.payload))); } - @test async "should apply form information - read with DELETE instead of POST"() { + @test async "should apply form information - invoke with DELETE instead of POST"() { // invoke with DELETE instead of POST const inputVector4 = { op: ["invokeaction"], From 2ced92086f61ee687840d6c193da51eb284dcd20 Mon Sep 17 00:00:00 2001 From: reluc Date: Thu, 9 May 2024 12:44:17 +0200 Subject: [PATCH 2/3] fix(binding-http): present the accept header according to TD specification See https://www.w3.org/TR/wot-thing-description11/#contentType-usage Fix #854 --- packages/binding-http/src/http-client-impl.ts | 22 +++- .../binding-http/test/http-client-test.ts | 113 +++++++++++++++++- 2 files changed, 133 insertions(+), 2 deletions(-) diff --git a/packages/binding-http/src/http-client-impl.ts b/packages/binding-http/src/http-client-impl.ts index 913624d0b..c46a364f8 100644 --- a/packages/binding-http/src/http-client-impl.ts +++ b/packages/binding-http/src/http-client-impl.ts @@ -112,7 +112,10 @@ export default class HttpClient implements ProtocolClient { } public async readResource(form: HttpForm): Promise { - const request = await this.generateFetchRequest(form, "GET"); + // See https://www.w3.org/TR/wot-thing-description11/#contentType-usage + // Cases: 1B + const headers = form.contentType != null ? [["accept", form.contentType]] : [["accept", ContentSerdes.DEFAULT]]; + const request = await this.generateFetchRequest(form, "GET", { headers }); debug(`HttpClient (readResource) sending ${request.method} to ${request.url}`); const result = await this.fetch(request); @@ -176,6 +179,15 @@ export default class HttpClient implements ProtocolClient { public async invokeResource(form: HttpForm, content?: Content): Promise { const headers = content != null ? [["content-type", content.type]] : []; + // See https://www.w3.org/TR/wot-thing-description11/#contentType-usage + // Cases: 1C and 2A + if (form.response?.contentType != null) { + headers.push(["accept", form.response?.contentType]); + } else if (form.contentType != null) { + headers.push(["accept", form.contentType]); + } else { + headers.push(["accept", ContentSerdes.DEFAULT]); + } const request = await this.generateFetchRequest(form, "POST", { headers, @@ -347,12 +359,20 @@ export default class HttpClient implements ProtocolClient { const headers = form["htv:headers"] as Array; for (const option of headers) { + // override defaults + requestInit.headers = requestInit.headers.filter( + (header) => header[0].toLowerCase() !== option["htv:fieldName"].toLowerCase() + ); requestInit.headers.push([option["htv:fieldName"], option["htv:fieldValue"]]); } } else if (typeof form["htv:headers"] === "object") { debug(`HttpClient got Form SINGLE-ENTRY 'headers' ${form["htv:headers"]}`); const option = form["htv:headers"] as HttpHeader; + // override defaults + requestInit.headers = requestInit.headers.filter( + (header) => header[0].toLowerCase() !== option["htv:fieldName"].toLowerCase() + ); requestInit.headers.push([option["htv:fieldName"], option["htv:fieldValue"]]); } diff --git a/packages/binding-http/test/http-client-test.ts b/packages/binding-http/test/http-client-test.ts index 918e72b4f..14d7ad6a4 100644 --- a/packages/binding-http/test/http-client-test.ts +++ b/packages/binding-http/test/http-client-test.ts @@ -50,6 +50,7 @@ interface TestVector { method?: string; schema?: DataSchema; payload?: DataSchemaValue; + headers?: Record; form: Form; } @@ -153,6 +154,10 @@ class TestHttpServer implements ProtocolServer { expect(req.method).to.equal(this.testVector.method); expect(req.url).to.equal(new URL(this.testVector.form.href).pathname); + if (this.testVector.headers) { + expect(req.headers).to.include(this.testVector.headers); + } + if (this.testVector.payload !== undefined) { // load payload const body: Array = []; @@ -211,6 +216,9 @@ class HttpClientTest1 { form: { href: `http://localhost:${port1}/`, }, + headers: { + accept: "application/json", + }, }; HttpClientTest1.httpServer.setTestVector(inputVector1); const resource = await this.client.readResource(inputVector1.form); @@ -225,6 +233,9 @@ class HttpClientTest1 { form: { href: `http://localhost:${port1}/`, }, + headers: { + "content-type": "application/json", + }, payload: "test", }; HttpClientTest1.httpServer.setTestVector(inputVector2); @@ -239,6 +250,10 @@ class HttpClientTest1 { form: { href: `http://localhost:${port1}/`, }, + headers: { + "content-type": "application/json", + accept: "application/json", + }, payload: "test", }; HttpClientTest1.httpServer.setTestVector(inputVector3); @@ -269,7 +284,46 @@ class HttpClientTest1 { body.toString("ascii").should.eql(""); } - @test async "should apply form information - read with POST instead of PUT"() { + @test async "should apply form information - read with not default content-type"() { + // read with defaults + const inputVector1 = { + op: ["readproperty"], + form: { + href: `http://localhost:${port1}/`, + contentType: "text/plain", + }, + headers: { + accept: "text/plain", + }, + }; + HttpClientTest1.httpServer.setTestVector(inputVector1); + const resource = await this.client.readResource(inputVector1.form); + const body = await resource.toBuffer(); + body.toString("ascii").should.eql(""); + } + + @test async "should apply form information - read with header override"() { + // read with defaults + const inputVector1 = { + op: ["readproperty"], + form: { + href: `http://localhost:${port1}/`, + "htv:headers": { + "htv:fieldName": "accept", + "htv:fieldValue": "text/plain", + }, + }, + headers: { + accept: "text/plain", + }, + }; + HttpClientTest1.httpServer.setTestVector(inputVector1); + const resource = await this.client.readResource(inputVector1.form); + const body = await resource.toBuffer(); + body.toString("ascii").should.eql(""); + } + + @test async "should apply form information - write with POST instead of PUT"() { // write with POST instead of PUT const inputVector2 = { op: ["writeproperty"], @@ -309,6 +363,63 @@ class HttpClientTest1 { HttpClientTest1.httpServer.setTestVector(inputVector4); await this.client.invokeResource(inputVector4.form); } + + @test async "should apply form information - invoke with not default content-type and no inputs"() { + // invoke with DELETE instead of POST + const inputVector4 = { + op: ["invokeaction"], + form: { + href: `http://localhost:${port1}/`, + contentType: "text/plain", + }, + headers: { + accept: "text/plain", + }, + }; + HttpClientTest1.httpServer.setTestVector(inputVector4); + await this.client.invokeResource(inputVector4.form); + } + + @test async "should apply form information - invoke with not default content-type"() { + // invoke with DELETE instead of POST + const inputVector4 = { + op: ["invokeaction"], + form: { + href: `http://localhost:${port1}/`, + contentType: "text/plain", + }, + headers: { + "content-type": "text/plain", + accept: "text/plain", + }, + payload: "test", + }; + HttpClientTest1.httpServer.setTestVector(inputVector4); + await this.client.invokeResource( + inputVector4.form, + new Content("text/plain", Readable.from(inputVector4.payload)) + ); + } + + @test async "should apply form information - invoke with default content-type and response content-type"() { + // invoke with DELETE instead of POST + const inputVector4 = { + op: ["invokeaction"], + form: { + href: `http://localhost:${port1}/`, + response: { + contentType: "text/plain", + }, + }, + headers: { + "content-type": "application/json", + accept: "text/plain", + }, + payload: "test", + }; + HttpClientTest1.httpServer.setTestVector(inputVector4); + await this.client.invokeResource(inputVector4.form, new DefaultContent(Readable.from(inputVector4.payload))); + } } @suite("HTTP client subscriptions") From bc4f3ae161021f8a4381754515fb78027a7fded2 Mon Sep 17 00:00:00 2001 From: Cristiano Aguzzi Date: Fri, 10 May 2024 09:57:21 +0200 Subject: [PATCH 3/3] docs(binding-http/http-client-impl): correct code comment Co-authored-by: danielpeintner --- packages/binding-http/src/http-client-impl.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/binding-http/src/http-client-impl.ts b/packages/binding-http/src/http-client-impl.ts index c46a364f8..45ed558f2 100644 --- a/packages/binding-http/src/http-client-impl.ts +++ b/packages/binding-http/src/http-client-impl.ts @@ -113,7 +113,7 @@ export default class HttpClient implements ProtocolClient { public async readResource(form: HttpForm): Promise { // See https://www.w3.org/TR/wot-thing-description11/#contentType-usage - // Cases: 1B + // Case: 1B const headers = form.contentType != null ? [["accept", form.contentType]] : [["accept", ContentSerdes.DEFAULT]]; const request = await this.generateFetchRequest(form, "GET", { headers }); debug(`HttpClient (readResource) sending ${request.method} to ${request.url}`);