Skip to content

Commit ef7550d

Browse files
authored
fix: throw "no cloud event detected" if one can't be read (cloudevents#139)
This commit changes the event mode detection in `HTTPReceiver` so that it will throw a TypeError if the event mode can't be detected per the spec. Signed-off-by: Lance Ball <lball@redhat.com>
1 parent b866edd commit ef7550d

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

Diff for: lib/bindings/http/http_receiver.js

+20-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@ const V03Binary = require("./receiver_binary_0_3");
22
const V03Structured = require("./receiver_structured_0_3.js");
33
const V1Binary = require("./receiver_binary_1.js");
44
const V1Structured = require("./receiver_structured_1.js");
5-
const constants = require("./constants");
5+
const {
6+
SPEC_V03,
7+
SPEC_V1,
8+
HEADER_CONTENT_TYPE,
9+
MIME_CE,
10+
BINARY_HEADERS_1,
11+
DEFAULT_SPEC_VERSION_HEADER
12+
} = require("./constants");
613

714
class HTTPReceiver {
815
constructor() {
@@ -22,33 +29,37 @@ class HTTPReceiver {
2229
const mode = getMode(headers);
2330
const version = getVersion(mode, headers, body);
2431
switch (version) {
25-
case constants.SPEC_V1:
32+
case SPEC_V1:
2633
return this.receivers.v1[mode].parse(body, headers);
27-
case constants.SPEC_V03:
34+
case SPEC_V03:
2835
return this.receivers.v03[mode].parse(body, headers);
2936
default:
3037
console.error(
31-
`Unknown spec version ${version}. Default to ${constants.SPEC_V1}`);
38+
`Unknown spec version ${version}. Default to ${SPEC_V1}`);
3239
return this.receivers.v1[mode].parse(body, headers);
3340
}
3441
}
3542
}
3643

3744
function getMode(headers) {
38-
let mode = "binary";
39-
const contentType = headers[constants.HEADER_CONTENT_TYPE];
40-
if (contentType && contentType.startsWith(constants.MIME_CE)) {
45+
let mode = "unknown";
46+
const contentType = headers[HEADER_CONTENT_TYPE];
47+
if (contentType && contentType.startsWith(MIME_CE)) {
4148
mode = "structured";
49+
} else if (headers[BINARY_HEADERS_1.ID]) {
50+
mode = "binary";
51+
} else {
52+
throw new TypeError("no cloud event detected");
4253
}
4354
return mode;
4455
}
4556

4657
function getVersion(mode, headers, body) {
47-
let version = constants.SPEC_V1; // default to 1.0
58+
let version = SPEC_V1; // default to 1.0
4859

4960
if (mode === "binary") {
5061
// Check the headers for the version
51-
const versionHeader = headers[constants.DEFAULT_SPEC_VERSION_HEADER];
62+
const versionHeader = headers[DEFAULT_SPEC_VERSION_HEADER];
5263
if (versionHeader) { version = versionHeader; }
5364
} else {
5465
// structured mode - the version is in the body

Diff for: test/bindings/http/promiscuous_receiver_test.js

+16
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,22 @@ const data = {
1818
};
1919

2020
describe("HTTP Transport Binding Receiver for CloudEvents", () => {
21+
describe("HTTP CloudEvent format detection", () => {
22+
const specversion = "1.0";
23+
it("Throws when the event format cannot be detected", () => {
24+
const payload = {
25+
id,
26+
type,
27+
source,
28+
data,
29+
specversion
30+
};
31+
32+
expect(receiver.accept.bind(receiver, {}, payload))
33+
.to.throw("no cloud event detected");
34+
});
35+
});
36+
2137
describe("V1", () => {
2238
const specversion = "1.0";
2339

0 commit comments

Comments
 (0)