diff --git a/lib/bindings/http/unmarshaller_0_2.js b/lib/bindings/http/unmarshaller_0_2.js index b1024e1..1108086 100644 --- a/lib/bindings/http/unmarshaller_0_2.js +++ b/lib/bindings/http/unmarshaller_0_2.js @@ -1,15 +1,16 @@ -var Spec02 = require("../../specs/spec_0_2.js"); -var JSONParser = require("../../formats/json/parser.js"); +var StructuredReceiver = require("./receiver_structured_0_2.js"); +var BinaryReceiver = require("./receiver_binary_0_2.js"); const Constants = require("./constants.js"); +const Commons = require("./commons.js"); const structured = "structured"; const binary = "binary"; -const jsonParserSpec02 = new JSONParser(new Spec02()); -const parsers = {}; -parsers[Constants.MIME_JSON] = jsonParserSpec02; -parsers[Constants.MIME_CE_JSON] = jsonParserSpec02; +const receiver_by_binding = { + structured : new StructuredReceiver(), + binary : new BinaryReceiver(), +}; const allowed_binary_content_types = []; allowed_binary_content_types.push(Constants.MIME_JSON); @@ -25,10 +26,6 @@ function validate_args(payload, headers) { if(!headers){ throw {message: "headers is null or undefined"}; } - - if(!headers[Constants.HEADER_CONTENT_TYPE]){ - throw {message: "content-type header not found"}; - } } // Is it binary or structured? @@ -59,19 +56,20 @@ var Unmarshaller = function() { Unmarshaller.prototype.unmarshall = function(payload, headers) { validate_args(payload, headers); - // Resolve the binding - var bindingName = resolve_binding_name(payload, headers); - var contentType = headers[Constants.HEADER_CONTENT_TYPE]; - - if(bindingName === structured){ - var parser = parsers[contentType]; - var cloudevent = parser.parse(payload); - - return cloudevent; - } else {//binary + var sanity_headers = Commons.sanity_and_clone(headers); + // Validation level 1 + if(!sanity_headers[Constants.HEADER_CONTENT_TYPE]){ + throw {message: "content-type header not found"}; } + // Resolve the binding + var binding_name = resolve_binding_name(payload, sanity_headers); + + var cloudevent = + receiver_by_binding[binding_name].parse(payload, sanity_headers); + + return cloudevent; } module.exports = Unmarshaller; diff --git a/test/bindings/http/unmarshaller_0_2_tests.js b/test/bindings/http/unmarshaller_0_2_tests.js index f20d920..3760124 100644 --- a/test/bindings/http/unmarshaller_0_2_tests.js +++ b/test/bindings/http/unmarshaller_0_2_tests.js @@ -125,4 +125,76 @@ describe("HTTP Transport Binding Unmarshaller", () => { .to.be.an("object"); }); }); + + describe("Binary", () => { + it("Throw error when has not allowed mime", () => { + // setup + 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" : "text/html" + }; + + var un = new Unmarshaller(); + + // act and assert + expect(un.unmarshall.bind(un, payload, attributes)) + .to.throw("content type not allowed"); + }); + + it("Throw error when the event does not follow the spec 0.2", () => { + // setup + var payload = { + "data" : "dataString" + }; + var attributes = { + "ce-type" : "type", + "CE-CloudEventsVersion" : "0.1", + "ce-source" : "source", + "ce-id" : "id", + "ce-time" : "2019-06-16T11:42:00Z", + "ce-schemaurl" : "http://schema.registry/v1", + "Content-Type" : "application/json" + }; + + var un = new Unmarshaller(); + + // act and assert + expect(un.unmarshall.bind(un, payload, attributes)) + .to.throw(); + }); + + it("No error when all attributes are in place", () => { + // setup + 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" + }; + + var un = new Unmarshaller(); + + // act + var actual = un.unmarshall(payload, attributes); + + // assert + expect(actual) + .to.be.an("object"); + + }); + }); });