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 <lball@redhat.com>
This commit is contained in:
Lance Ball 2022-06-14 17:44:06 -04:00 committed by GitHub
parent ce02e0a1f3
commit d6f52ca65f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 1 deletions

View File

@ -36,7 +36,7 @@ export function headersFor<T>(event: CloudEventV1<T>): 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;

View File

@ -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<undefined> = {