From 23887763e5e2484cc9108fa0a693286b116db2cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabio=20Jos=C3=A9?= Date: Sun, 16 Dec 2018 20:20:15 -0200 Subject: [PATCH] WIP: http binary binding MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fabio José --- lib/bindings/http/binary_0_1.js | 29 ++++++++++ lib/cloudevent.js | 21 +++++++- lib/specs/spec_0_1.js | 16 ++++++ lib/specs/spec_0_2.js | 16 ++++++ test/http_binding_0_1.js | 95 ++++++++++++++++++++++++++++++--- 5 files changed, 169 insertions(+), 8 deletions(-) create mode 100644 lib/bindings/http/binary_0_1.js diff --git a/lib/bindings/http/binary_0_1.js b/lib/bindings/http/binary_0_1.js new file mode 100644 index 0000000..6e47eaf --- /dev/null +++ b/lib/bindings/http/binary_0_1.js @@ -0,0 +1,29 @@ +var axios = require("axios"); + +function HTTPBinary(configuration){ + this.config = configuration; + + this.config["headers"] = { + "Content-Type":"application/cloudevents+json; charset=utf-8" + }; +} + +HTTPBinary.prototype.emit = function(cloudevent){ + + // Create new request object + var _config = JSON.parse(JSON.stringify(this.config)); + + // Always set stuff in _config + var _headers = _config["headers"]; + _headers["Content-Type"] = cloudevent.getContenttype(); + + _headers["CE-EventType"] = cloudevent.getType(); + + // Set the cloudevent payload + _config["data"] = cloudevent.format(); + + // Return the Promise + return axios.request(_config); +}; + +module.exports = HTTPBinary; diff --git a/lib/cloudevent.js b/lib/cloudevent.js index ebf53b3..64b5c3b 100644 --- a/lib/cloudevent.js +++ b/lib/cloudevent.js @@ -2,6 +2,7 @@ var Spec01 = require("./specs/spec_0_1.js"); var Spec02 = require("./specs/spec_0_2.js"); var JSONFormatter01 = require("./formats/json_0_1.js"); var HTTPStructured01 = require("./bindings/http/structured_0_1.js"); +var HTTPBinary01 = require("./bindings/http/binary_0_1.js"); /* * Class created using the Builder Design Pattern. @@ -33,6 +34,14 @@ Cloudevent.prototype.type = function(type){ return this; }; +Cloudevent.prototype.getType = function() { + return this.spec.getType(); +}; + +Cloudevent.prototype.getSpecversion = function() { + return this.spec.getSpecversion(); +}; + Cloudevent.prototype.source = function(_source){ this.spec.source(_source); return this; @@ -58,16 +67,25 @@ Cloudevent.prototype.contenttype = function(_contenttype){ return this; }; +Cloudevent.prototype.getContenttype = function() { + return this.spec.getContenttype(); +}; + Cloudevent.prototype.data = function(_data) { this.spec.data(_data); return this; }; +Cloudevent.prototype.getData = function() { + return this.spec.getData(); +}; + Cloudevent.prototype.addExtension = function(key, value){ this.spec.addExtension(key, value); return this; }; + /* * Export the specs */ @@ -86,7 +104,8 @@ Cloudevent.formats = { Cloudevent.bindings = { "http-structured" : HTTPStructured01, - "http-structured0.1" : HTTPStructured01 + "http-structured0.1" : HTTPStructured01, + "http-binary0.1" : HTTPBinary01 }; module.exports = Cloudevent; diff --git a/lib/specs/spec_0_1.js b/lib/specs/spec_0_1.js index 83a909f..81e1400 100644 --- a/lib/specs/spec_0_1.js +++ b/lib/specs/spec_0_1.js @@ -37,6 +37,14 @@ Spec01.prototype.type = function(_type){ return this; }; +Spec01.prototype.getType = function(){ + return this.payload["eventType"]; +}; + +Spec01.prototype.getSpecversion = function() { + return this.payload["cloudEventsVersion"]; +} + Spec01.prototype.eventTypeVersion = function(version){ this.payload["eventTypeVersion"] = version; return this; @@ -67,11 +75,19 @@ Spec01.prototype.contenttype = function(_contenttype){ return this; }; +Spec01.prototype.getContenttype = function() { + return this.payload["contentType"]; +}; + Spec01.prototype.data = function(_data){ this.payload["data"] = _data; return this; }; +Spec01.prototype.getData = function() { + return this.payload["data"]; +}; + Spec01.prototype.addExtension = function(key, value){ if(!this.payload["extensions"]){ this.payload["extensions"] = {}; diff --git a/lib/specs/spec_0_2.js b/lib/specs/spec_0_2.js index 436acab..66d70d1 100644 --- a/lib/specs/spec_0_2.js +++ b/lib/specs/spec_0_2.js @@ -36,6 +36,14 @@ Spec02.prototype.type = function(_type){ return this; }; +Spec02.prototype.getType = function(){ + return this.payload["type"]; +}; + +Spec02.prototype.getSpecversion = function() { + return this.payload["specversion"]; +}; + Spec02.prototype.source = function(_source){ this.payload["source"] = _source; return this; @@ -61,11 +69,19 @@ Spec02.prototype.contenttype = function(_contenttype){ return this; }; +Spec02.prototype.getContenttype = function() { + return this.payload["contenttype"]; +} + Spec02.prototype.data = function(_data){ this.payload["data"] = _data; return this; }; +Spec02.prototype.getData = function() { + return this.payload["data"]; +}; + Spec02.prototype.addExtension = function(key, value){ this.payload[key] = value; return this; diff --git a/test/http_binding_0_1.js b/test/http_binding_0_1.js index 9dc3d66..023ce50 100644 --- a/test/http_binding_0_1.js +++ b/test/http_binding_0_1.js @@ -7,18 +7,29 @@ const source = "urn:event:from:myapi/resourse/123"; const webhook = "https://cloudevents.io/webhook"; const contentType = "application/cloudevents+json; charset=utf-8"; -const HTTPBinding = Cloudevent.bindings["http-structured0.1"]; +const ceContentType = "application/json"; -var cloudevent = new Cloudevent() - .type(type) - .source(source); +const data = { + foo: "bar" +}; + +const Structured01 = Cloudevent.bindings["http-structured0.1"]; +const Binary01 = Cloudevent.bindings["http-binary0.1"]; + +var cloudevent = + new Cloudevent() + .type(type) + .source(source) + .contenttype(ceContentType) + .data(data); var httpcfg = { method : "POST", url : webhook + "/json" }; -var httpstructured01 = new HTTPBinding(httpcfg); +var httpstructured01 = new Structured01(httpcfg); +var httpbinary01 = new Binary01(httpcfg); describe("HTTP Transport Binding - Version 0.1", () => { beforeEach(() => { @@ -30,7 +41,7 @@ describe("HTTP Transport Binding - Version 0.1", () => { describe("Structured", () => { describe("JSON Format", () => { - it("requires '" + contentType + "' Content-Type in header", () => { + it("requires '" + contentType + "' Content-Type in the header", () => { return httpstructured01.emit(cloudevent) .then((response) => { expect(response.config.headers["Content-Type"]) @@ -38,7 +49,7 @@ describe("HTTP Transport Binding - Version 0.1", () => { }); }); - it("the request should be correct", () => { + it("the request payload should be correct", () => { return httpstructured01.emit(cloudevent) .then((response) => { expect(JSON.parse(response.config.data)) @@ -47,4 +58,74 @@ describe("HTTP Transport Binding - Version 0.1", () => { }); }); }); + + describe("Binary", () => { + describe("JSON Format", () => { + it("requires '" + cloudevent.getContenttype() + "' Content-Type in the header", () => { + return httpbinary01.emit(cloudevent) + .then((response) => { + expect(response.config.headers["Content-Type"]) + .to.equal(cloudevent.getContenttype()); + }); + }); + + it("the request payload should be correct", () => { + return httpbinary01.emit(cloudevent) + .then((response) => { + expect(JSON.parse(response.config.data)) + .to.deep.equal(cloudevent.getData()); + }); + }); + + it("HTTP Header contains 'CE-EventType'", () => { + return httpbinary01.emit(cloudevent) + .then((response) => { + expect(response.config.headers) + .to.have.property("CE-EventType"); + }); + }); + it("HTTP Header contains 'CE-EventTypeVersion'", () => { + return httpbinary01.emit(cloudevent) + .then((response) => { + expect(response.config.headers) + .to.have.property("CE-EventTypeVersion"); + }); + }); + it("HTTP Header contains 'CE-CloudEventsVersion'", () => { + return httpbinary01.emit(cloudevent) + .then((response) => { + expect(response.config.headers) + .to.have.property("CE-CloudEventsVersion"); + }); + }); + it("HTTP Header contains 'CE-Source'", () => { + return httpbinary01.emit(cloudevent) + .then((response) => { + expect(response.config.headers) + .to.have.property("CE-Source"); + }); + }); + it("HTTP Header contains 'CE-EventID'", () => { + return httpbinary01.emit(cloudevent) + .then((response) => { + expect(response.config.headers) + .to.have.property("CE-EventID"); + }); + }); + it("HTTP Header contains 'CE-EventTime'", () => { + return httpbinary01.emit(cloudevent) + .then((response) => { + expect(response.config.headers) + .to.have.property("CE-EventTime"); + }); + }); + it("HTTP Header contains 'CE-SchemaURL'", () => { + return httpbinary01.emit(cloudevent) + .then((response) => { + expect(response.config.headers) + .to.have.property("CE-SchemaURL"); + }); + }); + }); + }); });