Skip to content

Commit 5b747ee

Browse files
committed
fix: make application/json the default content type in binary mode
The Knative Kafka event source does not include a `Content-Type` header when sending binary events. The CE HTTP binding specification doesn't address how a receiver should handle this situation. This commit makes `application/json` the default. Fixes: cloudevents#117 Ref: cloudevents/spec#614 Signed-off-by: Lance Ball <[email protected]>
1 parent cd6a3ee commit 5b747ee

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

lib/bindings/http/receiver_binary.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ BinaryHTTPReceiver.prototype.check = function(payload, headers) {
5252
// Clone and low case all headers names
5353
const sanityHeaders = Commons.sanityAndClone(headers);
5454

55+
// If no content type is provided, default to application/json
56+
if (!sanityHeaders[Constants.HEADER_CONTENT_TYPE]) {
57+
sanityHeaders[Constants.HEADER_CONTENT_TYPE] = Constants.MIME_JSON;
58+
}
59+
5560
// Validation Level 1
5661
if (!this.allowedContentTypes
5762
.includes(sanityHeaders[Constants.HEADER_CONTENT_TYPE])) {

test/bindings/http/receiver_binary_0_3_tests.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,20 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.3", () => {
144144
expect(receiver.check.bind(receiver, payload, attributes))
145145
.to.not.throw();
146146
});
147+
148+
it("No error when content-type is unspecified", () => {
149+
const payload = {};
150+
const attributes = {
151+
"ce-type": "type",
152+
"ce-specversion": "0.3",
153+
"ce-source": "source",
154+
"ce-id": "id"
155+
};
156+
157+
// act and assert
158+
expect(receiver.check.bind(receiver, payload, attributes))
159+
.to.not.throw();
160+
});
147161
});
148162

149163
describe("Parse", () => {

test/bindings/http/receiver_binary_1_tests.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,20 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v1.0", () => {
130130
.to.throw("invalid content type");
131131
});
132132

133+
it("No error when content-type is unspecified", () => {
134+
const payload = {};
135+
const attributes = {
136+
"ce-type": "type",
137+
"ce-specversion": "1.0",
138+
"ce-source": "source",
139+
"ce-id": "id"
140+
};
141+
142+
// act and assert
143+
expect(receiver.check.bind(receiver, payload, attributes))
144+
.to.not.throw();
145+
});
146+
133147
it("No error when all required headers are in place", () => {
134148
// setup
135149
const payload = {};

0 commit comments

Comments
 (0)