HTTP unmarshaller: args validations
Signed-off-by: Fabio José <fabiojose@gmail.com>
This commit is contained in:
parent
6e8f63c45b
commit
2ca88b089c
|
@ -2,26 +2,45 @@
|
||||||
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 allowed_binary_content_types = [];
|
||||||
* Level 0 of validation: is that string? is that JSON?
|
allowed_binary_content_types.push("application/json");
|
||||||
*/
|
|
||||||
function validate_and_parse_as_json(payload) {
|
|
||||||
var json = payload;
|
|
||||||
|
|
||||||
if( (typeof payload) === "string"){
|
const allowed_structured_content_types = [];
|
||||||
try {
|
allowed_structured_content_types.push("application/cloudevents+json");
|
||||||
json = JSON.parse(payload);
|
|
||||||
|
|
||||||
}catch(e) {
|
function validate_args(payload, headers) {
|
||||||
throw {message: "Invalid payload", errors: e};
|
if(!payload){
|
||||||
}
|
throw {message: "payload is null or undefined"};
|
||||||
|
|
||||||
} else if( (typeof payload) !== "object"){
|
|
||||||
// anything else
|
|
||||||
throw {message: "Invalid payload type, allowed are: string or object"};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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() {
|
var Unmarshaller = function() {
|
||||||
|
@ -29,11 +48,10 @@ var Unmarshaller = function() {
|
||||||
}
|
}
|
||||||
|
|
||||||
Unmarshaller.prototype.unmarshall = function(payload, headers) {
|
Unmarshaller.prototype.unmarshall = function(payload, headers) {
|
||||||
|
validate_args(payload, headers);
|
||||||
|
|
||||||
var valid0 = validate_and_parse_as_json(payload);
|
// Resolve the binding
|
||||||
|
var binding = resolve_binding_type(payload, headers);
|
||||||
//TODO binary or structured ?
|
|
||||||
//"Content-Type"
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,15 +1,63 @@
|
||||||
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");
|
||||||
|
|
||||||
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
|
// setup
|
||||||
var payload = 83;
|
var payload = null;
|
||||||
var unmarshaller = new Unmarshaller();
|
var un = new Unmarshaller();
|
||||||
|
|
||||||
// act and assert
|
// act and assert
|
||||||
expect(unmarshaller.unmarshall.bind(payload))
|
expect(un.unmarshall.bind(un, payload))
|
||||||
.to.throw("Invalid payload type, allowed are: string or object");
|
.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");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue