Unmarshall for structured
Signed-off-by: Fabio José <fabiojose@gmail.com>
This commit is contained in:
parent
7bf643491f
commit
c84f6a5e37
|
@ -1,12 +1,25 @@
|
||||||
|
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 content_type_header = "content-type";
|
const content_type_header = "content-type";
|
||||||
const ce_structured_content_type = "application/cloudevents";
|
const ce_structured_content_type = "application/cloudevents";
|
||||||
|
|
||||||
|
const structured = "structured";
|
||||||
|
const binary = "binary";
|
||||||
|
|
||||||
|
const jsonParserSpec02 = new JSONParser(new Spec02());
|
||||||
|
const parsers = {};
|
||||||
|
parsers[json_mime] = jsonParserSpec02;
|
||||||
|
parsers[ce_json_mime] = jsonParserSpec02;
|
||||||
|
|
||||||
const allowed_binary_content_types = [];
|
const allowed_binary_content_types = [];
|
||||||
allowed_binary_content_types.push("application/json");
|
allowed_binary_content_types.push(json_mime);
|
||||||
|
|
||||||
const allowed_structured_content_types = [];
|
const allowed_structured_content_types = [];
|
||||||
allowed_structured_content_types.push("application/cloudevents+json");
|
allowed_structured_content_types.push(ce_json_mime);
|
||||||
|
|
||||||
function validate_args(payload, headers) {
|
function validate_args(payload, headers) {
|
||||||
if(!payload){
|
if(!payload){
|
||||||
|
@ -23,20 +36,20 @@ function validate_args(payload, headers) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Is it binary or structured?
|
// Is it binary or structured?
|
||||||
function resolve_binding_type(payload, headers) {
|
function resolve_binding_name(payload, headers) {
|
||||||
|
|
||||||
var contentType = headers[content_type_header];
|
var contentType = headers[content_type_header];
|
||||||
if(contentType.startsWith(ce_structured_content_type)){
|
if(contentType.startsWith(ce_structured_content_type)){
|
||||||
// Structured
|
// Structured
|
||||||
if(allowed_structured_content_types.includes(contentType)){
|
if(allowed_structured_content_types.includes(contentType)){
|
||||||
|
return structured;
|
||||||
} else {
|
} else {
|
||||||
throw {message: "structured+type not allowed", errors: [contentType]};
|
throw {message: "structured+type not allowed", errors: [contentType]};
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Binary
|
// Binary
|
||||||
if(allowed_binary_content_types.includes(contentType)){
|
if(allowed_binary_content_types.includes(contentType)){
|
||||||
|
return binary;
|
||||||
} else {
|
} else {
|
||||||
throw {message: "content type not allowed", errors : [contentType]};
|
throw {message: "content type not allowed", errors : [contentType]};
|
||||||
}
|
}
|
||||||
|
@ -51,7 +64,17 @@ Unmarshaller.prototype.unmarshall = function(payload, headers) {
|
||||||
validate_args(payload, headers);
|
validate_args(payload, headers);
|
||||||
|
|
||||||
// Resolve the binding
|
// Resolve the binding
|
||||||
var binding = resolve_binding_type(payload, headers);
|
var bindingName = resolve_binding_name(payload, headers);
|
||||||
|
var contentType = headers[content_type_header];
|
||||||
|
|
||||||
|
if(bindingName === structured){
|
||||||
|
var parser = parsers[contentType];
|
||||||
|
var cloudevent = parser.parse(payload);
|
||||||
|
|
||||||
|
return cloudevent;
|
||||||
|
} else {//binary
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,19 @@
|
||||||
var expect = require("chai").expect;
|
var expect = require("chai").expect;
|
||||||
var Unmarshaller = require("../../../lib/bindings/http/unmarshaller_0_2.js");
|
var Unmarshaller = require("../../../lib/bindings/http/unmarshaller_0_2.js");
|
||||||
|
var Cloudevent = require("../../../index.js");
|
||||||
|
|
||||||
|
const type = "com.github.pull.create";
|
||||||
|
const source = "urn:event:from:myapi/resourse/123";
|
||||||
|
const webhook = "https://cloudevents.io/webhook";
|
||||||
|
const contentType = "application/cloudevents+json; charset=utf-8";
|
||||||
|
const now = new Date();
|
||||||
|
const schemaurl = "http://cloudevents.io/schema.json";
|
||||||
|
|
||||||
|
const ceContentType = "application/json";
|
||||||
|
|
||||||
|
const data = {
|
||||||
|
foo: "bar"
|
||||||
|
};
|
||||||
|
|
||||||
describe("HTTP Transport Binding Unmarshaller", () => {
|
describe("HTTP Transport Binding Unmarshaller", () => {
|
||||||
|
|
||||||
|
@ -48,16 +62,41 @@ describe("HTTP Transport Binding Unmarshaller", () => {
|
||||||
.to.throw("content type not allowed");
|
.to.throw("content type not allowed");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Throw error when structured binding has not allowed mime", () => {
|
describe("Structured", () => {
|
||||||
// setup
|
it("Throw error when has not allowed mime", () => {
|
||||||
var payload = {};
|
// setup
|
||||||
var headers = {
|
var payload = {};
|
||||||
"content-type":"application/cloudevents+zip"
|
var headers = {
|
||||||
};
|
"content-type":"application/cloudevents+zip"
|
||||||
var un = new Unmarshaller();
|
};
|
||||||
|
var un = new Unmarshaller();
|
||||||
|
|
||||||
// act and assert
|
// act and assert
|
||||||
expect(un.unmarshall.bind(un, payload, headers))
|
expect(un.unmarshall.bind(un, payload, headers))
|
||||||
.to.throw("structured+type not allowed");
|
.to.throw("structured+type not allowed");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Throw error when the event does not follow the spec 0.2", () => {
|
||||||
|
// setup
|
||||||
|
var payload =
|
||||||
|
new Cloudevent()
|
||||||
|
.type(type)
|
||||||
|
.source(source)
|
||||||
|
.contenttype(ceContentType)
|
||||||
|
.time(now)
|
||||||
|
.schemaurl(schemaurl)
|
||||||
|
.data(data)
|
||||||
|
.toString();
|
||||||
|
|
||||||
|
var headers = {
|
||||||
|
"content-type":"application/cloudevents+json"
|
||||||
|
};
|
||||||
|
|
||||||
|
var un = new Unmarshaller();
|
||||||
|
|
||||||
|
// act and assert
|
||||||
|
expect(un.unmarshall.bind(un, payload, headers))
|
||||||
|
.to.throw("invalid payload");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue