http binary receiver to parse

Signed-off-by: Fabio José <fabiojose@gmail.com>
This commit is contained in:
Fabio José 2019-06-11 21:50:19 -03:00
parent 30f499cbc1
commit dd5f694dab
2 changed files with 171 additions and 0 deletions

View File

@ -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;

View File

@ -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", () => {
});
});