Skip to content

lib: change Receiver#accept() to be static Receiver.accept #271

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
Jul 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
52 changes: 15 additions & 37 deletions src/transport/receiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { BinaryHTTPReceiver as BinaryReceiver } from "./http/binary_receiver";
import { StructuredHTTPReceiver as StructuredReceiver } from "./http/structured_receiver";
import { CloudEventV03 } from "../event/v03";
import { CloudEventV1 } from "../event/v1";
import { Protocol } from "./protocols";
import CONSTANTS from "../constants";

/**
Expand All @@ -15,42 +14,21 @@ export enum Mode {
STRUCTURED = "structured",
}

const receivers = {
v1: {
structured: new StructuredReceiver(Version.V1),
binary: new BinaryReceiver(Version.V1),
},
v03: {
structured: new StructuredReceiver(Version.V03),
binary: new BinaryReceiver(Version.V03),
},
};

/**
* A class to receive a CloudEvent from an HTTP POST request.
*/
export class Receiver {
protocol: Protocol;
receivers: {
v1: {
structured: StructuredReceiver;
binary: BinaryReceiver;
[key: string]: unknown;
};
v03: {
structured: StructuredReceiver;
binary: BinaryReceiver;
[key: string]: unknown;
};
};

/**
* Create an instance of an HTTPReceiver to accept incoming CloudEvents.
* @param {Protocol} protocol the transport protocol - currently only Protocol.HTTP is supported
*/
constructor(protocol: Protocol = Protocol.HTTP) {
// currently unused, but reserved for future protocol implementations
this.protocol = protocol;
this.receivers = {
v1: {
structured: new StructuredReceiver(Version.V1),
binary: new BinaryReceiver(Version.V1),
},
v03: {
structured: new StructuredReceiver(Version.V03),
binary: new BinaryReceiver(Version.V03),
},
};
}
/**
* Acceptor for an incoming HTTP CloudEvent POST. Can process
* binary and structured incoming CloudEvents.
Expand All @@ -59,7 +37,7 @@ export class Receiver {
* @param {Object|JSON} body The body of the HTTP request
* @return {CloudEvent} A new {CloudEvent} instance
*/
accept(
static accept(
headers: Headers,
body: string | Record<string, unknown> | CloudEventV1 | CloudEventV03 | undefined | null,
): CloudEvent {
Expand All @@ -68,12 +46,12 @@ export class Receiver {
const version = getVersion(mode, cleanHeaders, body);
switch (version) {
case Version.V1:
return this.receivers.v1[mode].parse(body, headers);
return receivers.v1[mode].parse(body, headers);
case Version.V03:
return this.receivers.v03[mode].parse(body, headers);
return receivers.v03[mode].parse(body, headers);
default:
console.error(`Unknown spec version ${version}. Default to ${Version.V1}`);
return this.receivers.v1[mode].parse(body, headers);
return receivers.v1[mode].parse(body, headers);
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions test/conformance/steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { Receiver } from "../../src";
const { HTTPParser } = require("http-parser-js");

const parser = new HTTPParser(HTTPParser.REQUEST);
const receiver = new Receiver();

Given("HTTP Protocol Binding is supported", function (this: World) {
return true;
Expand All @@ -28,7 +27,7 @@ Given("an HTTP request", function (request: string) {
});

When("parsed as HTTP request", function () {
this.cloudevent = receiver.accept(this.headers, this.body);
this.cloudevent = Receiver.accept(this.headers, this.body);
return true;
});

Expand Down
25 changes: 12 additions & 13 deletions test/integration/http_receiver_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { expect } from "chai";
import { CloudEvent, Receiver, ValidationError } from "../../src";
import { CloudEventV1 } from "../../src/event/v1";

const receiver = new Receiver();
const id = "1234";
const type = "org.cncf.cloudevents.test";
const source = "urn:event:from:myapi/resourse/123";
Expand All @@ -22,7 +21,7 @@ describe("HTTP Transport Binding Receiver for CloudEvents", () => {
specversion,
};

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

it("Converts the JSON body of a binary event to an Object", () => {
Expand All @@ -34,7 +33,7 @@ describe("HTTP Transport Binding Receiver for CloudEvents", () => {
"ce-source": source,
};

const event: CloudEvent = receiver.accept(binaryHeaders, data);
const event: CloudEvent = Receiver.accept(binaryHeaders, data);
expect(typeof event.data).to.equal("object");
expect((event.data as Record<string, string>).lunch).to.equal("sushi");
});
Expand All @@ -47,7 +46,7 @@ describe("HTTP Transport Binding Receiver for CloudEvents", () => {
"ce-type": type,
"ce-source": source,
};
const event = receiver.accept(binaryHeaders, undefined);
const event = Receiver.accept(binaryHeaders, undefined);
expect(event.data).to.be.undefined;
});

Expand All @@ -59,7 +58,7 @@ describe("HTTP Transport Binding Receiver for CloudEvents", () => {
"ce-type": type,
"ce-source": source,
};
const event = receiver.accept(binaryHeaders, null);
const event = Receiver.accept(binaryHeaders, null);
expect(event.data).to.be.undefined;
});

Expand All @@ -72,7 +71,7 @@ describe("HTTP Transport Binding Receiver for CloudEvents", () => {
specversion,
};

const event = receiver.accept(structuredHeaders, payload);
const event = Receiver.accept(structuredHeaders, payload);
expect(typeof event.data).to.equal("object");
expect((event.data as Record<string, string>).lunch).to.equal("sushi");
});
Expand All @@ -86,7 +85,7 @@ describe("HTTP Transport Binding Receiver for CloudEvents", () => {
"ce-source": source,
};

const event: CloudEvent = receiver.accept(binaryHeaders, data);
const event: CloudEvent = Receiver.accept(binaryHeaders, data);
expect(event.validate()).to.be.true;
expect((event.data as Record<string, string>).lunch).to.equal("sushi");
});
Expand All @@ -101,7 +100,7 @@ describe("HTTP Transport Binding Receiver for CloudEvents", () => {
specversion,
};

const event: CloudEvent = receiver.accept(structuredHeaders, payload);
const event: CloudEvent = Receiver.accept(structuredHeaders, payload);
expect(event.validate()).to.be.true;
expect((event.data as Record<string, string>).lunch).to.equal("sushi");
});
Expand All @@ -119,7 +118,7 @@ describe("HTTP Transport Binding Receiver for CloudEvents", () => {
specversion,
};

const event = receiver.accept(structuredHeaders, payload);
const event = Receiver.accept(structuredHeaders, payload);
validateEvent(event, specversion);
});

Expand All @@ -132,7 +131,7 @@ describe("HTTP Transport Binding Receiver for CloudEvents", () => {
"ce-source": source,
};

const event = receiver.accept(binaryHeaders, data);
const event = Receiver.accept(binaryHeaders, data);
validateEvent(event, specversion);
});
});
Expand All @@ -149,7 +148,7 @@ describe("HTTP Transport Binding Receiver for CloudEvents", () => {
specversion,
};

const event = receiver.accept(structuredHeaders, payload);
const event = Receiver.accept(structuredHeaders, payload);
validateEvent(event, specversion);
});

Expand All @@ -162,7 +161,7 @@ describe("HTTP Transport Binding Receiver for CloudEvents", () => {
"ce-source": source,
};

const event = receiver.accept(binaryHeaders, data);
const event = Receiver.accept(binaryHeaders, data);
validateEvent(event, specversion);
});
});
Expand Down Expand Up @@ -192,7 +191,7 @@ describe("HTTP Transport Binding Receiver for CloudEvents", () => {
"x-forwarded-proto": "http",
"x-request-id": "d3649c1b-a968-40bf-a9da-3e853abc0c8b",
};
const event = receiver.accept(headers, data);
const event = Receiver.accept(headers, data);
expect(event instanceof CloudEvent).to.equal(true);
expect(event.id).to.equal(id);
expect(event.type).to.equal(type);
Expand Down