Receiver for structured

Signed-off-by: Fabio José <fabiojose@gmail.com>
This commit is contained in:
Fabio José 2019-06-16 18:47:09 -03:00
parent 733476f4a1
commit fbea122b73
2 changed files with 96 additions and 88 deletions

View File

@ -1,102 +1,46 @@
var http = require("http");
var Spec02 = require("../../specs/spec_0_2.js")
var spec02 = new Spec02();
const Constants = require("./constants.js");
const Commons = require("./commons.js");
const Cloudevent = require("../../cloudevent.js");
const Spec02 = require("../../specs/spec_0_2.js");
const allowedContentTypes = [];
allowedContentTypes.push("application/cloudevents+json; charset=utf-8");
const allowed_content_types = [];
allowed_content_types.push(Constants.MIME_CE_JSON);
function is_valid_http_request(req, res, config) {
var valid = true;
if(req.url === config.path
&& req.method.toLowerCase()
=== config.method.toLowerCase()) {
if(!req.headers["content-type"]
|| !allowedContentTypes.includes(
req.headers["content-type"].toLowerCase())){
res.statusCode = 400;
res.end("Bad Request");
valid = false;
function validate_args(payload, attributes) {
if(!payload){
throw {message: "payload is null or undefined"};
}
} else if(req.url !== config.path) {
res.statusCode = 404;
res.end("Not Found");
valid = false;
} else {
res.statusCode = 405;
res.end("Method Not Allowed");
valid = false;
if(!attributes) {
throw {message: "attributes is null or undefined"};
}
return valid;
}
function HTTPStructured(configuration){
this.config = configuration;
if(!this.config["path"]){
this.config["path"] = "/";
}
if(!this.config["method"]){
this.config["method"] = "POST";
}
if(!this.config["interface"]){
this.config["interface"] = "0.0.0.0";
if((typeof payload) !== "object"){
throw {message: "payload must be an object", erros: [typeof payload]};
}
}
HTTPStructured.prototype.receive = function(){
this.server;
var self = this;
function Receiver(configuration) {
return new Promise((resolve, reject) => {
self.server =
http.createServer((request, res) => {
if(is_valid_http_request(request, res, this.config)){
var body = [];
request.on("error", err => {
console.error(err);
})
.on("data", chunk => {
// accumulate the chunks
body.push(chunk);
})
.on("end", () => {
body = Buffer.concat(body).toString();
var jsonBody = JSON.parse(body);
try {
// Process/validate the body
spec02.check(jsonBody);
res.statusCode = 201;
res.end("Event Accepted");
}catch(e) {
res.statusCode = 400;
res.end(JSON.stringify(e));
}
});
}
});
self.server.listen(this.config.port, this.config.interface, (err) => {
if(err){
console.error(err);
reject(err);
}
});
});
}
HTTPStructured.prototype.stop = function() {
this.server.close();
Receiver.prototype.check = function(payload, headers) {
validate_args(payload, headers);
var sanity_headers = Commons.sanity_and_clone(headers);
// Validation Level 1
if(!allowed_content_types
.includes(sanity_headers[Constants.HEADER_CONTENT_TYPE])){
throw {message: "invalid content type",
errors: [sanity_headers[Constants.HEADER_CONTENT_TYPE]]};
}
module.exports = HTTPStructured;
// No erros! Its contains the minimum required attributes
}
Receiver.prototype.parse = function(payload, headers) {
this.check(payload, headers);
}
module.exports = Receiver;

View File

@ -0,0 +1,64 @@
var expect = require("chai").expect;
var HTTPStructuredReceiver02 =
require("../../../lib/bindings/http/receiver_structured_0_2.js");
var receiver = new HTTPStructuredReceiver02();
describe("HTTP Transport Binding Structured 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 the content-type is invalid", () => {
// setup
var payload = {};
var attributes = {
"Content-Type" : "text/html"
};
// act and assert
expect(receiver.check.bind(receiver, payload, attributes))
.to.throw("invalid content type");
});
it("No error when all required stuff are in place", () => {
// setup
var payload = {};
var attributes = {
"Content-Type" : "application/cloudevents+json"
};
// act and assert
expect(receiver.check.bind(receiver, payload, attributes))
.to.not.throw();
});
});
});