Skip to content

lib: make HTTPEmitter headers method a property of the class #186

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

Merged
merged 1 commit into from
May 24, 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
45 changes: 27 additions & 18 deletions lib/bindings/http/http_emitter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const BinaryHTTPEmitter = require("./emitter_binary.js");
const StructuredEmitter = require("./emitter_structured.js");
const EmitterV1 = require("./v1").BinaryEmitter;
const EmitterV03 = require("./v03").BinaryEmitter;

/** @typedef {import("../../cloudevent")} CloudEvent */

Expand Down Expand Up @@ -66,26 +68,33 @@ class HTTPEmitter {
}
throw new TypeError(`Unknown transport mode ${mode}.`);
}
}

/**
* Returns the HTTP headers that will be sent for this event when the HTTP transmission
* mode is "binary". Events sent over HTTP in structured mode only have a single CE header
* and that is "ce-id", corresponding to the event ID.
* @param {CloudEvent} event a CloudEvent
* @returns {Object} the headers that will be sent for the event
*/
headers(event) {
const headers = {};

this.binary.headerParserMap.forEach((parser, getterName) => {
const value = event[getterName];
if (value) {
headers[parser.headerName] = parser.parse(value);
}
});

return headers;
/**
* Returns the HTTP headers that will be sent for this event when the HTTP transmission
* mode is "binary". Events sent over HTTP in structured mode only have a single CE header
* and that is "ce-id", corresponding to the event ID.
* @param {CloudEvent} event a CloudEvent
* @param {string} [version] spec version number - default 1.0
* @returns {Object} the headers that will be sent for the event
*/
function headers(event, version = SPEC_V1) {
const headers = {};
let headerMap;
if (version === SPEC_V1) {
headerMap = EmitterV1;
} else if (version === SPEC_V03) {
headerMap = EmitterV03;
}
headerMap.forEach((parser, getterName) => {
const value = event[getterName];
if (value) {
headers[parser.headerName] = parser.parse(value);
}
});

return headers;
}

HTTPEmitter.headers = headers;
module.exports = HTTPEmitter;
7 changes: 2 additions & 5 deletions test/bindings/http/http_emitter_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ const {

const { CloudEvent, HTTPEmitter } = require("../../../");

const V1Spec = require("../../../lib/bindings/http/v1").Spec;
const V03Spec = require("../../../lib/bindings/http/v03").Spec;

const receiver = "https://cloudevents.io/";
const type = "com.example.test";
const source = "urn:event:from:myapi/resource/123";
Expand Down Expand Up @@ -67,7 +64,7 @@ describe("HTTP Transport Binding Emitter for CloudEvents", () => {
});

it("Provides the HTTP headers for a binary event", () => {
const headers = emitter.headers(event);
const headers = HTTPEmitter.headers(event);
expect(headers[BINARY_HEADERS_1.TYPE]).to.equal(event.type);
expect(headers[BINARY_HEADERS_1.SPEC_VERSION]).to.equal(event.specversion);
expect(headers[BINARY_HEADERS_1.SOURCE]).to.equal(event.source);
Expand Down Expand Up @@ -140,7 +137,7 @@ describe("HTTP Transport Binding Emitter for CloudEvents", () => {
});

it("Provides the HTTP headers for a binary event", () => {
const headers = emitter.headers(event);
const headers = HTTPEmitter.headers(event, SPEC_V03);
expect(headers[BINARY_HEADERS_03.TYPE]).to.equal(event.type);
expect(headers[BINARY_HEADERS_03.SPEC_VERSION]).to.equal(event.specversion);
expect(headers[BINARY_HEADERS_03.SOURCE]).to.equal(event.source);
Expand Down