Parser for each attribute type

Signed-off-by: Fabio José <fabiojose@gmail.com>
This commit is contained in:
Fabio José 2019-06-16 12:50:40 -03:00
parent 96d467b1e0
commit 37f60ceb78
2 changed files with 99 additions and 9 deletions

View File

@ -1,4 +1,6 @@
const Constants = require("./constants.js"); const Constants = require("./constants.js");
const Cloudevent = require("../../cloudevent.js");
const Spec02 = require("../../specs/spec_0_2.js");
const required_headers = []; const required_headers = [];
required_headers.push(Constants.BINARY_HEADERS_02.TYPE); required_headers.push(Constants.BINARY_HEADERS_02.TYPE);
@ -7,11 +9,26 @@ required_headers.push(Constants.BINARY_HEADERS_02.SOURCE);
required_headers.push(Constants.BINARY_HEADERS_02.ID); required_headers.push(Constants.BINARY_HEADERS_02.ID);
const setter_reflections = {}; const setter_reflections = {};
setter_reflections[Constants.BINARY_HEADERS_02.TYPE] = "type"; setter_reflections[Constants.BINARY_HEADERS_02.TYPE] = {
setter_reflections[Constants.BINARY_HEADERS_02.SOURCE] = "source"; name : "type",
setter_reflections[Constants.BINARY_HEADERS_02.ID] = "id"; parser : (v) => v
setter_reflections[Constants.BINARY_HEADERS_02.TIME] = "time"; };
setter_reflections[Constants.BINARY_HEADERS_02.SCHEMA_URL] = "schemaurl"; setter_reflections[Constants.BINARY_HEADERS_02.SOURCE] = {
name : "source",
parser: (v) => v
};
setter_reflections[Constants.BINARY_HEADERS_02.ID] = {
name : "id",
parser : (v) => v
};
setter_reflections[Constants.BINARY_HEADERS_02.TIME] = {
name : "time",
parser : (v) => new Date(Date.parse(v))
};
setter_reflections[Constants.BINARY_HEADERS_02.SCHEMA_URL] = {
name: "schemaurl",
parser: (v) => v
};
function validate_args(payload, attributes) { function validate_args(payload, attributes) {
if(!payload){ if(!payload){
@ -42,15 +59,34 @@ Receiver.prototype.check = function(payload, headers) {
} }
} }
if(headers[Constants.BINARY_HEADERS_02.SPEC_VERSION] !== "0.2"){
throw {message: "invalid spec version",
errors: [headers[Constants.BINARY_HEADERS_02.SPEC_VERSION]]};
}
// No erros! Its contains the minimum required attributes // No erros! Its contains the minimum required attributes
} }
Receiver.prototype.parse = function(payload, headers) { Receiver.prototype.parse = function(payload, headers) {
this.check(payload, headers); this.check(payload, headers);
for(setter in setter_reflections) { var cloudevent = new Cloudevent(Spec02);
for(header in setter_reflections) {
// dont worry, check() have seen what was required or not
if(headers[header]){
var setter_name = setter_reflections[header].name;
var parser_fn = setter_reflections[header].parser;
console.log(typeof parser_fn(headers[header]));
// invoke the setter function
cloudevent[setter_name](parser_fn(headers[header]));
} }
} }
// Checks the event spec
cloudevent.format();
// return the result
return cloudevent;
}
module.exports = Receiver; module.exports = Receiver;

View File

@ -93,7 +93,7 @@ describe("HTTP Transport Binding Binary Receiver 0.2", () => {
.to.throw("header 'ce-id' not found"); .to.throw("header 'ce-id' not found");
}); });
it("No error when all required headers are in place", () => { it("Throw error when spec is not 0.2", () => {
// setup // setup
var payload = {}; var payload = {};
var attributes = { var attributes = {
@ -103,6 +103,21 @@ describe("HTTP Transport Binding Binary Receiver 0.2", () => {
"ce-id" : "id" "ce-id" : "id"
}; };
// act and assert
expect(receiver.check.bind(receiver, payload, attributes))
.to.throw("invalid spec version");
});
it("No error when all required headers are in place", () => {
// setup
var payload = {};
var attributes = {
"ce-type" : "type",
"ce-specversion" : "0.2",
"ce-source" : "source",
"ce-id" : "id"
};
// act and assert // act and assert
expect(receiver.check.bind(receiver, payload, attributes)) expect(receiver.check.bind(receiver, payload, attributes))
.to.not.throw(); .to.not.throw();
@ -110,6 +125,45 @@ describe("HTTP Transport Binding Binary Receiver 0.2", () => {
}); });
describe("Parse", () => { describe("Parse", () => {
it("Cloudevent contains 'type'", () => {
// 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"
};
// act
var actual = receiver.parse(payload, attributes);
// assert
});
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"
};
// act
var actual = receiver.parse(payload, attributes);
// assert
expect(actual)
.to.be.an("object");
}); });
}); });
});