2021-03-18 19:23:46 +01:00
|
|
|
import { obj as createThoughStream } from 'through2';
|
|
|
|
import createMetaRPCHandler from './createMetaRPCHandler';
|
|
|
|
|
2021-12-06 17:40:39 +01:00
|
|
|
describe('createMetaRPCHandler', () => {
|
|
|
|
it('can call the api when handler receives a JSON-RPC request', () => {
|
2021-03-18 19:23:46 +01:00
|
|
|
const api = {
|
|
|
|
foo: (param1) => {
|
2021-12-06 17:40:39 +01:00
|
|
|
expect(param1).toStrictEqual('bar');
|
2021-03-18 19:23:46 +01:00
|
|
|
},
|
|
|
|
};
|
|
|
|
const streamTest = createThoughStream();
|
|
|
|
const handler = createMetaRPCHandler(api, streamTest);
|
|
|
|
handler({
|
|
|
|
id: 1,
|
|
|
|
method: 'foo',
|
|
|
|
params: ['bar'],
|
|
|
|
});
|
|
|
|
});
|
2021-12-08 22:36:53 +01:00
|
|
|
it('can write the response to the outstream', () => {
|
2021-03-18 19:23:46 +01:00
|
|
|
const api = {
|
2021-12-08 22:36:53 +01:00
|
|
|
foo: (param1) => {
|
|
|
|
expect(param1).toStrictEqual('bar');
|
|
|
|
return 'foobarbaz';
|
|
|
|
},
|
|
|
|
};
|
|
|
|
const streamTest = createThoughStream();
|
|
|
|
const handler = createMetaRPCHandler(api, streamTest);
|
|
|
|
handler({
|
|
|
|
id: 1,
|
|
|
|
method: 'foo',
|
|
|
|
params: ['bar'],
|
|
|
|
});
|
|
|
|
streamTest.on('data', (data) => {
|
|
|
|
expect(data.result).toStrictEqual('foobarbaz');
|
|
|
|
streamTest.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('can write an async response to the outstream', () => {
|
|
|
|
const api = {
|
|
|
|
foo: async (param1) => {
|
2021-12-06 17:40:39 +01:00
|
|
|
expect(param1).toStrictEqual('bar');
|
2021-12-08 22:36:53 +01:00
|
|
|
await new Promise((resolve) => setTimeout(() => resolve(), 100));
|
|
|
|
return 'foobarbaz';
|
2021-03-18 19:23:46 +01:00
|
|
|
},
|
|
|
|
};
|
|
|
|
const streamTest = createThoughStream();
|
|
|
|
const handler = createMetaRPCHandler(api, streamTest);
|
|
|
|
handler({
|
|
|
|
id: 1,
|
|
|
|
method: 'foo',
|
|
|
|
params: ['bar'],
|
|
|
|
});
|
|
|
|
streamTest.on('data', (data) => {
|
2021-12-06 17:40:39 +01:00
|
|
|
expect(data.result).toStrictEqual('foobarbaz');
|
2021-03-18 19:23:46 +01:00
|
|
|
streamTest.end();
|
|
|
|
});
|
|
|
|
});
|
2021-12-08 22:36:53 +01:00
|
|
|
it('can write the error to the outstream when method throws an error', () => {
|
2021-03-18 19:23:46 +01:00
|
|
|
const api = {
|
2021-12-08 22:36:53 +01:00
|
|
|
foo: (param1) => {
|
2021-12-06 17:40:39 +01:00
|
|
|
expect(param1).toStrictEqual('bar');
|
2021-12-08 22:36:53 +01:00
|
|
|
throw new Error('foo-error');
|
2021-03-18 19:23:46 +01:00
|
|
|
},
|
|
|
|
};
|
|
|
|
const streamTest = createThoughStream();
|
|
|
|
const handler = createMetaRPCHandler(api, streamTest);
|
|
|
|
handler({
|
|
|
|
id: 1,
|
|
|
|
method: 'foo',
|
|
|
|
params: ['bar'],
|
|
|
|
});
|
|
|
|
streamTest.on('data', (data) => {
|
2021-12-06 17:40:39 +01:00
|
|
|
expect(data.error.message).toStrictEqual('foo-error');
|
2021-03-18 19:23:46 +01:00
|
|
|
streamTest.end();
|
|
|
|
});
|
|
|
|
});
|
2021-12-06 17:40:39 +01:00
|
|
|
it('can not throw an error for writing an error after end', () => {
|
2021-05-13 00:12:46 +02:00
|
|
|
const api = {
|
2021-12-08 22:36:53 +01:00
|
|
|
foo: (param1) => {
|
2021-12-06 17:40:39 +01:00
|
|
|
expect(param1).toStrictEqual('bar');
|
2021-12-08 22:36:53 +01:00
|
|
|
throw new Error('foo-error');
|
2021-05-13 00:12:46 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
const streamTest = createThoughStream();
|
|
|
|
const handler = createMetaRPCHandler(api, streamTest);
|
|
|
|
streamTest.end();
|
2021-12-06 17:40:39 +01:00
|
|
|
expect(() => {
|
|
|
|
handler({
|
|
|
|
id: 1,
|
|
|
|
method: 'foo',
|
|
|
|
params: ['bar'],
|
|
|
|
});
|
|
|
|
}).not.toThrow();
|
2021-05-13 00:12:46 +02:00
|
|
|
});
|
2021-12-06 17:40:39 +01:00
|
|
|
it('can not throw an error for write after end', () => {
|
2021-05-13 00:12:46 +02:00
|
|
|
const api = {
|
2021-12-08 22:36:53 +01:00
|
|
|
foo: (param1) => {
|
2021-12-06 17:40:39 +01:00
|
|
|
expect(param1).toStrictEqual('bar');
|
2021-12-08 22:36:53 +01:00
|
|
|
return {
|
2021-05-13 00:12:46 +02:00
|
|
|
foo: 'bar',
|
2021-12-08 22:36:53 +01:00
|
|
|
};
|
2021-05-13 00:12:46 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
const streamTest = createThoughStream();
|
|
|
|
const handler = createMetaRPCHandler(api, streamTest);
|
|
|
|
streamTest.end();
|
2021-12-06 17:40:39 +01:00
|
|
|
expect(() => {
|
|
|
|
handler({
|
|
|
|
id: 1,
|
|
|
|
method: 'foo',
|
|
|
|
params: ['bar'],
|
|
|
|
});
|
|
|
|
}).not.toThrow();
|
2021-05-13 00:12:46 +02:00
|
|
|
});
|
2021-03-18 19:23:46 +01:00
|
|
|
});
|