Skip to content

Commit 2388776

Browse files
committed
WIP: http binary binding
Signed-off-by: Fabio José <[email protected]>
1 parent 26ae718 commit 2388776

File tree

5 files changed

+169
-8
lines changed

5 files changed

+169
-8
lines changed

lib/bindings/http/binary_0_1.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var axios = require("axios");
2+
3+
function HTTPBinary(configuration){
4+
this.config = configuration;
5+
6+
this.config["headers"] = {
7+
"Content-Type":"application/cloudevents+json; charset=utf-8"
8+
};
9+
}
10+
11+
HTTPBinary.prototype.emit = function(cloudevent){
12+
13+
// Create new request object
14+
var _config = JSON.parse(JSON.stringify(this.config));
15+
16+
// Always set stuff in _config
17+
var _headers = _config["headers"];
18+
_headers["Content-Type"] = cloudevent.getContenttype();
19+
20+
_headers["CE-EventType"] = cloudevent.getType();
21+
22+
// Set the cloudevent payload
23+
_config["data"] = cloudevent.format();
24+
25+
// Return the Promise
26+
return axios.request(_config);
27+
};
28+
29+
module.exports = HTTPBinary;

lib/cloudevent.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var Spec01 = require("./specs/spec_0_1.js");
22
var Spec02 = require("./specs/spec_0_2.js");
33
var JSONFormatter01 = require("./formats/json_0_1.js");
44
var HTTPStructured01 = require("./bindings/http/structured_0_1.js");
5+
var HTTPBinary01 = require("./bindings/http/binary_0_1.js");
56

