diff --git a/lib/bindings/http/receiver_structured_0_2.js b/lib/bindings/http/receiver_structured_0_2.js index 81eec42..6ca0ebc 100644 --- a/lib/bindings/http/receiver_structured_0_2.js +++ b/lib/bindings/http/receiver_structured_0_2.js @@ -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; - } - - } 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; +function validate_args(payload, attributes) { + if(!payload){ + throw {message: "payload is null or undefined"}; } - return valid; -} - -function HTTPStructured(configuration){ - this.config = configuration; - - if(!this.config["path"]){ - this.config["path"] = "/"; + if(!attributes) { + throw {message: "attributes is null or undefined"}; } - 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]]}; + } + + // No erros! Its contains the minimum required attributes } -module.exports = HTTPStructured; +Receiver.prototype.parse = function(payload, headers) { + this.check(payload, headers); +} + +module.exports = Receiver; diff --git a/test/bindings/http/receiver_strutured_0_2_test.js b/test/bindings/http/receiver_strutured_0_2_test.js new file mode 100644 index 0000000..5087020 --- /dev/null +++ b/test/bindings/http/receiver_strutured_0_2_test.js @@ -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(); + }); + }); +});