Receiver for structured
Signed-off-by: Fabio José <fabiojose@gmail.com>
This commit is contained in:
parent
733476f4a1
commit
fbea122b73
|
@ -1,102 +1,46 @@
|
||||||
var http = require("http");
|
const Constants = require("./constants.js");
|
||||||
var Spec02 = require("../../specs/spec_0_2.js")
|
const Commons = require("./commons.js");
|
||||||
var spec02 = new Spec02();
|
const Cloudevent = require("../../cloudevent.js");
|
||||||
|
const Spec02 = require("../../specs/spec_0_2.js");
|
||||||
|
|
||||||
const allowedContentTypes = [];
|
const allowed_content_types = [];
|
||||||
allowedContentTypes.push("application/cloudevents+json; charset=utf-8");
|
allowed_content_types.push(Constants.MIME_CE_JSON);
|
||||||
|
|
||||||
function is_valid_http_request(req, res, config) {
|
function validate_args(payload, attributes) {
|
||||||
var valid = true;
|
if(!payload){
|
||||||
|
throw {message: "payload is null or undefined"};
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
} 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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return valid;
|
if(!attributes) {
|
||||||
}
|
throw {message: "attributes is null or undefined"};
|
||||||
|
|
||||||
function HTTPStructured(configuration){
|
|
||||||
this.config = configuration;
|
|
||||||
|
|
||||||
if(!this.config["path"]){
|
|
||||||
this.config["path"] = "/";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!this.config["method"]){
|
if((typeof payload) !== "object"){
|
||||||
this.config["method"] = "POST";
|
throw {message: "payload must be an object", erros: [typeof payload]};
|
||||||
}
|
|
||||||
|
|
||||||
if(!this.config["interface"]){
|
|
||||||
this.config["interface"] = "0.0.0.0";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
HTTPStructured.prototype.receive = function(){
|
function Receiver(configuration) {
|
||||||
this.server;
|
|
||||||
var self = this;
|
|
||||||
|
|
||||||
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() {
|
Receiver.prototype.check = function(payload, headers) {
|
||||||
this.server.close();
|
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]]};
|
||||||
|
}
|
||||||
|
|
||||||
|
// No erros! Its contains the minimum required attributes
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = HTTPStructured;
|
Receiver.prototype.parse = function(payload, headers) {
|
||||||
|
this.check(payload, headers);
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = Receiver;
|
||||||
|
|
|
@ -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();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue