Skip to content

Commit

Permalink
Merge pull request #12 from zabertech/10270--defaults-for-reg-sub
Browse files Browse the repository at this point in the history
10270 - reasonable defaults for args, kwargs, details for reg/sub handlers
  • Loading branch information
zaberSatnam authored Sep 23, 2022
2 parents f22d0ae + db67592 commit 33dea25
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "swampyer",
"version": "1.4.2",
"version": "1.4.3",
"description": "A lightweight WAMP client implementing the WAMP v2 basic profile",
"main": "lib/index.js",
"module": "lib/index.js",
Expand Down
38 changes: 38 additions & 0 deletions src/swampyer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,28 @@ describe(`${Swampyer.prototype.register.name}()`, () => {
});
});

it('provides reasonable defaults for args, kwargs, and details if they are undefined in the invocation data', async () => {
const regHandler = jest.fn().mockImplementation(async () => 'fancy result');

const regPromise = wamp.register('com.test.something', regHandler);

const regRequest = await transportProvider.transport.read();
expect(regRequest).toEqual([MessageTypes.Register, expect.any(Number), expect.any(Object), 'com.test.something']);
transportProvider.sendToLib(MessageTypes.Registered, [regRequest[1] as number, 1234]);

const regId = await regPromise;
expect(regId).toEqual(1234);

const args = undefined;
const kwargs = undefined;
const details = undefined;
transportProvider.sendToLib(MessageTypes.Invocation, [5656, 1234, details!, args!, kwargs!]);
await waitUntilPass(() => expect(regHandler).toBeCalledTimes(1));
expect(regHandler).toBeCalledWith([], {}, {});

expect(await transportProvider.transport.read()).toEqual([MessageTypes.Yield, 5656, {}, ['fancy result'], {}]);
});

it('multiple reigstrations are kept separate and handled properly when a call() is made for them', async () => {
const regHandler1 = jest.fn().mockResolvedValue('fancy result 1');
const regPromise1 = wamp.register('com.test.something', regHandler1);
Expand Down Expand Up @@ -804,6 +826,22 @@ describe(`${Swampyer.prototype.subscribe.name}()`, () => {
expect(subHandler2).toBeCalledWith(['for 2nd sub'], {}, {});
});

it('provides reasonable defaults for args, kwargs, and details if they are undefined in the event data', async () => {
const subHandler = jest.fn();
const promise = wamp.subscribe('com.some.uri', subHandler);
const request = await transportProvider.transport.read();
expect(request).toEqual([MessageTypes.Subscribe, expect.any(Number), expect.any(Object), 'com.some.uri']);
transportProvider.sendToLib(MessageTypes.Subscribed, [request[1] as number, 1234]);
await promise;

const args = undefined;
const kwargs = undefined;
const details = undefined;
transportProvider.sendToLib(MessageTypes.Event, [1234, 5555, details!, args!, kwargs!]);
expect(subHandler).toBeCalledTimes(1);
expect(subHandler).toBeCalledWith([], {}, {});
});

it('throws an error if subscription fails', async () => {
const subHandler = jest.fn();
const promise = wamp.subscribe('com.some.uri', subHandler);
Expand Down
4 changes: 2 additions & 2 deletions src/swampyer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ export class Swampyer {
const [subscriptionId, , details, args, kwargs] = data as MessageData[MessageTypes.Event];
const { uri, handler } = this.subscriptionHandlers[subscriptionId] || {};
try {
handler?.(args, kwargs, details);
handler?.(args ?? [], kwargs ?? {}, details ?? {});
} catch (e) {
// eslint-disable-next-line no-console
console.error(`An unhandled error occurred while running subscription handler for "${uri}"`, e);
Expand All @@ -407,7 +407,7 @@ export class Swampyer {
[MessageTypes.Invocation, requestId, {}, 'com.error.unavailable', ['No handler available for this request'], {}]
);
} else {
Promise.resolve((async () => handler(args, kwargs, details))())
Promise.resolve((async () => handler(args ?? [], kwargs ?? {}, details ?? {}))())
.then(result => this.transport!._send(MessageTypes.Yield, [requestId, {}, [result], {}]))
.catch(e => {
// eslint-disable-next-line no-console
Expand Down

0 comments on commit 33dea25

Please sign in to comment.