Skip to content

Commit b976273

Browse files
committed
HTTP Binding Structured 0.2 impl
Signed-off-by: Fabio José <[email protected]>
1 parent e3b2ce6 commit b976273

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

lib/bindings/http/structured_0_2.js

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

lib/cloudevent.js

Lines changed: 2 additions & 0 deletions
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 HTTPStructured02 = require("./bindings/http/structured_0_2.js");
56
var HTTPBinary01 = require("./bindings/http/binary_0_1.js");
67
var HTTPBinary02 = require("./bindings/http/binary_0_2.js");
78

@@ -122,6 +123,7 @@ Cloudevent.formats = {
122123
Cloudevent.bindings = {
123124
"http-structured" : HTTPStructured01,
124125
"http-structured0.1" : HTTPStructured01,
126+
"http-structured0.2" : HTTPStructured02,
125127
"http-binary0.1" : HTTPBinary01,
126128
"http-binary0.2" : HTTPBinary02
127129
};

test/http_binding_0_2.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const data = {
1515
foo: "bar"
1616
};
1717

18-
const Structured01 = Cloudevent.bindings["http-structured0.1"];
18+
const Structured02 = Cloudevent.bindings["http-structured0.2"];
1919
const Binary02 = Cloudevent.bindings["http-binary0.2"];
2020

2121
var cloudevent =
@@ -27,14 +27,12 @@ var cloudevent =
2727
.schemaurl(schemaurl)
2828
.data(data);
2929

30-
cloudevent.eventTypeVersion("1.0.0");
31-
3230
var httpcfg = {
3331
method : "POST",
3432
url : webhook + "/json"
3533
};
3634

37-
var httpstructured01 = new Structured01(httpcfg);
35+
var httpstructured02 = new Structured02(httpcfg);
3836
var httpbinary02 = new Binary02(httpcfg);
3937

4038
describe("HTTP Transport Binding - Version 0.2", () => {
@@ -48,15 +46,15 @@ describe("HTTP Transport Binding - Version 0.2", () => {
4846
describe("Structured", () => {
4947
describe("JSON Format", () => {
5048
it("requires '" + contentType + "' Content-Type in the header", () => {
51-
return httpstructured01.emit(cloudevent)
49+
return httpstructured02.emit(cloudevent)
5250
.then((response) => {
5351
expect(response.config.headers["Content-Type"])
5452
.to.equal(contentType);
5553
});
5654
});
5755

5856
it("the request payload should be correct", () => {
59-
return httpstructured01.emit(cloudevent)
57+
return httpstructured02.emit(cloudevent)
6058
.then((response) => {
6159
expect(JSON.parse(response.config.data))
6260
.to.deep.equal(cloudevent.format());

0 commit comments

Comments
 (0)