Unit testing for http bindings
Signed-off-by: Fabio José <fabiojose@gmail.com>
This commit is contained in:
parent
e05aadb965
commit
50fdc762f4
|
@ -0,0 +1,24 @@
|
||||||
|
var axios = require("axios");
|
||||||
|
|
||||||
|
function HTTPStructured(configuration){
|
||||||
|
this.config = configuration;
|
||||||
|
|
||||||
|
this.config['headers'] = {
|
||||||
|
'Content-Type':'application/cloudevents+json; charset=utf-8'
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
HTTPStructured.prototype.emit = function(cloudevent){
|
||||||
|
|
||||||
|
// Create new request object
|
||||||
|
var _config = JSON.parse(JSON.stringify(this.config));
|
||||||
|
|
||||||
|
// Set the cloudevent payload
|
||||||
|
_config['data'] = cloudevent.format();
|
||||||
|
|
||||||
|
// Return the Promise
|
||||||
|
return axios.request(_config);
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = HTTPStructured;
|
||||||
|
|
|
@ -0,0 +1,43 @@
|
||||||
|
var expect = require("chai").expect;
|
||||||
|
var Cloudevent = require("../index.js");
|
||||||
|
var nock = require("nock");
|
||||||
|
|
||||||
|
const type = "com.github.pull.create";
|
||||||
|
const source = "urn:event:from:myapi/resourse/123";
|
||||||
|
const webhook = "https://cloudevents.io/webhook";
|
||||||
|
const contentType = "application/cloudevents+json; charset=utf-8";
|
||||||
|
|
||||||
|
var cloudevent = new Cloudevent()
|
||||||
|
.type(type)
|
||||||
|
.source(source);
|
||||||
|
|
||||||
|
var httpcfg = {
|
||||||
|
method : 'POST',
|
||||||
|
url : webhook + '/json'
|
||||||
|
};
|
||||||
|
|
||||||
|
var httpstructured_0_1 =
|
||||||
|
new Cloudevent.bindings['http-structured0.1'](httpcfg);
|
||||||
|
|
||||||
|
describe("HTTP Transport Binding", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
// Mocking the webhook
|
||||||
|
nock(webhook)
|
||||||
|
.post("/json")
|
||||||
|
.reply(201, {status: 'accepted'});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("Structured", () => {
|
||||||
|
describe("JSON Format", () => {
|
||||||
|
it("requires '" + contentType + "' Content-Type in header", () => {
|
||||||
|
return httpstructured_0_1.emit(cloudevent)
|
||||||
|
.then(response => {
|
||||||
|
//console.log(response.config);
|
||||||
|
expect(response.config.headers['Content-Type'])
|
||||||
|
.to.equal(contentType);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
Loading…
Reference in New Issue