Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/transport/receiver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Headers } from "./http/headers";
import { Headers, sanitize } from "./http/headers";
import { CloudEvent, Version, ValidationError } from "..";
import { BinaryHTTPReceiver as BinaryReceiver } from "./http/binary_receiver";
import { StructuredHTTPReceiver as StructuredReceiver } from "./http/structured_receiver";
Expand Down Expand Up @@ -60,8 +60,9 @@ export class Receiver {
* @return {CloudEvent} A new {CloudEvent} instance
*/
accept(headers: Headers, body: string | Record<string, unknown> | CloudEventV1 | CloudEventV03): CloudEvent {
const mode: Mode = getMode(headers);
const version = getVersion(mode, headers, body);
const cleanHeaders: Headers = sanitize(headers);
const mode: Mode = getMode(cleanHeaders);
const version = getVersion(mode, cleanHeaders, body);
switch (version) {
case Version.V1:
return this.receivers.v1[mode].parse(body, headers);
Expand Down
29 changes: 29 additions & 0 deletions test/http_receiver_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,35 @@ describe("HTTP Transport Binding Receiver for CloudEvents", () => {
expect(typeof event.data).to.equal("object");
expect((event.data as Record<string, string>).lunch).to.equal("sushi");
});

it("Recognizes headers in title case for binary events", () => {
const binaryHeaders = {
"Content-Type": "application/json; charset=utf-8",
"ce-specversion": specversion,
"ce-id": id,
"ce-type": type,
"ce-source": source,
};

const event: CloudEvent = receiver.accept(binaryHeaders, data);
expect(event.validate()).to.be.true;
expect((event.data as Record<string, string>).lunch).to.equal("sushi");
});

it("Recognizes headers in title case for structured events", () => {
const structuredHeaders = { "Content-Type": "application/cloudevents+json" };
const payload = {
id,
type,
source,
data,
specversion,
};

const event: CloudEvent = receiver.accept(structuredHeaders, payload);
expect(event.validate()).to.be.true;
expect((event.data as Record<string, string>).lunch).to.equal("sushi");
});
});

describe("V1", () => {
Expand Down