Skip to content

Commit e653ca5

Browse files
committed
fix: throw "no cloud event detected" if one can't be read
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 <[email protected]>
1 parent c1fda94 commit e653ca5

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

lib/bindings/http/http_receiver.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,14 @@ class HTTPReceiver {
3535
}
3636

3737
function getMode(headers) {
38-
let mode = "binary";
38+
let mode = "unknown";
3939
const contentType = headers[constants.HEADER_CONTENT_TYPE];
4040
if (contentType && contentType.startsWith(constants.MIME_CE)) {
4141
mode = "structured";
42+
} else if (headers[constants.BINARY_HEADERS_1.ID]) {
43+
mode = "binary";
44+
} else {
45+
throw new TypeError("no cloud event detected");
4246
}
4347
return mode;
4448
}

test/bindings/http/promiscuous_receiver_test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,22 @@ const data = {
1111
};
1212

1313
describe("HTTP Transport Binding Receiver for CloudEvents", () => {
14+
describe("HTTP CloudEvent format detection", () => {
15+
const specversion = "1.0";
16+
it("Throws when the event format cannot be detected", () => {
17+
const payload = {
18+
id,
19+
type,
20+
source,
21+
data,
22+
specversion
23+
};
24+
25+
expect(receiver.accept.bind(receiver, {}, payload))
26+
.to.throw("no cloud event detected");
27+
});
28+
});
29+
1430
describe("V1", () => {
1531
const specversion = "1.0";
1632

0 commit comments

Comments
 (0)