From 67b3ac2b214e771d51cca294fbde779bb1d8b337 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabio=20Jos=C3=A9?= Date: Tue, 25 Jun 2019 10:42:26 -0300 Subject: [PATCH] Processing the extensions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fabio José --- lib/bindings/http/receiver_binary_0_2.js | 24 ++++++++++++++-- .../http/receiver_binary_0_2_tests.js | 28 +++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/lib/bindings/http/receiver_binary_0_2.js b/lib/bindings/http/receiver_binary_0_2.js index 9634c8e..6d1abd2 100644 --- a/lib/bindings/http/receiver_binary_0_2.js +++ b/lib/bindings/http/receiver_binary_0_2.js @@ -22,6 +22,10 @@ setter_reflections[Constants.BINARY_HEADERS_02.TYPE] = { name : "type", parser : (v) => v }; +setter_reflections[Constants.BINARY_HEADERS_02.SPEC_VERSION] = { + name : "specversion", + parser : (v) => "0.2" +}; setter_reflections[Constants.BINARY_HEADERS_02.SOURCE] = { name : "source", parser: (v) => v @@ -102,6 +106,7 @@ Receiver.prototype.parse = function(payload, headers) { // Clone and low case all headers names var sanity_headers = Commons.sanity_and_clone(headers); + var processed_headers = []; var cloudevent = new Cloudevent(Spec02); for(header in setter_reflections) { // dont worry, check() have seen what was required or not @@ -111,16 +116,31 @@ Receiver.prototype.parse = function(payload, headers) { // invoke the setter function cloudevent[setter_name](parser_fn(sanity_headers[header])); + + // to use ahead, for extensions processing + processed_headers.push(header); } } // Parses the payload - var parsedPayload = + var parsed_payload = parser_by_type[sanity_headers[Constants.HEADER_CONTENT_TYPE]] .parse(payload); + // Every unprocessed header should be an extension + Array.from(Object.keys(sanity_headers)) + .filter(value => !processed_headers.includes(value)) + .filter(value => + value.startsWith(Constants.BINARY_HEADERS_02.EXTENSIONS_PREFIX)) + .map(extension => + extension.substring(Constants.BINARY_HEADERS_02.EXTENSIONS_PREFIX.length) + ).forEach(extension => + cloudevent.addExtension(extension, + sanity_headers[Constants.BINARY_HEADERS_02.EXTENSIONS_PREFIX+extension]) + ); + // Sets the data - cloudevent.data(parsedPayload); + cloudevent.data(parsed_payload); // Checks the event spec cloudevent.format(); diff --git a/test/bindings/http/receiver_binary_0_2_tests.js b/test/bindings/http/receiver_binary_0_2_tests.js index 1323cba..85d79d1 100644 --- a/test/bindings/http/receiver_binary_0_2_tests.js +++ b/test/bindings/http/receiver_binary_0_2_tests.js @@ -353,6 +353,34 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.2", () => { expect(actual) .to.be.an("object"); + expect(actual) + .to.have.property("format"); + }); + + it("Should accept 'extension1'", () => { + // setup + var extension1 = "mycuston-ext1"; + var payload = { + "data" : "dataString" + }; + 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/json", + "ce-extension1" : extension1 + }; + + // act + var actual = receiver.parse(payload, attributes); + var actualExtensions = actual.getExtensions(); + + // assert + expect(actualExtensions["extension1"]) + .to.equal(extension1); }); }); });