diff --git a/lib/bindings/http/unmarshaller_0_2.js b/lib/bindings/http/unmarshaller_0_2.js index 31cf543..49fc02f 100644 --- a/lib/bindings/http/unmarshaller_0_2.js +++ b/lib/bindings/http/unmarshaller_0_2.js @@ -1,10 +1,8 @@ var Spec02 = require("../../specs/spec_0_2.js"); var JSONParser = require("../../formats/json/parser.js"); -const json_mime = "application/json"; -const ce_json_mime = "application/cloudevents+json"; +const Constants = require("./constants.js"); -const content_type_header = "content-type"; const ce_structured_content_type = "application/cloudevents"; const structured = "structured"; @@ -12,14 +10,14 @@ const binary = "binary"; const jsonParserSpec02 = new JSONParser(new Spec02()); const parsers = {}; -parsers[json_mime] = jsonParserSpec02; -parsers[ce_json_mime] = jsonParserSpec02; +parsers[Constants.MIME_JSON] = jsonParserSpec02; +parsers[Constants.MIME_CE_JSON] = jsonParserSpec02; const allowed_binary_content_types = []; -allowed_binary_content_types.push(json_mime); +allowed_binary_content_types.push(Constants.MIME_JSON); const allowed_structured_content_types = []; -allowed_structured_content_types.push(ce_json_mime); +allowed_structured_content_types.push(Constants.MIME_CE_JSON); function validate_args(payload, headers) { if(!payload){ @@ -30,7 +28,7 @@ function validate_args(payload, headers) { throw {message: "headers is null or undefined"}; } - if(!headers[content_type_header]){ + if(!headers[Constants.HEADER_CONTENT_TYPE]){ throw {message: "content-type header not found"}; } } @@ -38,9 +36,10 @@ function validate_args(payload, headers) { // Is it binary or structured? function resolve_binding_name(payload, headers) { - var contentType = headers[content_type_header]; + var contentType = headers[Constants.HEADER_CONTENT_TYPE]; if(contentType.startsWith(ce_structured_content_type)){ // Structured + console.log(allowed_structured_content_types); if(allowed_structured_content_types.includes(contentType)){ return structured; } else { @@ -65,7 +64,7 @@ Unmarshaller.prototype.unmarshall = function(payload, headers) { // Resolve the binding var bindingName = resolve_binding_name(payload, headers); - var contentType = headers[content_type_header]; + var contentType = headers[Constants.HEADER_CONTENT_TYPE]; if(bindingName === structured){ var parser = parsers[contentType];