From 8418f77a6bc85e7c67993e31bdfcaf89cd29ed37 Mon Sep 17 00:00:00 2001 From: Jingwen Peng Date: Sun, 28 Jul 2019 14:57:41 +0800 Subject: [PATCH 1/3] Support receiving "application/octet-stream" content in binary transport mode Signed-off-by: Jingwen Peng --- lib/bindings/http/constants.js | 7 +-- lib/bindings/http/receiver_binary_0_2.js | 4 ++ lib/bindings/http/unmarshaller_0_2.js | 1 + .../http/receiver_binary_0_2_tests.js | 46 ++++++++++++++++++- 4 files changed, 53 insertions(+), 5 deletions(-) diff --git a/lib/bindings/http/constants.js b/lib/bindings/http/constants.js index aa4f5c0..fafb771 100644 --- a/lib/bindings/http/constants.js +++ b/lib/bindings/http/constants.js @@ -2,9 +2,10 @@ module.exports = { CHARSET_DEFAULT : "utf-8", - MIME_JSON : "application/json", - MIME_CE : "application/cloudevents", - MIME_CE_JSON : "application/cloudevents+json", + MIME_JSON : "application/json", + MIME_OCTET_STREAM : "application/octet-stream", + MIME_CE : "application/cloudevents", + MIME_CE_JSON : "application/cloudevents+json", HEADER_CONTENT_TYPE : "content-type", diff --git a/lib/bindings/http/receiver_binary_0_2.js b/lib/bindings/http/receiver_binary_0_2.js index 8174204..b578c59 100644 --- a/lib/bindings/http/receiver_binary_0_2.js +++ b/lib/bindings/http/receiver_binary_0_2.js @@ -12,9 +12,13 @@ const { const parserByType = {}; parserByType[Constants.MIME_JSON] = new JSONParser(); +parserByType[Constants.MIME_OCTET_STREAM] = { + parse(payload) { return payload; } +}; const allowedContentTypes = []; allowedContentTypes.push(Constants.MIME_JSON); +allowedContentTypes.push(Constants.MIME_OCTET_STREAM); const requiredHeaders = []; requiredHeaders.push(Constants.BINARY_HEADERS_02.TYPE); diff --git a/lib/bindings/http/unmarshaller_0_2.js b/lib/bindings/http/unmarshaller_0_2.js index 8ecce98..d16b60c 100644 --- a/lib/bindings/http/unmarshaller_0_2.js +++ b/lib/bindings/http/unmarshaller_0_2.js @@ -14,6 +14,7 @@ const receiverByBinding = { const allowedBinaryContentTypes = []; allowedBinaryContentTypes.push(Constants.MIME_JSON); +allowedBinaryContentTypes.push(Constants.MIME_OCTET_STREAM); const allowedStructuredContentTypes = []; allowedStructuredContentTypes.push(Constants.MIME_CE_JSON); diff --git a/test/bindings/http/receiver_binary_0_2_tests.js b/test/bindings/http/receiver_binary_0_2_tests.js index 85d79d1..08acfaf 100644 --- a/test/bindings/http/receiver_binary_0_2_tests.js +++ b/test/bindings/http/receiver_binary_0_2_tests.js @@ -285,7 +285,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.2", () => { .to.equal("http://schema.registry/v1"); }); - it("Cloudevent contains 'contenttype'", () => { + it("Cloudevent contains 'contenttype' (application/json)", () => { // setup var payload = { "data" : "dataString" @@ -308,7 +308,28 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.2", () => { .to.equal("application/json"); }); - it("Cloudevent contains 'data'", () => { + it("Cloudevent contains 'contenttype' (application/octet-stream)", () => { + // setup + var payload = "The payload is binary data"; + var attributes = { + "ce-type" : "type", + "ce-specversion" : "0.2", + "ce-source" : "/source", + "ce-id" : "id", + "ce-time" : "2019-06-16T11:42:00Z", + "ce-schemaurl" : "http://schema.registry/v1", + "Content-Type" : "application/octet-stream" + }; + + // act + var actual = receiver.parse(payload, attributes); + + // assert + expect(actual.getContenttype()) + .to.equal("application/json"); + }); + + it("Cloudevent contains 'data' (application/json)", () => { // setup var payload = { "data" : "dataString" @@ -331,6 +352,27 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.2", () => { .to.deep.equal(payload); }); + it("Cloudevent contains 'data' (application/octet-stream)", () => { + // setup + var payload = "The payload is binary data"; + var attributes = { + "ce-type" : "type", + "ce-specversion" : "0.2", + "ce-source" : "/source", + "ce-id" : "id", + "ce-time" : "2019-06-16T11:42:00Z", + "ce-schemaurl" : "http://schema.registry/v1", + "Content-Type" : "application/octet-stream" + }; + + // act + var actual = receiver.parse(payload, attributes); + + // assert + expect(actual.getData()) + .to.deep.equal(payload); + }); + it("No error when all attributes are in place", () => { // setup var payload = { From 4eef9a59c50f0cc9a61f6577436c3ceb1ede04aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabio=20Jos=C3=A9?= Date: Sun, 28 Jul 2019 21:58:03 -0300 Subject: [PATCH 2/3] Fix the test from PR #26: Closes #25 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fabio José --- test/bindings/http/receiver_binary_0_2_tests.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/bindings/http/receiver_binary_0_2_tests.js b/test/bindings/http/receiver_binary_0_2_tests.js index 08acfaf..6725c54 100644 --- a/test/bindings/http/receiver_binary_0_2_tests.js +++ b/test/bindings/http/receiver_binary_0_2_tests.js @@ -326,7 +326,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.2", () => { // assert expect(actual.getContenttype()) - .to.equal("application/json"); + .to.equal("application/octet-stream"); }); it("Cloudevent contains 'data' (application/json)", () => { From a8fcd8c9e787b385ec26a39efd1b8f8cb8863c5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabio=20Jos=C3=A9?= Date: Sun, 28 Jul 2019 22:01:13 -0300 Subject: [PATCH 3/3] Update to version 0.2.6 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fabio José --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 5c19b1e..bd8873d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cloudevents-sdk", - "version": "0.2.5", + "version": "0.2.6", "description": "CloudEvents SDK for JavaScript", "main": "index.js", "scripts": {