From 8418f77a6bc85e7c67993e31bdfcaf89cd29ed37 Mon Sep 17 00:00:00 2001 From: Jingwen Peng Date: Sun, 28 Jul 2019 14:57:41 +0800 Subject: [PATCH] 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 = {