WIP: http binary binding
Signed-off-by: Fabio José <fabiojose@gmail.com>
This commit is contained in:
parent
26ae718e7d
commit
23887763e5
|
@ -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;
|
|
@ -2,6 +2,7 @@ var Spec01 = require("./specs/spec_0_1.js");
|
||||||
var Spec02 = require("./specs/spec_0_2.js");
|
var Spec02 = require("./specs/spec_0_2.js");
|
||||||
var JSONFormatter01 = require("./formats/json_0_1.js");
|
var JSONFormatter01 = require("./formats/json_0_1.js");
|
||||||
var HTTPStructured01 = require("./bindings/http/structured_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.
|
* Class created using the Builder Design Pattern.
|
||||||
|
@ -33,6 +34,14 @@ Cloudevent.prototype.type = function(type){
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Cloudevent.prototype.getType = function() {
|
||||||
|
return this.spec.getType();
|
||||||
|
};
|
||||||
|
|
||||||
|
Cloudevent.prototype.getSpecversion = function() {
|
||||||
|
return this.spec.getSpecversion();
|
||||||
|
};
|
||||||
|
|
||||||
Cloudevent.prototype.source = function(_source){
|
Cloudevent.prototype.source = function(_source){
|
||||||
this.spec.source(_source);
|
this.spec.source(_source);
|
||||||
return this;
|
return this;
|
||||||
|
@ -58,16 +67,25 @@ Cloudevent.prototype.contenttype = function(_contenttype){
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Cloudevent.prototype.getContenttype = function() {
|
||||||
|
return this.spec.getContenttype();
|
||||||
|
};
|
||||||
|
|
||||||
Cloudevent.prototype.data = function(_data) {
|
Cloudevent.prototype.data = function(_data) {
|
||||||
this.spec.data(_data);
|
this.spec.data(_data);
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Cloudevent.prototype.getData = function() {
|
||||||
|
return this.spec.getData();
|
||||||
|
};
|
||||||
|
|
||||||
Cloudevent.prototype.addExtension = function(key, value){
|
Cloudevent.prototype.addExtension = function(key, value){
|
||||||
this.spec.addExtension(key, value);
|
this.spec.addExtension(key, value);
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Export the specs
|
* Export the specs
|
||||||
*/
|
*/
|
||||||
|
@ -86,7 +104,8 @@ Cloudevent.formats = {
|
||||||
|
|
||||||
Cloudevent.bindings = {
|
Cloudevent.bindings = {
|
||||||
"http-structured" : HTTPStructured01,
|
"http-structured" : HTTPStructured01,
|
||||||
"http-structured0.1" : HTTPStructured01
|
"http-structured0.1" : HTTPStructured01,
|
||||||
|
"http-binary0.1" : HTTPBinary01
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = Cloudevent;
|
module.exports = Cloudevent;
|
||||||
|
|
|
@ -37,6 +37,14 @@ Spec01.prototype.type = function(_type){
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Spec01.prototype.getType = function(){
|
||||||
|
return this.payload["eventType"];
|
||||||
|
};
|
||||||
|
|
||||||
|
Spec01.prototype.getSpecversion = function() {
|
||||||
|
return this.payload["cloudEventsVersion"];
|
||||||
|
}
|
||||||
|
|
||||||
Spec01.prototype.eventTypeVersion = function(version){
|
Spec01.prototype.eventTypeVersion = function(version){
|
||||||
this.payload["eventTypeVersion"] = version;
|
this.payload["eventTypeVersion"] = version;
|
||||||
return this;
|
return this;
|
||||||
|
@ -67,11 +75,19 @@ Spec01.prototype.contenttype = function(_contenttype){
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Spec01.prototype.getContenttype = function() {
|
||||||
|
return this.payload["contentType"];
|
||||||
|
};
|
||||||
|
|
||||||
Spec01.prototype.data = function(_data){
|
Spec01.prototype.data = function(_data){
|
||||||
this.payload["data"] = _data;
|
this.payload["data"] = _data;
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Spec01.prototype.getData = function() {
|
||||||
|
return this.payload["data"];
|
||||||
|
};
|
||||||
|
|
||||||
Spec01.prototype.addExtension = function(key, value){
|
Spec01.prototype.addExtension = function(key, value){
|
||||||
if(!this.payload["extensions"]){
|
if(!this.payload["extensions"]){
|
||||||
this.payload["extensions"] = {};
|
this.payload["extensions"] = {};
|
||||||
|
|
|
@ -36,6 +36,14 @@ Spec02.prototype.type = function(_type){
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Spec02.prototype.getType = function(){
|
||||||
|
return this.payload["type"];
|
||||||
|
};
|
||||||
|
|
||||||
|
Spec02.prototype.getSpecversion = function() {
|
||||||
|
return this.payload["specversion"];
|
||||||
|
};
|
||||||
|
|
||||||
Spec02.prototype.source = function(_source){
|
Spec02.prototype.source = function(_source){
|
||||||
this.payload["source"] = _source;
|
this.payload["source"] = _source;
|
||||||
return this;
|
return this;
|
||||||
|
@ -61,11 +69,19 @@ Spec02.prototype.contenttype = function(_contenttype){
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Spec02.prototype.getContenttype = function() {
|
||||||
|
return this.payload["contenttype"];
|
||||||
|
}
|
||||||
|
|
||||||
Spec02.prototype.data = function(_data){
|
Spec02.prototype.data = function(_data){
|
||||||
this.payload["data"] = _data;
|
this.payload["data"] = _data;
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Spec02.prototype.getData = function() {
|
||||||
|
return this.payload["data"];
|
||||||
|
};
|
||||||
|
|
||||||
Spec02.prototype.addExtension = function(key, value){
|
Spec02.prototype.addExtension = function(key, value){
|
||||||
this.payload[key] = value;
|
this.payload[key] = value;
|
||||||
return this;
|
return this;
|
||||||
|
|
|
@ -7,18 +7,29 @@ const source = "urn:event:from:myapi/resourse/123";
|
||||||
const webhook = "https://cloudevents.io/webhook";
|
const webhook = "https://cloudevents.io/webhook";
|
||||||
const contentType = "application/cloudevents+json; charset=utf-8";
|
const contentType = "application/cloudevents+json; charset=utf-8";
|
||||||
|
|
||||||
const HTTPBinding = Cloudevent.bindings["http-structured0.1"];
|
const ceContentType = "application/json";
|
||||||
|
|
||||||
var cloudevent = new Cloudevent()
|
const data = {
|
||||||
|
foo: "bar"
|
||||||
|
};
|
||||||
|
|
||||||
|
const Structured01 = Cloudevent.bindings["http-structured0.1"];
|
||||||
|
const Binary01 = Cloudevent.bindings["http-binary0.1"];
|
||||||
|
|
||||||
|
var cloudevent =
|
||||||
|
new Cloudevent()
|
||||||
.type(type)
|
.type(type)
|
||||||
.source(source);
|
.source(source)
|
||||||
|
.contenttype(ceContentType)
|
||||||
|
.data(data);
|
||||||
|
|
||||||
var httpcfg = {
|
var httpcfg = {
|
||||||
method : "POST",
|
method : "POST",
|
||||||
url : webhook + "/json"
|
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", () => {
|
describe("HTTP Transport Binding - Version 0.1", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
|
@ -30,7 +41,7 @@ describe("HTTP Transport Binding - Version 0.1", () => {
|
||||||
|
|
||||||
describe("Structured", () => {
|
describe("Structured", () => {
|
||||||
describe("JSON Format", () => {
|
describe("JSON Format", () => {
|
||||||
it("requires '" + contentType + "' Content-Type in header", () => {
|
it("requires '" + contentType + "' Content-Type in the header", () => {
|
||||||
return httpstructured01.emit(cloudevent)
|
return httpstructured01.emit(cloudevent)
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
expect(response.config.headers["Content-Type"])
|
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)
|
return httpstructured01.emit(cloudevent)
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
expect(JSON.parse(response.config.data))
|
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");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue