From 9ccfaf25baa871f680ae755b6a2d8334aef4c517 Mon Sep 17 00:00:00 2001 From: Lance Ball Date: Tue, 5 May 2020 18:02:39 -0400 Subject: [PATCH 1/3] 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 Signed-off-by: Grant Timmerman --- lib/bindings/http/receiver_binary.js | 5 +++++ test/bindings/http/receiver_binary_0_3_tests.js | 14 ++++++++++++++ test/bindings/http/receiver_binary_1_tests.js | 14 ++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/lib/bindings/http/receiver_binary.js b/lib/bindings/http/receiver_binary.js index 0aecc2ec..71a0c80c 100644 --- a/lib/bindings/http/receiver_binary.js +++ b/lib/bindings/http/receiver_binary.js @@ -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])) { diff --git a/test/bindings/http/receiver_binary_0_3_tests.js b/test/bindings/http/receiver_binary_0_3_tests.js index 86467c14..89580cf1 100644 --- a/test/bindings/http/receiver_binary_0_3_tests.js +++ b/test/bindings/http/receiver_binary_0_3_tests.js @@ -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", () => { diff --git a/test/bindings/http/receiver_binary_1_tests.js b/test/bindings/http/receiver_binary_1_tests.js index 6d778150..9ee2444b 100644 --- a/test/bindings/http/receiver_binary_1_tests.js +++ b/test/bindings/http/receiver_binary_1_tests.js @@ -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 = {}; From 28a9bf011ce33d55e59d7c59997b5f912e353bc9 Mon Sep 17 00:00:00 2001 From: Grant Timmerman Date: Tue, 5 May 2020 22:29:16 -0700 Subject: [PATCH 2/3] refactor: remove ext folder Signed-off-by: Grant Timmerman --- ext/spec_0_3.json | 88 ------------------------------------------- ext/spec_1.json | 80 --------------------------------------- lib/specs/spec_0_3.js | 87 +++++++++++++++++++++++++++++++++++++++++- lib/specs/spec_1.js | 81 ++++++++++++++++++++++++++++++++++++++- 4 files changed, 166 insertions(+), 170 deletions(-) delete mode 100644 ext/spec_0_3.json delete mode 100644 ext/spec_1.json diff --git a/ext/spec_0_3.json b/ext/spec_0_3.json deleted file mode 100644 index 2c362698..00000000 --- a/ext/spec_0_3.json +++ /dev/null @@ -1,88 +0,0 @@ -{ - "$ref": "#/definitions/event", - "definitions": { - "specversion": { - "type": "string", - "minLength": 1, - "const": "0.3" - }, - "datacontenttype": { - "type": "string" - }, - "data": { - "type": [ - "object", - "string" - ] - }, - "event": { - "properties": { - "specversion": { - "$ref": "#/definitions/specversion" - }, - "datacontenttype": { - "$ref": "#/definitions/datacontenttype" - }, - "data": { - "$ref": "#/definitions/data" - }, - "id": { - "$ref": "#/definitions/id" - }, - "time": { - "$ref": "#/definitions/time" - }, - "schemaurl": { - "$ref": "#/definitions/schemaurl" - }, - "subject": { - "$ref": "#/definitions/subject" - }, - "type": { - "$ref": "#/definitions/type" - }, - "extensions": { - "$ref": "#/definitions/extensions" - }, - "source": { - "$ref": "#/definitions/source" - } - }, - "required": [ - "specversion", - "id", - "type", - "source" - ], - "type": "object" - }, - "id": { - "type": "string", - "minLength": 1 - }, - "time": { - "format": "date-time", - "type": "string" - }, - "schemaurl": { - "type": "string", - "format": "uri-reference" - }, - "subject": { - "type": "string", - "minLength": 1 - }, - "type": { - "type": "string", - "minLength": 1 - }, - "extensions": { - "type": "object" - }, - "source": { - "format": "uri-reference", - "type": "string" - } - }, - "type": "object" -} diff --git a/ext/spec_1.json b/ext/spec_1.json deleted file mode 100644 index 50e1a607..00000000 --- a/ext/spec_1.json +++ /dev/null @@ -1,80 +0,0 @@ -{ - "$ref": "#/definitions/event", - "definitions": { - "specversion": { - "type": "string", - "minLength": 1, - "const": "1.0" - }, - "datacontenttype": { - "type": "string" - }, - "data": { - "type": ["object", "string"] - }, - "data_base64": { - "type": "string" - }, - "event": { - "properties": { - "specversion": { - "$ref": "#/definitions/specversion" - }, - "datacontenttype": { - "$ref": "#/definitions/datacontenttype" - }, - "data": { - "$ref": "#/definitions/data" - }, - "data_base64": { - "$ref": "#/definitions/data_base64" - }, - "id": { - "$ref": "#/definitions/id" - }, - "time": { - "$ref": "#/definitions/time" - }, - "dataschema": { - "$ref": "#/definitions/dataschema" - }, - "subject": { - "$ref": "#/definitions/subject" - }, - "type": { - "$ref": "#/definitions/type" - }, - "source": { - "$ref": "#/definitions/source" - } - }, - "required": ["specversion", "id", "type", "source"], - "type": "object" - }, - "id": { - "type": "string", - "minLength": 1 - }, - "time": { - "format": "date-time", - "type": "string" - }, - "dataschema": { - "type": "string", - "format": "uri" - }, - "subject": { - "type": "string", - "minLength": 1 - }, - "type": { - "type": "string", - "minLength": 1 - }, - "source": { - "format": "uri-reference", - "type": "string" - } - }, - "type": "object" -} diff --git a/lib/specs/spec_0_3.js b/lib/specs/spec_0_3.js index 66752952..f07e0fb1 100644 --- a/lib/specs/spec_0_3.js +++ b/lib/specs/spec_0_3.js @@ -25,7 +25,92 @@ SUPPORTED_CONTENT_ENCODING.base64 = { check: (data) => isBase64(data) }; -const schema = require("../../ext/spec_0_3.json"); +const schema = { + $ref: "#/definitions/event", + definitions: { + specversion: { + const: "0.3" + }, + datacontenttype: { + type: "string" + }, + data: { + type: [ + "object", + "string" + ] + }, + event: { + properties: { + specversion: { + $ref: "#/definitions/specversion" + }, + datacontenttype: { + $ref: "#/definitions/datacontenttype" + }, + data: { + $ref: "#/definitions/data" + }, + id: { + $ref: "#/definitions/id" + }, + time: { + $ref: "#/definitions/time" + }, + schemaurl: { + $ref: "#/definitions/schemaurl" + }, + subject: { + $ref: "#/definitions/subject" + }, + type: { + $ref: "#/definitions/type" + }, + extensions: { + $ref: "#/definitions/extensions" + }, + source: { + $ref: "#/definitions/source" + } + }, + required: [ + "specversion", + "id", + "type", + "source" + ], + type: "object" + }, + id: { + type: "string", + minLength: 1 + }, + time: { + format: "date-time", + type: "string" + }, + schemaurl: { + type: "string", + format: "uri-reference" + }, + subject: { + type: "string", + minLength: 1 + }, + type: { + type: "string", + minLength: 1 + }, + extensions: { + type: "object" + }, + source: { + format: "uri-reference", + type: "string" + } + }, + type: "object" +}; const ajv = new Ajv({ extendRefs: true diff --git a/lib/specs/spec_1.js b/lib/specs/spec_1.js index 89e13b49..6415d15e 100644 --- a/lib/specs/spec_1.js +++ b/lib/specs/spec_1.js @@ -27,7 +27,86 @@ const RESERVED_ATTRIBUTES = { data_base64: "data_base64" }; -const schema = require("../../ext/spec_1.json"); +const schema = { + $ref: "#/definitions/event", + definitions: { + specversion: { + type: "string", + minLength: 1, + const: "1.0" + }, + datacontenttype: { + type: "string" + }, + data: { + type: ["object", "string"] + }, + data_base64: { + type: "string" + }, + event: { + properties: { + specversion: { + $ref: "#/definitions/specversion" + }, + datacontenttype: { + $ref: "#/definitions/datacontenttype" + }, + data: { + $ref: "#/definitions/data" + }, + data_base64: { + $ref: "#/definitions/data_base64" + }, + id: { + $ref: "#/definitions/id" + }, + time: { + $ref: "#/definitions/time" + }, + dataschema: { + $ref: "#/definitions/dataschema" + }, + subject: { + $ref: "#/definitions/subject" + }, + type: { + $ref: "#/definitions/type" + }, + source: { + $ref: "#/definitions/source" + } + }, + required: ["specversion", "id", "type", "source"], + type: "object" + }, + id: { + type: "string", + minLength: 1 + }, + time: { + format: "date-time", + type: "string" + }, + dataschema: { + type: "string", + format: "uri" + }, + subject: { + type: "string", + minLength: 1 + }, + type: { + type: "string", + minLength: 1 + }, + source: { + format: "uri-reference", + type: "string" + } + }, + type: "object" +}; const ajv = new Ajv({ extendRefs: true From b147becb791f5ff74fe926e3d291a9fa20a64243 Mon Sep 17 00:00:00 2001 From: Grant Timmerman Date: Wed, 6 May 2020 11:59:48 -0700 Subject: [PATCH 3/3] Revert "fix: make application/json the default content type in binary mode (#118)" This reverts commit 9ccfaf25baa871f680ae755b6a2d8334aef4c517. Signed-off-by: Grant Timmerman --- lib/bindings/http/receiver_binary.js | 5 ----- test/bindings/http/receiver_binary_0_3_tests.js | 14 -------------- test/bindings/http/receiver_binary_1_tests.js | 14 -------------- 3 files changed, 33 deletions(-) diff --git a/lib/bindings/http/receiver_binary.js b/lib/bindings/http/receiver_binary.js index 71a0c80c..0aecc2ec 100644 --- a/lib/bindings/http/receiver_binary.js +++ b/lib/bindings/http/receiver_binary.js @@ -52,11 +52,6 @@ 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])) { diff --git a/test/bindings/http/receiver_binary_0_3_tests.js b/test/bindings/http/receiver_binary_0_3_tests.js index 89580cf1..86467c14 100644 --- a/test/bindings/http/receiver_binary_0_3_tests.js +++ b/test/bindings/http/receiver_binary_0_3_tests.js @@ -144,20 +144,6 @@ 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", () => { diff --git a/test/bindings/http/receiver_binary_1_tests.js b/test/bindings/http/receiver_binary_1_tests.js index 9ee2444b..6d778150 100644 --- a/test/bindings/http/receiver_binary_1_tests.js +++ b/test/bindings/http/receiver_binary_1_tests.js @@ -130,20 +130,6 @@ 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 = {};