67
/*
78
* Class created using the Builder Design Pattern.
@@ -33,6 +34,14 @@ Cloudevent.prototype.type = function(type){
3334
return this;
3435
};
3536

37+
Cloudevent.prototype.getType = function() {
38+
return this.spec.getType();
39+
};
40+
41+
Cloudevent.prototype.getSpecversion = function() {
42+
return this.spec.getSpecversion();
43+
};
44+
3645
Cloudevent.prototype.source = function(_source){
3746
this.spec.source(_source);
3847
return this;
@@ -58,16 +67,25 @@ Cloudevent.prototype.contenttype = function(_contenttype){
5867
return this;
5968
};
6069

70+
Cloudevent.prototype.getContenttype = function() {
71+
return this.spec.getContenttype();
72+
};
73+
6174
Cloudevent.prototype.data = function(_data) {
6275
this.spec.data(_data);
6376
return this;
6477
};
6578

79+
Cloudevent.prototype.getData = function() {
80+
return this.spec.getData();
81+
};
82+
6683
Cloudevent.prototype.addExtension = function(key, value){
6784
this.spec.addExtension(key, value);
6885
return this;
6986
};
7087

88+
7189
/*
7290
* Export the specs
7391
*/
@@ -86,7 +104,8 @@ Cloudevent.formats = {
86104

87105
Cloudevent.bindings = {
88106
"http-structured" : HTTPStructured01,
89-
"http-structured0.1" : HTTPStructured01
107+
"http-structured0.1" : HTTPStructured01,
108+
"http-binary0.1" : HTTPBinary01
90109
};
91110

92111
module.exports = Cloudevent;

lib/specs/spec_0_1.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ Spec01.prototype.type = function(_type){
3737
return this;
3838
};
3939

40+
Spec01.prototype.getType = function(){
41+
return this.payload["eventType"];
42+
};
43+
44+
Spec01.prototype.getSpecversion = function() {
45+
return this.payload["cloudEventsVersion"];
46+
}
47+
4048
Spec01.prototype.eventTypeVersion = function(version){
4149
this.payload["eventTypeVersion"] = version;
4250
return this;
@@ -67,11 +75,19 @@ Spec01.prototype.contenttype = function(_contenttype){
6775
return this;
6876
};
6977

78+
Spec01.prototype.getContenttype = function() {
79+
return this.payload["contentType"];
80+
};
81+
7082
Spec01.prototype.data = function(_data){
7183
this.payload["data"] = _data;
7284
return this;
7385
};
7486

87+
Spec01.prototype.getData = function() {
88+
return this.payload["data"];
89+
};
90+
7591
Spec01.prototype.addExtension = function(key, value){
7692
if(!this.payload["extensions"]){
7793
this.payload["extensions"] = {};

lib/specs/spec_0_2.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ Spec02.prototype.type = function(_type){
3636
return this;
3737
};
3838

39+
Spec02.prototype.getType = function(){
40+
return this.payload["type"];
41+
};
42+
43+
Spec02.prototype.getSpecversion = function() {
44+
return this.payload["specversion"];
45+
};
46+
3947
Spec02.prototype.source = function(_source){
4048
this.payload["source"] = _source;
4149
return this;
@@ -61,11 +69,19 @@ Spec02.prototype.contenttype = function(_contenttype){
6169
return this;
6270
};
6371

72+
Spec02.prototype.getContenttype = function() {
73+
return this.payload["contenttype"];
74+
}
75+
6476
Spec02.prototype.data = function(_data){
6577
this.payload["data"] = _data;
6678
return this;
6779
};
6880

81+
Spec02.prototype.getData = function() {
82+
return this.payload["data"];
83+
};
84+
6985
Spec02.prototype.addExtension = function(key, value){
7086
this.payload[key] = value;
7187
return this;

test/http_binding_0_1.js

Lines changed: 88 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,29 @@ const source = "urn:event:from:myapi/resourse/123";
77
const webhook = "https://cloudevents.io/webhook";
88
const contentType = "application/cloudevents+json; charset=utf-8";
99

10-
const HTTPBinding = Cloudevent.bindings["http-structured0.1"];
10+
const ceContentType = "application/json";
1111

12-
var cloudevent = new Cloudevent()
13-
.type(type)
14-
.source(source);
12+
const data = {
13+
foo: "bar"
14+
};
15+
16+
const Structured01 = Cloudevent.bindings["http-structured0.1"];
17+
const Binary01 = Cloudevent.bindings["http-binary0.1"];
18+
19+
var cloudevent =
20+
new Cloudevent()
21+
.type(type)
22+
.source(source)
23+
.contenttype(ceContentType)
24+
.data(data);
1525

1626
var httpcfg = {
1727
method : "POST",
1828
url : webhook + "/json"
1929
};
2030

21-
var httpstructured01 = new HTTPBinding(httpcfg);
31+
var httpstructured01 = new Structured01(httpcfg);
32+
var httpbinary01 = new Binary01(httpcfg);
2233

2334
describe("HTTP Transport Binding - Version 0.1", () => {
2435
beforeEach(() => {
@@ -30,15 +41,15 @@ describe("HTTP Transport Binding - Version 0.1", () => {
3041

3142
describe("Structured", () => {
3243
describe("JSON Format", () => {
33-
it("requires '" + contentType + "' Content-Type in header", () => {
44+
it("requires '" + contentType + "' Content-Type in the header", () => {
3445
return httpstructured01.emit(cloudevent)
3546
.then((response) => {
3647
expect(response.config.headers["Content-Type"])
3748
.to.equal(contentType);
3849
});
3950
});
4051

41-
it("the request should be correct", () => {
52+
it("the request payload should be correct", () => {
4253
return httpstructured01.emit(cloudevent)
4354
.then((response) => {
4455
expect(JSON.parse(response.config.data))
@@ -47,4 +58,74 @@ describe("HTTP Transport Binding - Version 0.1", () => {
4758
});
4859
});
4960
});
61+
62+
describe("Binary", () => {
63+
describe("JSON Format", () => {
64+
it("requires '" + cloudevent.getContenttype() + "' Content-Type in the header", () => {
65+
return httpbinary01.emit(cloudevent)
66+
.then((response) => {
67+
expect(response.config.headers["Content-Type"])
68+
.to.equal(cloudevent.getContenttype());
69+
});
70+
});
71+
72+
it("the request payload should be correct", () => {
73+
return httpbinary01.emit(cloudevent)
74+
.then((response) => {
75+
expect(JSON.parse(response.config.data))
76+
.to.deep.equal(cloudevent.getData());
77+
});
78+
});
79+
80+
it("HTTP Header contains 'CE-EventType'", () => {
81+
return httpbinary01.emit(cloudevent)
82+
.then((response) => {
83+
expect(response.config.headers)
84+
.to.have.property("CE-EventType");
85+
});
86+
});
87+
it("HTTP Header contains 'CE-EventTypeVersion'", () => {
88+
return httpbinary01.emit(cloudevent)
89+
.then((response) => {
90+
expect(response.config.headers)
91+
.to.have.property("CE-EventTypeVersion");
92+
});
93+
});
94+
it("HTTP Header contains 'CE-CloudEventsVersion'", () => {
95+
return httpbinary01.emit(cloudevent)
96+
.then((response) => {
97+
expect(response.config.headers)
98+
.to.have.property("CE-CloudEventsVersion");
99+
});
100+
});
101+
it("HTTP Header contains 'CE-Source'", () => {
102+
return httpbinary01.emit(cloudevent)
103+
.then((response) => {
104+
expect(response.config.headers)
105+
.to.have.property("CE-Source");
106+
});
107+
});
108+
it("HTTP Header contains 'CE-EventID'", () => {
109+
return httpbinary01.emit(cloudevent)
110+
.then((response) => {
111+
expect(response.config.headers)
112+
.to.have.property("CE-EventID");
113+
});
114+
});
115+
it("HTTP Header contains 'CE-EventTime'", () => {
116+
return httpbinary01.emit(cloudevent)
117+
.then((response) => {
118+
expect(response.config.headers)
119+
.to.have.property("CE-EventTime");
120+
});
121+
});
122+
it("HTTP Header contains 'CE-SchemaURL'", () => {
123+
return httpbinary01.emit(cloudevent)
124+
.then((response) => {
125+
expect(response.config.headers)
126+
.to.have.property("CE-SchemaURL");
127+
});
128+
});
129+
});
130+
});
50131
});

0 commit comments

Comments
 (0)