Binary 0.2 impl
Signed-off-by: Fabio José <fabiojose@gmail.com>
This commit is contained in:
parent
25d3909d3f
commit
e3b2ce6b52
|
@ -0,0 +1,39 @@
|
|||
var axios = require("axios");
|
||||
|
||||
function HTTPBinary(configuration){
|
||||
this.config = configuration;
|
||||
|
||||
this.config["headers"] = {
|
||||
"Content-Type":"application/cloudevents+json; charset=utf-8"
|
||||
};
|
||||
}
|
||||
|
||||
HTTPBinary.prototype.emit = function(cloudevent){
|
||||
|
||||
// Create new request object
|
||||
var _config = JSON.parse(JSON.stringify(this.config));
|
||||
|
||||
// Always set stuff in _config
|
||||
var _headers = _config["headers"];
|
||||
|
||||
if(cloudevent.getContenttype()) {
|
||||
_headers["Content-Type"] = cloudevent.getContenttype();
|
||||
}
|
||||
|
||||
_headers["ce-type"] = cloudevent.getType();
|
||||
_headers["ce-specversion"] = cloudevent.getSpecversion();
|
||||
_headers["ce-source"] = cloudevent.getSource();
|
||||
_headers["ce-id"] = cloudevent.getId();
|
||||
if(cloudevent.getTime()) {
|
||||
_headers["ce-time"] = cloudevent.getTime();
|
||||
}
|
||||
_headers["ce-schemaurl"] = cloudevent.getSchemaurl();
|
||||
|
||||
// Set the cloudevent payload
|
||||
_config["data"] = cloudevent.format().data;
|
||||
|
||||
// Return the Promise
|
||||
return axios.request(_config);
|
||||
};
|
||||
|
||||
module.exports = HTTPBinary;
|
|
@ -3,6 +3,7 @@ var Spec02 = require("./specs/spec_0_2.js");
|
|||
var JSONFormatter01 = require("./formats/json_0_1.js");
|
||||
var HTTPStructured01 = require("./bindings/http/structured_0_1.js");
|
||||
var HTTPBinary01 = require("./bindings/http/binary_0_1.js");
|
||||
var HTTPBinary02 = require("./bindings/http/binary_0_2.js");
|
||||
|
||||
/*
|
||||
* Class created using the Builder Design Pattern.
|
||||
|
@ -121,7 +122,8 @@ Cloudevent.formats = {
|
|||
Cloudevent.bindings = {
|
||||
"http-structured" : HTTPStructured01,
|
||||
"http-structured0.1" : HTTPStructured01,
|
||||
"http-binary0.1" : HTTPBinary01
|
||||
"http-binary0.1" : HTTPBinary01,
|
||||
"http-binary0.2" : HTTPBinary02
|
||||
};
|
||||
|
||||
module.exports = Cloudevent;
|
||||
|
|
|
@ -0,0 +1,130 @@
|
|||
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";
|
||||
const now = new Date();
|
||||
const schemaurl = "http://cloudevents.io/schema.json";
|
||||
|
||||
const ceContentType = "application/json";
|
||||
|
||||
const data = {
|
||||
foo: "bar"
|
||||
};
|
||||
|
||||
const Structured01 = Cloudevent.bindings["http-structured0.1"];
|
||||
const Binary02 = Cloudevent.bindings["http-binary0.2"];
|
||||
|
||||
var cloudevent =
|
||||
new Cloudevent()
|
||||
.type(type)
|
||||
.source(source)
|
||||
.contenttype(ceContentType)
|
||||
.time(now)
|
||||
.schemaurl(schemaurl)
|
||||
.data(data);
|
||||
|
||||
cloudevent.eventTypeVersion("1.0.0");
|
||||
|
||||
var httpcfg = {
|
||||
method : "POST",
|
||||
url : webhook + "/json"
|
||||
};
|
||||
|
||||
var httpstructured01 = new Structured01(httpcfg);
|
||||
var httpbinary02 = new Binary02(httpcfg);
|
||||
|
||||
describe("HTTP Transport Binding - Version 0.2", () => {
|
||||
beforeEach(() => {
|
||||
// Mocking the webhook
|
||||
nock(webhook)
|
||||
.post("/json")
|
||||
.reply(201, {status: "accepted"});
|
||||
});
|
||||
|
||||
describe("Structured", () => {
|
||||
describe("JSON Format", () => {
|
||||
it("requires '" + contentType + "' Content-Type in the header", () => {
|
||||
return httpstructured01.emit(cloudevent)
|
||||
.then((response) => {
|
||||
expect(response.config.headers["Content-Type"])
|
||||
.to.equal(contentType);
|
||||
});
|
||||
});
|
||||
|
||||
it("the request payload should be correct", () => {
|
||||
return httpstructured01.emit(cloudevent)
|
||||
.then((response) => {
|
||||
expect(JSON.parse(response.config.data))
|
||||
.to.deep.equal(cloudevent.format());
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("Binary", () => {
|
||||
describe("JSON Format", () => {
|
||||
it("requires '" + cloudevent.getContenttype() + "' Content-Type in the header", () => {
|
||||
return httpbinary02.emit(cloudevent)
|
||||
.then((response) => {
|
||||
expect(response.config.headers["Content-Type"])
|
||||
.to.equal(cloudevent.getContenttype());
|
||||
});
|
||||
});
|
||||
|
||||
it("the request payload should be correct", () => {
|
||||
return httpbinary02.emit(cloudevent)
|
||||
.then((response) => {
|
||||
expect(JSON.parse(response.config.data))
|
||||
.to.deep.equal(cloudevent.getData());
|
||||
});
|
||||
});
|
||||
|
||||
it("HTTP Header contains 'ce-type'", () => {
|
||||
return httpbinary02.emit(cloudevent)
|
||||
.then((response) => {
|
||||
expect(response.config.headers)
|
||||
.to.have.property("ce-type");
|
||||
});
|
||||
});
|
||||
it("HTTP Header contains 'ce-specversion'", () => {
|
||||
return httpbinary02.emit(cloudevent)
|
||||
.then((response) => {
|
||||
expect(response.config.headers)
|
||||
.to.have.property("ce-specversion");
|
||||
});
|
||||
});
|
||||
it("HTTP Header contains 'ce-source'", () => {
|
||||
return httpbinary02.emit(cloudevent)
|
||||
.then((response) => {
|
||||
expect(response.config.headers)
|
||||
.to.have.property("ce-source");
|
||||
});
|
||||
});
|
||||
it("HTTP Header contains 'ce-id'", () => {
|
||||
return httpbinary02.emit(cloudevent)
|
||||
.then((response) => {
|
||||
expect(response.config.headers)
|
||||
.to.have.property("ce-id");
|
||||
});
|
||||
});
|
||||
it("HTTP Header contains 'ce-time'", () => {
|
||||
return httpbinary02.emit(cloudevent)
|
||||
.then((response) => {
|
||||
expect(response.config.headers)
|
||||
.to.have.property("ce-time");
|
||||
});
|
||||
});
|
||||
it("HTTP Header contains 'ce-schemaurl'", () => {
|
||||
return httpbinary02.emit(cloudevent)
|
||||
.then((response) => {
|
||||
expect(response.config.headers)
|
||||
.to.have.property("ce-schemaurl");
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue