From 713d323de66f1567b69a0010a81d5fe5c5d5863a Mon Sep 17 00:00:00 2001 From: Lance Ball Date: Tue, 14 Jun 2022 14:59:41 -0400 Subject: [PATCH 1/2] fix: HTTP headers for extensions with false values CloudEvent objects may include extensions that have a defined key and a `false` value. This change ensures that HTTP messages for CloudEvents containing these extension values include the appropriate headers. Signed-off-by: Lance Ball --- src/message/http/headers.ts | 2 +- test/integration/message_test.ts | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/message/http/headers.ts b/src/message/http/headers.ts index e8a9c708..770832f3 100644 --- a/src/message/http/headers.ts +++ b/src/message/http/headers.ts @@ -36,7 +36,7 @@ export function headersFor(event: CloudEventV1): Headers { // iterate over the event properties - generate a header for each Object.getOwnPropertyNames(event).forEach((property) => { const value = event[property]; - if (value) { + if (value !== undefined) { const map: MappedParser | undefined = headerMap[property] as MappedParser; if (map) { headers[map.name] = map.parser.parse(value as string) as string; diff --git a/test/integration/message_test.ts b/test/integration/message_test.ts index f4f8e210..0c10e917 100644 --- a/test/integration/message_test.ts +++ b/test/integration/message_test.ts @@ -41,6 +41,16 @@ const imageData = new Uint32Array(fs.readFileSync(path.join(process.cwd(), "test const image_base64 = asBase64(imageData); describe("HTTP transport", () => { + + it("Includes extensions when type is 'boolean' with a false value", () => { + const evt = new CloudEvent({ source: "test", type: "test", extboolean: false }); + expect(evt.hasOwnProperty("extboolean")).to.equal(true); + expect(evt["extboolean"]).to.equal(false); + const message = HTTP.binary(evt); + expect(message.headers.hasOwnProperty("ce-extboolean")).to.equal(true); + expect(message.headers["ce-extboolean"]).to.equal(false); + }); + it("Handles events with no content-type and no datacontenttype", () => { const body = "{Something[Not:valid}JSON"; const message: Message = { From e67732087947782d66ad54f0c5b4d3decd273900 Mon Sep 17 00:00:00 2001 From: Lance Ball Date: Tue, 14 Jun 2022 15:10:37 -0400 Subject: [PATCH 2/2] fixup: add test for structured mode as well Signed-off-by: Lance Ball --- test/integration/message_test.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/test/integration/message_test.ts b/test/integration/message_test.ts index 0c10e917..b1afad4b 100644 --- a/test/integration/message_test.ts +++ b/test/integration/message_test.ts @@ -42,7 +42,7 @@ const image_base64 = asBase64(imageData); describe("HTTP transport", () => { - it("Includes extensions when type is 'boolean' with a false value", () => { + it("Includes extensions in binary mode when type is 'boolean' with a false value", () => { const evt = new CloudEvent({ source: "test", type: "test", extboolean: false }); expect(evt.hasOwnProperty("extboolean")).to.equal(true); expect(evt["extboolean"]).to.equal(false); @@ -51,6 +51,16 @@ describe("HTTP transport", () => { expect(message.headers["ce-extboolean"]).to.equal(false); }); + it("Includes extensions in structured when type is 'boolean' with a false value", () => { + const evt = new CloudEvent({ source: "test", type: "test", extboolean: false }); + expect(evt.hasOwnProperty("extboolean")).to.equal(true); + expect(evt["extboolean"]).to.equal(false); + const message = HTTP.structured(evt); + const body = JSON.parse(message.body as string); + expect(body.hasOwnProperty("extboolean")).to.equal(true); + expect(body.extboolean).to.equal(false); + }); + it("Handles events with no content-type and no datacontenttype", () => { const body = "{Something[Not:valid}JSON"; const message: Message = {