From d6f52ca65f893fdb581bf06b2ff97b3d6eeeb744 Mon Sep 17 00:00:00 2001 From: Lance Ball Date: Tue, 14 Jun 2022 17:44:06 -0400 Subject: [PATCH] fix: HTTP headers for extensions with false values (#493) * 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 | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/message/http/headers.ts b/src/message/http/headers.ts index e8a9c70..770832f 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 f4f8e21..b1afad4 100644 --- a/test/integration/message_test.ts +++ b/test/integration/message_test.ts @@ -41,6 +41,26 @@ const imageData = new Uint32Array(fs.readFileSync(path.join(process.cwd(), "test const image_base64 = asBase64(imageData); describe("HTTP transport", () => { + + 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); + const message = HTTP.binary(evt); + expect(message.headers.hasOwnProperty("ce-extboolean")).to.equal(true); + 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 = {