Skip to content

Commit d6f52ca

Browse files
authored
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 <[email protected]>
1 parent ce02e0a commit d6f52ca

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/message/http/headers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export function headersFor<T>(event: CloudEventV1<T>): Headers {
3636
// iterate over the event properties - generate a header for each
3737
Object.getOwnPropertyNames(event).forEach((property) => {
3838
const value = event[property];
39-
if (value) {
39+
if (value !== undefined) {
4040
const map: MappedParser | undefined = headerMap[property] as MappedParser;
4141
if (map) {
4242
headers[map.name] = map.parser.parse(value as string) as string;

test/integration/message_test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,26 @@ const imageData = new Uint32Array(fs.readFileSync(path.join(process.cwd(), "test
4141
const image_base64 = asBase64(imageData);
4242

4343
describe("HTTP transport", () => {
44+
45+
it("Includes extensions in binary mode when type is 'boolean' with a false value", () => {
46+
const evt = new CloudEvent({ source: "test", type: "test", extboolean: false });
47+
expect(evt.hasOwnProperty("extboolean")).to.equal(true);
48+
expect(evt["extboolean"]).to.equal(false);
49+
const message = HTTP.binary(evt);
50+
expect(message.headers.hasOwnProperty("ce-extboolean")).to.equal(true);
51+
expect(message.headers["ce-extboolean"]).to.equal(false);
52+
});
53+
54+
it("Includes extensions in structured when type is 'boolean' with a false value", () => {
55+
const evt = new CloudEvent({ source: "test", type: "test", extboolean: false });
56+
expect(evt.hasOwnProperty("extboolean")).to.equal(true);
57+
expect(evt["extboolean"]).to.equal(false);
58+
const message = HTTP.structured(evt);
59+
const body = JSON.parse(message.body as string);
60+
expect(body.hasOwnProperty("extboolean")).to.equal(true);
61+
expect(body.extboolean).to.equal(false);
62+
});
63+
4464
it("Handles events with no content-type and no datacontenttype", () => {
4565
const body = "{Something[Not:valid}JSON";
4666
const message: Message<undefined> = {

0 commit comments

Comments
 (0)