fix: make application/json the default content type in binary mode (#118)

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: https://github.com/cloudevents/sdk-javascript/issues/117
Ref: https://github.com/cloudevents/spec/issues/614

Signed-off-by: Lance Ball <lball@redhat.com>
This commit is contained in:
Lance Ball 2020-05-05 18:02:39 -04:00 committed by GitHub
parent 79ec3ef126
commit d9e9ae6bdc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 0 deletions

View File

@ -52,6 +52,11 @@ BinaryHTTPReceiver.prototype.check = function(payload, headers) {
// Clone and low case all headers names
const sanityHeaders = Commons.sanityAndClone(headers);
// If no content type is provided, default to application/json
if (!sanityHeaders[Constants.HEADER_CONTENT_TYPE]) {
sanityHeaders[Constants.HEADER_CONTENT_TYPE] = Constants.MIME_JSON;
}
// Validation Level 1
if (!this.allowedContentTypes
.includes(sanityHeaders[Constants.HEADER_CONTENT_TYPE])) {

View File

@ -144,6 +144,20 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.3", () => {
expect(receiver.check.bind(receiver, payload, attributes))
.to.not.throw();
});
it("No error when content-type is unspecified", () => {
const payload = {};
const attributes = {
"ce-type": "type",
"ce-specversion": "0.3",
"ce-source": "source",
"ce-id": "id"
};
// act and assert
expect(receiver.check.bind(receiver, payload, attributes))
.to.not.throw();
});
});
describe("Parse", () => {

View File

@ -130,6 +130,20 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v1.0", () => {
.to.throw("invalid content type");
});
it("No error when content-type is unspecified", () => {
const payload = {};
const attributes = {
"ce-type": "type",
"ce-specversion": "1.0",
"ce-source": "source",
"ce-id": "id"
};
// act and assert
expect(receiver.check.bind(receiver, payload, attributes))
.to.not.throw();
});
it("No error when all required headers are in place", () => {
// setup
const payload = {};