Skip to content

Commit 1a81e6e

Browse files
committed
Test the extensions
Signed-off-by: Fabio José <[email protected]>
1 parent 177bac8 commit 1a81e6e

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

lib/bindings/http/binary_0_2.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var axios = require("axios");
2+
var empty = require("is-empty");
23

34
function HTTPBinary(configuration){
45
this.config = configuration;
@@ -32,6 +33,12 @@ HTTPBinary.prototype.emit = function(cloudevent){
3233
// Set the cloudevent payload
3334
_config["data"] = cloudevent.format().data;
3435

36+
// Have extensions?
37+
var exts = cloudevent.getExtensions();
38+
for(ext in exts){
39+
_headers["ce-" + ext] = exts[ext];
40+
}
41+
3542
// Return the Promise
3643
return axios.request(_config);
3744
};

lib/cloudevent.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ var HTTPBinary02 = require("./bindings/http/binary_0_2.js");
1414
function Cloudevent(_spec, _formatter){
1515
this.spec = (_spec) ? new _spec(Cloudevent) : new Spec01(Cloudevent);
1616
this.formatter = (_formatter) ? _formatter : new JSONFormatter01();
17+
18+
// The map of extensions
19+
this.extensions = {};
1720
}
1821

1922
/*
@@ -100,9 +103,17 @@ Cloudevent.prototype.getData = function() {
100103

101104
Cloudevent.prototype.addExtension = function(key, value){
102105
this.spec.addExtension(key, value);
106+
107+
// Stores localy
108+
this.extensions[key] = value;
109+
103110
return this;
104111
};
105112

113+
Cloudevent.prototype.getExtensions = function() {
114+
return this.extensions;
115+
}
116+
106117

107118
/*
108119
* Export the specs

test/http_binding_0_2.js

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

18+
const ext1Name = "extension1";
19+
const ext1Value = "foobar";
20+
const ext2Name = "extension2";
21+
const ext2Value = "acme";
22+
1823
const Structured02 = Cloudevent.bindings["http-structured0.2"];
1924
const Binary02 = Cloudevent.bindings["http-binary0.2"];
2025

@@ -25,7 +30,9 @@ var cloudevent =
2530
.contenttype(ceContentType)
2631
.time(now)
2732
.schemaurl(schemaurl)
28-
.data(data);
33+
.data(data)
34+
.addExtension(ext1Name, ext1Value)
35+
.addExtension(ext2Name, ext2Value);
2936

3037
var httpcfg = {
3138
method : "POST",
@@ -123,6 +130,20 @@ describe("HTTP Transport Binding - Version 0.2", () => {
123130
.to.have.property("ce-schemaurl");
124131
});
125132
});
133+
it("HTTP Header contains 'ce-" + ext1Name + "'", () => {
134+
return httpbinary02.emit(cloudevent)
135+
.then((response) => {
136+
expect(response.config.headers)
137+
.to.have.property("ce-" + ext1Name);
138+
});
139+
});
140+
it("HTTP Header contains 'ce-" + ext2Name + "'", () => {
141+
return httpbinary02.emit(cloudevent)
142+
.then((response) => {
143+
expect(response.config.headers)
144+
.to.have.property("ce-" + ext2Name);
145+
});
146+
});
126147
});
127148
});
128149
});

0 commit comments

Comments
 (0)