Skip to content

Commit e3b2ce6

Browse files
committed
Binary 0.2 impl
Signed-off-by: Fabio José <[email protected]>
1 parent 25d3909 commit e3b2ce6

File tree

3 files changed

+172
-1
lines changed

3 files changed

+172
-1
lines changed

lib/bindings/http/binary_0_2.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
19+
if(cloudevent.getContenttype()) {
20+
_headers["Content-Type"] = cloudevent.getContenttype();
21+
}
22+
23+
_headers["ce-type"] = cloudevent.getType();
24+
_headers["ce-specversion"] = cloudevent.getSpecversion();
25+
_headers["ce-source"] = cloudevent.getSource();
26+
_headers["ce-id"] = cloudevent.getId();
27+
if(cloudevent.getTime()) {
28+
_headers["ce-time"] = cloudevent.getTime();
29+
}
30+
_headers["ce-schemaurl"] = cloudevent.getSchemaurl();
31+
32+
// Set the cloudevent payload
33+
_config["data"] = cloudevent.format().data;
34+
35+
// Return the Promise
36+
return axios.request(_config);
37+
};
38+
39+
module.exports = HTTPBinary;

lib/cloudevent.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ 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");
55
var HTTPBinary01 = require("./bindings/http/binary_0_1.js");
6+
var HTTPBinary02 = require("./bindings/http/binary_0_2.js");
67

78
/*
89
* Class created using the Builder Design Pattern.
@@ -121,7 +122,8 @@ Cloudevent.formats = {
121122
Cloudevent.bindings = {
122123
"http-structured" : HTTPStructured01,
123124
"http-structured0.1" : HTTPStructured01,
124-
"http-binary0.1" : HTTPBinary01
125+
"http-binary0.1" : HTTPBinary01,
126+
"http-binary0.2" : HTTPBinary02
125127
};
126128

127129
module.exports = Cloudevent;

test/http_binding_0_2.js

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
var expect = require("chai").expect;
2+
var Cloudevent = require("../index.js");
3+
var nock = require("nock");
4+
5+
const type = "com.github.pull.create";
6+
const source = "urn:event:from:myapi/resourse/123";
7+
const webhook = "https://cloudevents.io/webhook";
8+
const contentType = "application/cloudevents+json; charset=utf-8";
9+
const now = new Date();
10+
const schemaurl = "http://cloudevents.io/schema.json";
11+
12+
const ceContentType = "application/json";
13+
14+
const data = {
15+
foo: "bar"
16+
};
17+
18+
const Structured01 = Cloudevent.bindings["http-structured0.1"];
19+
const Binary02 = Cloudevent.bindings["http-binary0.2"];
20+
21+
var cloudevent =
22+
new Cloudevent()
23+
.type(type)
24+
.source(source)
25+
.contenttype(ceContentType)
26+
.time(now)
27+
.schemaurl(schemaurl)
28+
.data(data);
29+
30+
cloudevent.eventTypeVersion("1.0.0");
31+
32+
var httpcfg = {
33+
method : "POST",
34+
url : webhook + "/json"
35+
};
36+
37+
var httpstructured01 = new Structured01(httpcfg);
38+
var httpbinary02 = new Binary02(httpcfg);
39+
40+
describe("HTTP Transport Binding - Version 0.2", () => {
41+
beforeEach(() => {
42+
// Mocking the webhook
43+
nock(webhook)
44+
.post("/json")
45+
.reply(201, {status: "accepted"});
46+
});
47+
48+
describe("Structured", () => {
49+
describe("JSON Format", () => {
50+
it("requires '" + contentType + "' Content-Type in the header", () => {
51+
return httpstructured01.emit(cloudevent)
52+
.then((response) => {
53+
expect(response.config.headers["Content-Type"])
54+
.to.equal(contentType);
55+
});
56+
});
57+
58+
it("the request payload should be correct", () => {
59+
return httpstructured01.emit(cloudevent)
60+
.then((response) => {
61+
expect(JSON.parse(response.config.data))
62+
.to.deep.equal(cloudevent.format());
63+
});
64+
});
65+
});
66+
});
67+
68+
describe("Binary", () => {
69+
describe("JSON Format", () => {
70+
it("requires '" + cloudevent.getContenttype() + "' Content-Type in the header", () => {
71+
return httpbinary02.emit(cloudevent)
72+
.then((response) => {
73+
expect(response.config.headers["Content-Type"])
74+
.to.equal(cloudevent.getContenttype());
75+
});
76+
});
77+
78+
it("the request payload should be correct", () => {
79+
return httpbinary02.emit(cloudevent)
80+
.then((response) => {
81+
expect(JSON.parse(response.config.data))
82+
.to.deep.equal(cloudevent.getData());
83+
});
84+
});
85+
86+
it("HTTP Header contains 'ce-type'", () => {
87+
return httpbinary02.emit(cloudevent)
88+
.then((response) => {
89+
expect(response.config.headers)
90+
.to.have.property("ce-type");
91+
});
92+
});
93+
it("HTTP Header contains 'ce-specversion'", () => {
94+
return httpbinary02.emit(cloudevent)
95+
.then((response) => {
96+
expect(response.config.headers)
97+
.to.have.property("ce-specversion");
98+
});
99+
});
100+
it("HTTP Header contains 'ce-source'", () => {
101+
return httpbinary02.emit(cloudevent)
102+
.then((response) => {
103+
expect(response.config.headers)
104+
.to.have.property("ce-source");
105+
});
106+
});
107+
it("HTTP Header contains 'ce-id'", () => {
108+
return httpbinary02.emit(cloudevent)
109+
.then((response) => {
110+
expect(response.config.headers)
111+
.to.have.property("ce-id");
112+
});
113+
});
114+
it("HTTP Header contains 'ce-time'", () => {
115+
return httpbinary02.emit(cloudevent)
116+
.then((response) => {
117+
expect(response.config.headers)
118+
.to.have.property("ce-time");
119+
});
120+
});
121+
it("HTTP Header contains 'ce-schemaurl'", () => {
122+
return httpbinary02.emit(cloudevent)
123+
.then((response) => {
124+
expect(response.config.headers)
125+
.to.have.property("ce-schemaurl");
126+
});
127+
});
128+
});
129+
});
130+
});

0 commit comments

Comments
 (0)