diff --git a/lib/bindings/http/unmarshaller_0_2.js b/lib/bindings/http/unmarshaller_0_2.js index 6fdea1e..760aa95 100644 --- a/lib/bindings/http/unmarshaller_0_2.js +++ b/lib/bindings/http/unmarshaller_0_2.js @@ -2,26 +2,45 @@ const content_type_header = "content-type"; const ce_structured_content_type = "application/cloudevents"; -/** - * Level 0 of validation: is that string? is that JSON? - */ -function validate_and_parse_as_json(payload) { - var json = payload; +const allowed_binary_content_types = []; +allowed_binary_content_types.push("application/json"); - if( (typeof payload) === "string"){ - try { - json = JSON.parse(payload); +const allowed_structured_content_types = []; +allowed_structured_content_types.push("application/cloudevents+json"); - }catch(e) { - throw {message: "Invalid payload", errors: e}; - } - - } else if( (typeof payload) !== "object"){ - // anything else - throw {message: "Invalid payload type, allowed are: string or object"}; +function validate_args(payload, headers) { + if(!payload){ + throw {message: "payload is null or undefined"}; } - return json; + if(!headers){ + throw {message: "headers is null or undefined"}; + } + + if(!headers[content_type_header]){ + throw {message: "content-type header not found"}; + } +} + +// Is it binary or structured? +function resolve_binding_type(payload, headers) { + + var contentType = headers[content_type_header]; + if(contentType.startsWith(ce_structured_content_type)){ + // Structured + if(allowed_structured_content_types.includes(contentType)){ + + } else { + throw {message: "structured+type not allowed", errors: [contentType]}; + } + } else { + // Binary + if(allowed_binary_content_types.includes(contentType)){ + + } else { + throw {message: "content type not allowed", errors : [contentType]}; + } + } } var Unmarshaller = function() { @@ -29,11 +48,10 @@ var Unmarshaller = function() { } Unmarshaller.prototype.unmarshall = function(payload, headers) { + validate_args(payload, headers); - var valid0 = validate_and_parse_as_json(payload); - - //TODO binary or structured ? - //"Content-Type" + // Resolve the binding + var binding = resolve_binding_type(payload, headers); } diff --git a/test/bindings/http/unmarshaller_0_2_tests.js b/test/bindings/http/unmarshaller_0_2_tests.js index 5dca1cf..0dddeba 100644 --- a/test/bindings/http/unmarshaller_0_2_tests.js +++ b/test/bindings/http/unmarshaller_0_2_tests.js @@ -1,15 +1,63 @@ var expect = require("chai").expect; var Unmarshaller = require("../../../lib/bindings/http/unmarshaller_0_2.js"); -describe("HTTP Unmarshaller - Version 0.2", () => { +describe("HTTP Transport Binding Unmarshaller", () => { - it("Throw error when payload is not string or object", () => { + it("Throw error when payload is null", () => { // setup - var payload = 83; - var unmarshaller = new Unmarshaller(); + var payload = null; + var un = new Unmarshaller(); // act and assert - expect(unmarshaller.unmarshall.bind(payload)) - .to.throw("Invalid payload type, allowed are: string or object"); + expect(un.unmarshall.bind(un, payload)) + .to.throw("payload is null or undefined"); + }); + + it("Throw error when headers is null", () => { + // setup + var payload = {}; + var headers = null; + var un = new Unmarshaller(); + + // act and assert + expect(un.unmarshall.bind(un, payload, headers)) + .to.throw("headers is null or undefined"); + }); + + it("Throw error when there is no content-type header", () => { + // setup + var payload = {}; + var headers = {}; + var un = new Unmarshaller(); + + // act and assert + expect(un.unmarshall.bind(un, payload, headers)) + .to.throw("content-type header not found"); + }); + + it("Throw error when content-type is not allowed", () => { + // setup + var payload = {}; + var headers = { + "content-type":"text/xml" + }; + var un = new Unmarshaller(); + + // act and assert + expect(un.unmarshall.bind(un, payload, headers)) + .to.throw("content type not allowed"); + }); + + it("Throw error when structured binding has not allowed mime", () => { + // setup + var payload = {}; + var headers = { + "content-type":"application/cloudevents+zip" + }; + var un = new Unmarshaller(); + + // act and assert + expect(un.unmarshall.bind(un, payload, headers)) + .to.throw("structured+type not allowed"); }); });