mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-25 11:28:51 +01:00
5076e5057f
The `nodeify` utility is no longer needed for the background API. Instead each method is assumed to be either synchronous or Promise- returning. The error handling was updated to at least log the error in the case where a method fall fails after the connection is broken.
116 lines
3.0 KiB
JavaScript
116 lines
3.0 KiB
JavaScript
import { obj as createThoughStream } from 'through2';
|
|
import createMetaRPCHandler from './createMetaRPCHandler';
|
|
|
|
describe('createMetaRPCHandler', () => {
|
|
it('can call the api when handler receives a JSON-RPC request', () => {
|
|
const api = {
|
|
foo: (param1) => {
|
|
expect(param1).toStrictEqual('bar');
|
|
},
|
|
};
|
|
const streamTest = createThoughStream();
|
|
const handler = createMetaRPCHandler(api, streamTest);
|
|
handler({
|
|
id: 1,
|
|
method: 'foo',
|
|
params: ['bar'],
|
|
});
|
|
});
|
|
it('can write the response to the outstream', () => {
|
|
const api = {
|
|
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) => {
|
|
expect(param1).toStrictEqual('bar');
|
|
await new Promise((resolve) => setTimeout(() => resolve(), 100));
|
|
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 the error to the outstream when method throws an error', () => {
|
|
const api = {
|
|
foo: (param1) => {
|
|
expect(param1).toStrictEqual('bar');
|
|
throw new Error('foo-error');
|
|
},
|
|
};
|
|
const streamTest = createThoughStream();
|
|
const handler = createMetaRPCHandler(api, streamTest);
|
|
handler({
|
|
id: 1,
|
|
method: 'foo',
|
|
params: ['bar'],
|
|
});
|
|
streamTest.on('data', (data) => {
|
|
expect(data.error.message).toStrictEqual('foo-error');
|
|
streamTest.end();
|
|
});
|
|
});
|
|
it('can not throw an error for writing an error after end', () => {
|
|
const api = {
|
|
foo: (param1) => {
|
|
expect(param1).toStrictEqual('bar');
|
|
throw new Error('foo-error');
|
|
},
|
|
};
|
|
const streamTest = createThoughStream();
|
|
const handler = createMetaRPCHandler(api, streamTest);
|
|
streamTest.end();
|
|
expect(() => {
|
|
handler({
|
|
id: 1,
|
|
method: 'foo',
|
|
params: ['bar'],
|
|
});
|
|
}).not.toThrow();
|
|
});
|
|
it('can not throw an error for write after end', () => {
|
|
const api = {
|
|
foo: (param1) => {
|
|
expect(param1).toStrictEqual('bar');
|
|
return {
|
|
foo: 'bar',
|
|
};
|
|
},
|
|
};
|
|
const streamTest = createThoughStream();
|
|
const handler = createMetaRPCHandler(api, streamTest);
|
|
streamTest.end();
|
|
expect(() => {
|
|
handler({
|
|
id: 1,
|
|
method: 'foo',
|
|
params: ['bar'],
|
|
});
|
|
}).not.toThrow();
|
|
});
|
|
});
|