Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose a mode and version agnostic event receiver #120

Merged
merged 2 commits into from
May 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
const CloudEvent = require("./lib/cloudevent.js");
const HTTPReceiver = require("./lib/bindings/http/http_receiver.js");

module.exports = CloudEvent;
module.exports = {
CloudEvent,
HTTPReceiver
};
61 changes: 61 additions & 0 deletions lib/bindings/http/http_receiver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const V03Binary = require("./receiver_binary_0_3");
const V03Structured = require("./receiver_structured_0_3.js");
const V1Binary = require("./receiver_binary_1.js");
const V1Structured = require("./receiver_structured_1.js");
const constants = require("./constants");

class HTTPReceiver {
constructor() {
this.receivers = {
v1: {
structured: new V1Structured(),
binary: new V1Binary()
},
v03: {
structured: new V03Structured(),
binary: new V03Binary()
}
};
}

accept(headers, body) {
const mode = getMode(headers);
const version = getVersion(mode, headers, body);
switch (version) {
case constants.SPEC_V1:
return this.receivers.v1[mode].parse(body, headers);
case constants.SPEC_V03:
return this.receivers.v03[mode].parse(body, headers);
default:
console.error(
`Unknown spec version ${version}. Default to ${constants.SPEC_V1}`);
return this.receivers.v1[mode].parse(body, headers);
}
}
}

function getMode(headers) {
let mode = "binary";
const contentType = headers[constants.HEADER_CONTENT_TYPE];
if (contentType && contentType.startsWith(constants.MIME_CE)) {
mode = "structured";
}
return mode;
}

function getVersion(mode, headers, body) {
let version = constants.SPEC_V1; // default to 1.0

if (mode === "binary") {
// Check the headers for the version
const versionHeader = headers[constants.DEFAULT_SPEC_VERSION_HEADER];
if (versionHeader) { version = versionHeader; }
} else {
// structured mode - the version is in the body
version = body instanceof String
? JSON.parse(body).specversion : body.specversion;
}
return version;
}

module.exports = HTTPReceiver;
90 changes: 90 additions & 0 deletions test/bindings/http/promiscuous_receiver_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
const { expect } = require("chai");
const { CloudEvent, HTTPReceiver } = require("../../../index.js");
const constants = require("../../../lib/bindings/http/constants.js");

const receiver = new HTTPReceiver();
const id = "1234";
const type = "org.cncf.cloudevents.test";
const source = "urn:event:from:myapi/resourse/123";
const data = {
lunch: "sushi"
};

describe("HTTP Transport Binding Receiver for CloudEvents", () => {
describe("V1", () => {
const specversion = "1.0";
lance marked this conversation as resolved.
Show resolved Hide resolved

it("Structured data returns a CloudEvent", () => {
const payload = {
id,
type,
source,
data,
specversion
};

const headers = {
[constants.HEADER_CONTENT_TYPE]: constants.MIME_CE_JSON
};

const event = receiver.accept(headers, payload);
validateEvent(event, specversion);
});

it("Binary data returns a CloudEvent", () => {
const headers = {
[constants.HEADER_CONTENT_TYPE]: constants.DEFAULT_CONTENT_TYPE,
[constants.DEFAULT_SPEC_VERSION_HEADER]: specversion,
[constants.BINARY_HEADERS_1.ID]: id,
[constants.BINARY_HEADERS_1.TYPE]: type,
[constants.BINARY_HEADERS_1.SOURCE]: source
};

const event = receiver.accept(headers, data);
validateEvent(event, specversion);
});
});

describe("V03", () => {
const specversion = "0.3";
lance marked this conversation as resolved.
Show resolved Hide resolved

it("Structured data returns a CloudEvent", () => {
const payload = {
id,
type,
source,
data,
specversion
};

const headers = {
[constants.HEADER_CONTENT_TYPE]: constants.MIME_CE_JSON
};

const event = receiver.accept(headers, payload);
validateEvent(event, specversion);
});

it("Binary data returns a CloudEvent", () => {
const headers = {
[constants.HEADER_CONTENT_TYPE]: constants.DEFAULT_CONTENT_TYPE,
[constants.DEFAULT_SPEC_VERSION_HEADER]: specversion,
[constants.BINARY_HEADERS_03.ID]: id,
[constants.BINARY_HEADERS_03.TYPE]: type,
[constants.BINARY_HEADERS_03.SOURCE]: source
};

const event = receiver.accept(headers, data);
validateEvent(event, specversion);
});
});
});

function validateEvent(event, specversion) {
expect(event instanceof CloudEvent).to.equal(true);
expect(event.getId()).to.equal(id);
expect(event.getType()).to.equal(type);
expect(event.getSource()).to.equal(source);
expect(event.getData()).to.deep.equal(data);
expect(event.getSpecversion()).to.equal(specversion);
}
2 changes: 1 addition & 1 deletion test/bindings/http/receiver_structured_1_test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const expect = require("chai").expect;
const v1 = require("../../../v1/index.js");
const CloudEvent = require("../../../index.js");
const { CloudEvent } = require("../../../index.js");

const { asBase64 } = require("../../../lib/utils/fun.js");

Expand Down
2 changes: 1 addition & 1 deletion test/bindings/http/unmarshaller_0_3_tests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const expect = require("chai").expect;
const Unmarshaller = require("../../../lib/bindings/http/unmarshaller_0_3.js");
const CloudEvent = require("../../../index.js");
const { CloudEvent } = require("../../../index.js");
const v03 = require("../../../v03/index.js");

const type = "com.github.pull.create";
Expand Down
2 changes: 1 addition & 1 deletion test/spec_0_3_tests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const expect = require("chai").expect;
const Spec03 = require("../lib/specs/spec_0_3.js");
const CloudEvent = require("../index.js");
const { CloudEvent } = require("../index.js");
const { v4: uuidv4 } = require("uuid");

const id = uuidv4();
Expand Down
2 changes: 1 addition & 1 deletion test/spec_1_tests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const expect = require("chai").expect;
const Spec1 = require("../lib/specs/spec_1.js");
const CloudEvent = require("../index.js");
const { CloudEvent } = require("../index.js");
const { v4: uuidv4 } = require("uuid");
const { asBase64 } = require("../lib/utils/fun.js");

Expand Down