WIP: http binary binding

Signed-off-by: Fabio José <fabiojose@gmail.com>
This commit is contained in:
Fabio José 2018-12-16 20:20:15 -02:00
parent 26ae718e7d
commit 23887763e5
5 changed files with 169 additions and 8 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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"] = {};

View File

@ -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;

View File

@ -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");
});
});
});
});
});