Skip to content

Commit 50fdc76

Browse files
committed
Unit testing for http bindings
Signed-off-by: Fabio José <[email protected]>
1 parent e05aadb commit 50fdc76

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

lib/bindings/http/structured_0_1.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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;
24+

test/http_binding_0_1.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
10+
var cloudevent = new Cloudevent()
11+
.type(type)
12+
.source(source);
13+
14+
var httpcfg = {
15+
method : 'POST',
16+
url : webhook + '/json'
17+
};
18+
19+
var httpstructured_0_1 =
20+
new Cloudevent.bindings['http-structured0.1'](httpcfg);
21+
22+
describe("HTTP Transport Binding", () => {
23+
beforeEach(() => {
24+
// Mocking the webhook
25+
nock(webhook)
26+
.post("/json")
27+
.reply(201, {status: 'accepted'});
28+
});
29+
30+
describe("Structured", () => {
31+
describe("JSON Format", () => {
32+
it("requires '" + contentType + "' Content-Type in header", () => {
33+
return httpstructured_0_1.emit(cloudevent)
34+
.then(response => {
35+
//console.log(response.config);
36+
expect(response.config.headers['Content-Type'])
37+
.to.equal(contentType);
38+
});
39+
});
40+
});
41+
});
42+
});
43+

0 commit comments

Comments
 (0)