Unmarshaller impl and tests
Signed-off-by: Fabio José <fabiojose@gmail.com>
This commit is contained in:
parent
064b0226bd
commit
b4c3846491
|
@ -1,15 +1,16 @@
|
||||||
var Spec02 = require("../../specs/spec_0_2.js");
|
var StructuredReceiver = require("./receiver_structured_0_2.js");
|
||||||
var JSONParser = require("../../formats/json/parser.js");
|
var BinaryReceiver = require("./receiver_binary_0_2.js");
|
||||||
|
|
||||||
const Constants = require("./constants.js");
|
const Constants = require("./constants.js");
|
||||||
|
const Commons = require("./commons.js");
|
||||||
|
|
||||||
const structured = "structured";
|
const structured = "structured";
|
||||||
const binary = "binary";
|
const binary = "binary";
|
||||||
|
|
||||||
const jsonParserSpec02 = new JSONParser(new Spec02());
|
const receiver_by_binding = {
|
||||||
const parsers = {};
|
structured : new StructuredReceiver(),
|
||||||
parsers[Constants.MIME_JSON] = jsonParserSpec02;
|
binary : new BinaryReceiver(),
|
||||||
parsers[Constants.MIME_CE_JSON] = jsonParserSpec02;
|
};
|
||||||
|
|
||||||
const allowed_binary_content_types = [];
|
const allowed_binary_content_types = [];
|
||||||
allowed_binary_content_types.push(Constants.MIME_JSON);
|
allowed_binary_content_types.push(Constants.MIME_JSON);
|
||||||
|
@ -25,10 +26,6 @@ function validate_args(payload, headers) {
|
||||||
if(!headers){
|
if(!headers){
|
||||||
throw {message: "headers is null or undefined"};
|
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?
|
// Is it binary or structured?
|
||||||
|
@ -59,19 +56,20 @@ var Unmarshaller = function() {
|
||||||
Unmarshaller.prototype.unmarshall = function(payload, headers) {
|
Unmarshaller.prototype.unmarshall = function(payload, headers) {
|
||||||
validate_args(payload, headers);
|
validate_args(payload, headers);
|
||||||
|
|
||||||
// Resolve the binding
|
var sanity_headers = Commons.sanity_and_clone(headers);
|
||||||
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
|
|
||||||
|
|
||||||
|
// 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;
|
module.exports = Unmarshaller;
|
||||||
|
|
|
@ -125,4 +125,76 @@ describe("HTTP Transport Binding Unmarshaller", () => {
|
||||||
.to.be.an("object");
|
.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");
|
||||||
|
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue