From 3dad0d200ced10426a100db35b14fd2da0a81c06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabio=20Jos=C3=A9?= Date: Thu, 20 Jun 2019 21:58:10 -0300 Subject: [PATCH] Remove server stuff MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fabio José --- README.md | 50 +--------- lib/bindings/http/server/structured_0_2.js | 102 --------------------- test/http_binding_0_2.js | 95 ------------------- 3 files changed, 1 insertion(+), 246 deletions(-) delete mode 100644 lib/bindings/http/server/structured_0_2.js diff --git a/README.md b/README.md index 0731823..647e023 100644 --- a/README.md +++ b/README.md @@ -174,35 +174,8 @@ See how to listen to events using [express](https://github.com/expressjs/express). ```js +var Unmarshaller02 = require("cloudevents-sdk/http/unmarshaller/v02"); - -``` - -#### Receiving Using our Simple HTTP Server - -See how to listen to events using out simple http server. - -```js -var Cloudevent = require("cloudevents-sdk"); - -// The binding configuration -var config = { - path : "/events", - port : 10300, - method : "POST" -}; - -// The binding instance -var binding = new Cloudevent.bindings["http-structured0.2"](config); - -binding.listen() - .then(cloudevent => { - // do something with event - }) - .catch(err => { - // deal with errors - console.error(err); - }); ``` ## Repository Structure @@ -357,27 +330,6 @@ Receiver.check(Object, Map) Cloudevent Receiver.parse(Object, Map) ``` -##### Receiver Binding Server - -Use this API to start a HTTP server and listen to events. - -```js -/* - * The constructor must receives the map of configurations. - */ -Receiver(config) - -/* - * Listen to events and returns a Promise. - */ -Promise Receiver.listen() - -/* - * Stops the listen to events - */ -Receiver.stop() -``` - ### `Unmarshaller` classes The Unmarshaller classes uses the receiver API, abstracting the formats: diff --git a/lib/bindings/http/server/structured_0_2.js b/lib/bindings/http/server/structured_0_2.js deleted file mode 100644 index 9d46597..0000000 --- a/lib/bindings/http/server/structured_0_2.js +++ /dev/null @@ -1,102 +0,0 @@ -var http = require("http"); -var Spec02 = require("../../../specs/spec_0_2.js") -var spec02 = new Spec02(); - -const allowedContentTypes = []; -allowedContentTypes.push("application/cloudevents+json; charset=utf-8"); - -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; - } - - 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"; - } -} - -HTTPStructured.prototype.listen = function(){ - 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() { - this.server.close(); -} - -module.exports = HTTPStructured; diff --git a/test/http_binding_0_2.js b/test/http_binding_0_2.js index 7bbecb5..01f619b 100644 --- a/test/http_binding_0_2.js +++ b/test/http_binding_0_2.js @@ -1,8 +1,6 @@ var expect = require("chai").expect; var Cloudevent = require("../index.js"); var nock = require("nock"); -var ReceiverStructured01 = - require("../lib/bindings/http/server/structured_0_2.js"); var http = require("http"); var request = require("request"); var Spec02 = require("../lib/specs/spec_0_2.js"); @@ -81,99 +79,6 @@ describe("HTTP Transport Binding - Version 0.2", () => { }); }); }); - - describe("Receiver", () => { - var receiver; - before(() => { - // setup - receiver = new ReceiverStructured01(receiverConfig); - receiver.listen() - .then(response => { - console.log(response); - }) - .catch(err => { - console.error(err); - }); - }); - - after(() => { - receiver.stop(); - }); - - it("Should return 404 when path is wrong", () => { - // act - request.post("http://localhost:" + receiverConfig.port + "/foobar", - (err, res, body) => { - // assert - expect(res.statusCode).to.equal(404); - }); - }); - - it("Should return 405 when method is wrong", () => { - // act - request.get("http://localhost:" + receiverConfig.port - + receiverConfig.path, - (err, res, body) => { - // assert - expect(res.statusCode).to.equal(405); - }); - }); - - it("Should return 400 when Content-Type is wrong", () => { - // act - request.post("http://localhost:" + receiverConfig.port - + receiverConfig.path, - (err, res, body) => { - // assert - expect(res.statusCode).to.equal(400); - }); - }); - - it("Should return 400 when is not a cloudevent", () => { - // setup - var requestOptions = { - url : "http://localhost:" + receiverConfig.port - + receiverConfig.path, - method : "POST", - headers : { - "Content-Type":"application/cloudevents+json; charset=utf-8" - }, - body : JSON.stringify({"foo": "bar"}) - }; - - // act - request(requestOptions, - (err, res, body) => { - // assert - expect(res.statusCode).to.equal(400); - }); - }); - - it("Should return 201 when accepts the event", () => { - // setup - var ce_spec02 = new Spec02(); - ce_spec02 - .type(type) - .source(source); - - var requestOptions = { - url : "http://localhost:" + receiverConfig.port - + receiverConfig.path, - method : "POST", - headers : { - "Content-Type":"application/cloudevents+json; charset=utf-8" - }, - body : JSON.stringify(ce_spec02.payload) - }; - - // act - request(requestOptions, - (err, res, body) => { - // assert - expect(res.statusCode).to.equal(201); - }); - }); - }); }); describe("Binary", () => {