Skip to content

Commit f50e80f

Browse files
authored
lib: make HTTPEmitter headers method a property of the class (#186)
Signed-off-by: Lance Ball <[email protected]>
1 parent abc114b commit f50e80f

File tree

2 files changed

+29
-23
lines changed

2 files changed

+29
-23
lines changed

lib/bindings/http/http_emitter.js

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const BinaryHTTPEmitter = require("./emitter_binary.js");
22
const StructuredEmitter = require("./emitter_structured.js");
3+
const EmitterV1 = require("./v1").BinaryEmitter;
4+
const EmitterV03 = require("./v03").BinaryEmitter;
35

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

@@ -66,26 +68,33 @@ class HTTPEmitter {
6668
}
6769
throw new TypeError(`Unknown transport mode ${mode}.`);
6870
}
71+
}
6972

70-
/**
71-
* Returns the HTTP headers that will be sent for this event when the HTTP transmission
72-
* mode is "binary". Events sent over HTTP in structured mode only have a single CE header
73-
* and that is "ce-id", corresponding to the event ID.
74-
* @param {CloudEvent} event a CloudEvent
75-
* @returns {Object} the headers that will be sent for the event
76-
*/
77-
headers(event) {
78-
const headers = {};
79-
80-
this.binary.headerParserMap.forEach((parser, getterName) => {
81-
const value = event[getterName];
82-
if (value) {
83-
headers[parser.headerName] = parser.parse(value);
84-
}
85-
});
86-
87-
return headers;
73+
/**
74+
* Returns the HTTP headers that will be sent for this event when the HTTP transmission
75+
* mode is "binary". Events sent over HTTP in structured mode only have a single CE header
76+
* and that is "ce-id", corresponding to the event ID.
77+
* @param {CloudEvent} event a CloudEvent
78+
* @param {string} [version] spec version number - default 1.0
79+
* @returns {Object} the headers that will be sent for the event
80+
*/
81+
function headers(event, version = SPEC_V1) {
82+
const headers = {};
83+
let headerMap;
84+
if (version === SPEC_V1) {
85+
headerMap = EmitterV1;
86+
} else if (version === SPEC_V03) {
87+
headerMap = EmitterV03;
8888
}
89+
headerMap.forEach((parser, getterName) => {
90+
const value = event[getterName];
91+
if (value) {
92+
headers[parser.headerName] = parser.parse(value);
93+
}
94+
});
95+
96+
return headers;
8997
}
9098

99+
HTTPEmitter.headers = headers;
91100
module.exports = HTTPEmitter;

test/bindings/http/http_emitter_test.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ const {
1111

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

14-
const V1Spec = require("../../../lib/bindings/http/v1").Spec;
15-
const V03Spec = require("../../../lib/bindings/http/v03").Spec;
16-
1714
const receiver = "https://cloudevents.io/";
1815
const type = "com.example.test";
1916
const source = "urn:event:from:myapi/resource/123";
@@ -67,7 +64,7 @@ describe("HTTP Transport Binding Emitter for CloudEvents", () => {
6764
});
6865

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

142139
it("Provides the HTTP headers for a binary event", () => {
143-
const headers = emitter.headers(event);
140+
const headers = HTTPEmitter.headers(event, SPEC_V03);
144141
expect(headers[BINARY_HEADERS_03.TYPE]).to.equal(event.type);
145142
expect(headers[BINARY_HEADERS_03.SPEC_VERSION]).to.equal(event.specversion);
146143
expect(headers[BINARY_HEADERS_03.SOURCE]).to.equal(event.source);

0 commit comments

Comments
 (0)