http binary receiver to parse
Signed-off-by: Fabio José <fabiojose@gmail.com>
This commit is contained in:
parent
30f499cbc1
commit
dd5f694dab
|
@ -0,0 +1,56 @@
|
||||||
|
const Constants = require("./constants.js");
|
||||||
|
|
||||||
|
const required_headers = [];
|
||||||
|
required_headers.push(Constants.BINARY_HEADERS_02.TYPE);
|
||||||
|
required_headers.push(Constants.BINARY_HEADERS_02.SPEC_VERSION);
|
||||||
|
required_headers.push(Constants.BINARY_HEADERS_02.SOURCE);
|
||||||
|
required_headers.push(Constants.BINARY_HEADERS_02.ID);
|
||||||
|
|
||||||
|
const setter_reflections = {};
|
||||||
|
setter_reflections[Constants.BINARY_HEADERS_02.TYPE] = "type";
|
||||||
|
setter_reflections[Constants.BINARY_HEADERS_02.SOURCE] = "source";
|
||||||
|
setter_reflections[Constants.BINARY_HEADERS_02.ID] = "id";
|
||||||
|
setter_reflections[Constants.BINARY_HEADERS_02.TIME] = "time";
|
||||||
|
setter_reflections[Constants.BINARY_HEADERS_02.SCHEMA_URL] = "schemaurl";
|
||||||
|
|
||||||
|
function validate_args(payload, attributes) {
|
||||||
|
if(!payload){
|
||||||
|
throw {message: "payload is null or undefined"};
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!attributes) {
|
||||||
|
throw {message: "attributes is null or undefined"};
|
||||||
|
}
|
||||||
|
|
||||||
|
if((typeof payload) !== "object"){
|
||||||
|
throw {message: "payload must be an object", erros: [typeof payload]};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function Receiver(configuration) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
Receiver.prototype.check = function(payload, headers) {
|
||||||
|
// Validation Level 0
|
||||||
|
validate_args(payload, headers);
|
||||||
|
|
||||||
|
// Validation Level 1
|
||||||
|
for(i in required_headers){
|
||||||
|
if(!headers[required_headers[i]]){
|
||||||
|
throw {message: "header '" + required_headers[i] + "' not found"};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// No erros! Its contains the minimum required attributes
|
||||||
|
}
|
||||||
|
|
||||||
|
Receiver.prototype.parse = function(payload, headers) {
|
||||||
|
this.check(payload, headers);
|
||||||
|
|
||||||
|
for(setter in setter_reflections) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = Receiver;
|
|
@ -0,0 +1,115 @@
|
||||||
|
var expect = require("chai").expect;
|
||||||
|
|
||||||
|
var HTTPBinaryReceiver02 =
|
||||||
|
require("../../../lib/bindings/http/binary_receiver_0_2.js");
|
||||||
|
|
||||||
|
var receiver = new HTTPBinaryReceiver02();
|
||||||
|
|
||||||
|
describe("HTTP Transport Binding Binary Receiver 0.2", () => {
|
||||||
|
describe("Check", () => {
|
||||||
|
it("Throw error when payload arg is null or undefined", () => {
|
||||||
|
// setup
|
||||||
|
var payload = null;
|
||||||
|
var attributes = {};
|
||||||
|
|
||||||
|
// act and assert
|
||||||
|
expect(receiver.check.bind(receiver, payload, attributes))
|
||||||
|
.to.throw("payload is null or undefined");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Throw error when attributes arg is null or undefined", () => {
|
||||||
|
// setup
|
||||||
|
var payload = {};
|
||||||
|
var attributes = null;
|
||||||
|
|
||||||
|
// act and assert
|
||||||
|
expect(receiver.check.bind(receiver, payload, attributes))
|
||||||
|
.to.throw("attributes is null or undefined");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Throw error when payload is not an object", () => {
|
||||||
|
// setup
|
||||||
|
var payload = "wow";
|
||||||
|
var attributes = {};
|
||||||
|
|
||||||
|
// act and assert
|
||||||
|
expect(receiver.check.bind(receiver, payload, attributes))
|
||||||
|
.to.throw("payload must be an object");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Throw error when headers has no 'ce-type'", () => {
|
||||||
|
// setup
|
||||||
|
var payload = {};
|
||||||
|
var attributes = {
|
||||||
|
"ce-specversion" : "specversion",
|
||||||
|
"ce-source" : "source",
|
||||||
|
"ce-id" : "id"
|
||||||
|
};
|
||||||
|
|
||||||
|
// act and assert
|
||||||
|
expect(receiver.check.bind(receiver, payload, attributes))
|
||||||
|
.to.throw("header 'ce-type' not found");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Throw error when headers has no 'ce-specversion'", () => {
|
||||||
|
// setup
|
||||||
|
var payload = {};
|
||||||
|
var attributes = {
|
||||||
|
"ce-type" : "type",
|
||||||
|
"ce-source" : "source",
|
||||||
|
"ce-id" : "id"
|
||||||
|
};
|
||||||
|
|
||||||
|
// act and assert
|
||||||
|
expect(receiver.check.bind(receiver, payload, attributes))
|
||||||
|
.to.throw("header 'ce-specversion' not found");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Throw error when headers has no 'ce-source'", () => {
|
||||||
|
// setup
|
||||||
|
var payload = {};
|
||||||
|
var attributes = {
|
||||||
|
"ce-type" : "type",
|
||||||
|
"ce-specversion" : "specversion",
|
||||||
|
"ce-id" : "id"
|
||||||
|
};
|
||||||
|
|
||||||
|
// act and assert
|
||||||
|
expect(receiver.check.bind(receiver, payload, attributes))
|
||||||
|
.to.throw("header 'ce-source' not found");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Throw error when headers has no 'ce-id'", () => {
|
||||||
|
// setup
|
||||||
|
var payload = {};
|
||||||
|
var attributes = {
|
||||||
|
"ce-type" : "type",
|
||||||
|
"ce-specversion" : "specversion",
|
||||||
|
"ce-source" : "source"
|
||||||
|
};
|
||||||
|
|
||||||
|
// act and assert
|
||||||
|
expect(receiver.check.bind(receiver, payload, attributes))
|
||||||
|
.to.throw("header 'ce-id' not found");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("No error when all required headers are in place", () => {
|
||||||
|
// setup
|
||||||
|
var payload = {};
|
||||||
|
var attributes = {
|
||||||
|
"ce-type" : "type",
|
||||||
|
"ce-specversion" : "specversion",
|
||||||
|
"ce-source" : "source",
|
||||||
|
"ce-id" : "id"
|
||||||
|
};
|
||||||
|
|
||||||
|
// act and assert
|
||||||
|
expect(receiver.check.bind(receiver, payload, attributes))
|
||||||
|
.to.not.throw();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("Parse", () => {
|
||||||
|
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue