Skip to content

Commit

Permalink
fix: throw "no cloud event detected" if one can't be read (#139)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
lance authored May 11, 2020
1 parent b866edd commit ef7550d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
29 changes: 20 additions & 9 deletions lib/bindings/http/http_receiver.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ 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");
const {
SPEC_V03,
SPEC_V1,
HEADER_CONTENT_TYPE,
MIME_CE,
BINARY_HEADERS_1,
DEFAULT_SPEC_VERSION_HEADER
} = require("./constants");

class HTTPReceiver {
constructor() {
Expand All @@ -22,33 +29,37 @@ class HTTPReceiver {
const mode = getMode(headers);
const version = getVersion(mode, headers, body);
switch (version) {
case constants.SPEC_V1:
case SPEC_V1:
return this.receivers.v1[mode].parse(body, headers);
case constants.SPEC_V03:
case SPEC_V03:
return this.receivers.v03[mode].parse(body, headers);
default:
console.error(
`Unknown spec version ${version}. Default to ${constants.SPEC_V1}`);
`Unknown spec version ${version}. Default to ${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)) {
let mode = "unknown";
const contentType = headers[HEADER_CONTENT_TYPE];
if (contentType && contentType.startsWith(MIME_CE)) {
mode = "structured";
} else if (headers[BINARY_HEADERS_1.ID]) {
mode = "binary";
} else {
throw new TypeError("no cloud event detected");
}
return mode;
}

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

if (mode === "binary") {
// Check the headers for the version
const versionHeader = headers[constants.DEFAULT_SPEC_VERSION_HEADER];
const versionHeader = headers[DEFAULT_SPEC_VERSION_HEADER];
if (versionHeader) { version = versionHeader; }
} else {
// structured mode - the version is in the body
Expand Down
16 changes: 16 additions & 0 deletions test/bindings/http/promiscuous_receiver_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@ const data = {
};

describe("HTTP Transport Binding Receiver for CloudEvents", () => {
describe("HTTP CloudEvent format detection", () => {
const specversion = "1.0";
it("Throws when the event format cannot be detected", () => {
const payload = {
id,
type,
source,
data,
specversion
};

expect(receiver.accept.bind(receiver, {}, payload))
.to.throw("no cloud event detected");
});
});

describe("V1", () => {
const specversion = "1.0";

Expand Down

0 comments on commit ef7550d

Please # to comment.