From ef453b0486754a6dc8a08a8a1c1054ca3a05cb90 Mon Sep 17 00:00:00 2001 From: Lance Ball Date: Thu, 1 Apr 2021 16:49:11 -0400 Subject: [PATCH] fix: defaults properly handled for emitterFor() Fixes: https://github.com/cloudevents/sdk-javascript/issues/391 Signed-off-by: Lance Ball --- src/transport/emitter.ts | 5 +-- test/integration/emitter_factory_test.ts | 41 ++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/transport/emitter.ts b/src/transport/emitter.ts index 617f8d1..2dc132e 100644 --- a/src/transport/emitter.ts +++ b/src/transport/emitter.ts @@ -28,6 +28,7 @@ export interface TransportFunction { (message: Message, options?: Options): Promise; } +const emitterDefaults = { binding: HTTP, mode: Mode.BINARY }; /** * emitterFactory creates and returns an EmitterFunction using the supplied * TransportFunction. The returned EmitterFunction will invoke the Binding's @@ -41,11 +42,11 @@ export interface TransportFunction { * @param {Mode} options.mode the encoding mode (Mode.BINARY or Mode.STRUCTURED) * @returns {EmitterFunction} an EmitterFunction to send events with */ -export function emitterFor(fn: TransportFunction, options = { binding: HTTP, mode: Mode.BINARY }): EmitterFunction { +export function emitterFor(fn: TransportFunction, options = emitterDefaults): EmitterFunction { if (!fn) { throw new TypeError("A TransportFunction is required"); } - const { binding, mode } = options; + const { binding, mode } = { ...emitterDefaults, ...options }; return function emit(event: CloudEvent, opts?: Options): Promise { opts = opts || {}; diff --git a/test/integration/emitter_factory_test.ts b/test/integration/emitter_factory_test.ts index 31b2e29..e251055 100644 --- a/test/integration/emitter_factory_test.ts +++ b/test/integration/emitter_factory_test.ts @@ -59,6 +59,47 @@ function gotEmitter(message: Message, options?: Options): Promise { ); } +describe("emitterFor() defaults", () => { + it("Defaults to HTTP binding, binary mode", () => { + function transport(message: Message): Promise { + // A binary message will have the source attribute as a header + expect(message.headers[CONSTANTS.CE_HEADERS.TYPE]).to.equal("emitter.test"); + return Promise.resolve(); + } + const emitter = emitterFor(transport); + emitter( + new CloudEvent({ + id: "1234", + source: "/emitter/test", + type: "emitter.test", + }), + ); + }); + + it("Supports HTTP binding, structured mode", () => { + function transport(message: Message): Promise { + console.error(message); + // A binary message will have the source attribute as a header + expect(message.headers["content-type"]).to.equal(CONSTANTS.DEFAULT_CE_CONTENT_TYPE); + const body = JSON.parse(message.body as string); + /* @ts-ignore */ + expect(body.id).to.equal("1234"); + return Promise.resolve(); + } + // Ignore the next line to ensure that HTTP transport is still the default. + // Otherwise, tslint would complain that the param did not have `binding: ` + /* @ts-ignore */ + const emitter = emitterFor(transport, { mode: Mode.STRUCTURED }); + emitter( + new CloudEvent({ + id: "1234", + source: "/emitter/test", + type: "emitter.test", + }), + ); + }); +}); + describe("HTTP Transport Binding for emitterFactory", () => { beforeEach(() => { nock(sink)