The http binding for binary - v0.3

Signed-off-by: Fabio José <fabiojose@gmail.com>
This commit is contained in:
Fabio José 2019-08-01 17:35:33 -03:00
parent b53d660402
commit 0f8823c36e
2 changed files with 234 additions and 0 deletions

View File

@ -0,0 +1,64 @@
const BinaryHTTPEmitter = require("./emitter_binary.js");
const Constants = require("./constants.js");
const headerByGetter = {};
headerByGetter["getDataContentType"] = {
name : Constants.HEADER_CONTENT_TYPE,
parser : (v) => v
};
headerByGetter["getDataContentEncoding"] = {
name : Constants.BINARY_HEADERS_03.CONTENT_ENCONDING,
parser : (v) => v
};
headerByGetter["getSubject"] = {
name : Constants.BINARY_HEADERS_03.SUBJECT,
parser : (v) => v
};
headerByGetter["getType"] = {
name : Constants.BINARY_HEADERS_03.TYPE,
parser : (v) => v
};
headerByGetter["getSpecversion"] = {
name : Constants.BINARY_HEADERS_03.SPEC_VERSION,
parser : (v) => v
};
headerByGetter["getSource"] = {
name : Constants.BINARY_HEADERS_03.SOURCE,
parser : (v) => v
};
headerByGetter["getId"] = {
name : Constants.BINARY_HEADERS_03.ID,
parser : (v) => v
};
headerByGetter["getTime"] = {
name : Constants.BINARY_HEADERS_03.TIME,
parser : (v) => v
};
headerByGetter["getSchemaurl"] = {
name : Constants.BINARY_HEADERS_03.SCHEMA_URL,
parser : (v) => v
};
function HTTPBinary(configuration){
this.emitter = new BinaryHTTPEmitter(
configuration,
headerByGetter,
Constants.BINARY_HEADERS_03.EXTENSIONS_PREFIX
);
}
HTTPBinary.prototype.emit = function(cloudevent){
return this.emitter.emit(cloudevent);
};
module.exports = HTTPBinary;

170
test/http_binding_0_3.js Normal file
View File

@ -0,0 +1,170 @@
const expect = require("chai").expect;
const nock = require("nock");
const http = require("http");
const request = require("request");
const BinaryHTTPEmitter =
require("../lib/bindings/http/emitter_binary_0_3.js");
const Cloudevent = require("../lib/cloudevent.js");
const v03 = require("../v03/index.js");
const type = "com.github.pull.create";
const source = "urn:event:from:myapi/resourse/123";
const contentEncoding = "base64";
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 dataBase64 = "Y2xvdWRldmVudHMK";
const ext1Name = "extension1";
const ext1Value = "foobar";
const ext2Name = "extension2";
const ext2Value = "acme";
const cloudevent =
new Cloudevent(v03.Spec)
.type(type)
.source(source)
.dataContentType(ceContentType)
.subject("subject.ext")
.time(now)
.schemaurl(schemaurl)
.data(data)
.addExtension(ext1Name, ext1Value)
.addExtension(ext2Name, ext2Value);
const cebase64 =
new Cloudevent(v03.Spec)
.type(type)
.source(source)
.dataContentType(ceContentType)
.dataContentEncoding(contentEncoding)
.time(now)
.schemaurl(schemaurl)
.data(dataBase64)
.addExtension(ext1Name, ext1Value)
.addExtension(ext2Name, ext2Value);
const webhook = "https://cloudevents.io/webhook";
const httpcfg = {
method : "POST",
url : webhook + "/json"
};
const binary = new BinaryHTTPEmitter(httpcfg);
describe("HTTP Transport Binding - Version 0.3", () => {
beforeEach(() => {
// Mocking the webhook
nock(webhook)
.post("/json")
.reply(201, {status: "accepted"});
});
describe("Binary", () => {
describe("JSON Format", () => {
it("requires '" + cloudevent.getDataContentType() + "' Content-Type in the header", () => {
return binary.emit(cloudevent)
.then((response) => {
expect(response.config.headers["Content-Type"])
.to.equal(cloudevent.getDataContentType());
});
});
it("the request payload should be correct", () => {
return binary.emit(cloudevent)
.then((response) => {
expect(JSON.parse(response.config.data))
.to.deep.equal(cloudevent.getData());
});
});
it("HTTP Header contains 'ce-type'", () => {
return binary.emit(cloudevent)
.then((response) => {
expect(response.config.headers)
.to.have.property("ce-type");
});
});
it("HTTP Header contains 'ce-specversion'", () => {
return binary.emit(cloudevent)
.then((response) => {
expect(response.config.headers)
.to.have.property("ce-specversion");
});
});
it("HTTP Header contains 'ce-source'", () => {
return binary.emit(cloudevent)
.then((response) => {
expect(response.config.headers)
.to.have.property("ce-source");
});
});
it("HTTP Header contains 'ce-id'", () => {
return binary.emit(cloudevent)
.then((response) => {
expect(response.config.headers)
.to.have.property("ce-id");
});
});
it("HTTP Header contains 'ce-time'", () => {
return binary.emit(cloudevent)
.then((response) => {
expect(response.config.headers)
.to.have.property("ce-time");
});
});
it("HTTP Header contains 'ce-schemaurl'", () => {
return binary.emit(cloudevent)
.then((response) => {
expect(response.config.headers)
.to.have.property("ce-schemaurl");
});
});
it("HTTP Header contains 'ce-datacontentencoding'", () => {
return binary.emit(cebase64)
.then((response) => {
expect(response.config.headers)
.to.have.property("ce-datacontentencoding");
});
});
it("HTTP Header contains 'ce-" + ext1Name + "'", () => {
return binary.emit(cloudevent)
.then((response) => {
expect(response.config.headers)
.to.have.property("ce-" + ext1Name);
});
});
it("HTTP Header contains 'ce-" + ext2Name + "'", () => {
return binary.emit(cloudevent)
.then((response) => {
expect(response.config.headers)
.to.have.property("ce-" + ext2Name);
});
});
it("HTTP Header contains 'ce-subject'", () => {
return binary.emit(cloudevent)
.then((response) => {
expect(response.config.headers)
.to.have.property("ce-subject");
});
});
});
});
});