mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-30 08:09:15 +01:00
cacefc72fc
* return early from metaRPCHandler if stream has ended * add write after end guard to handleUpdate
98 lines
2.6 KiB
JavaScript
98 lines
2.6 KiB
JavaScript
import { strict as assert } from 'assert';
|
|
import { obj as createThoughStream } from 'through2';
|
|
import createMetaRPCHandler from './createMetaRPCHandler';
|
|
|
|
describe('createMetaRPCHandler', function () {
|
|
it('can call the api when handler receives a JSON-RPC request', function (done) {
|
|
const api = {
|
|
foo: (param1) => {
|
|
assert.strictEqual(param1, 'bar');
|
|
done();
|
|
},
|
|
};
|
|
const streamTest = createThoughStream();
|
|
const handler = createMetaRPCHandler(api, streamTest);
|
|
handler({
|
|
id: 1,
|
|
method: 'foo',
|
|
params: ['bar'],
|
|
});
|
|
});
|
|
it('can write the response to the outstream when api callback is called', function (done) {
|
|
const api = {
|
|
foo: (param1, cb) => {
|
|
assert.strictEqual(param1, 'bar');
|
|
cb(null, 'foobarbaz');
|
|
},
|
|
};
|
|
const streamTest = createThoughStream();
|
|
const handler = createMetaRPCHandler(api, streamTest);
|
|
handler({
|
|
id: 1,
|
|
method: 'foo',
|
|
params: ['bar'],
|
|
});
|
|
streamTest.on('data', (data) => {
|
|
assert.strictEqual(data.result, 'foobarbaz');
|
|
streamTest.end();
|
|
done();
|
|
});
|
|
});
|
|
it('can write the error to the outstream when api callback is called with an error', function (done) {
|
|
const api = {
|
|
foo: (param1, cb) => {
|
|
assert.strictEqual(param1, 'bar');
|
|
cb(new Error('foo-error'));
|
|
},
|
|
};
|
|
const streamTest = createThoughStream();
|
|
const handler = createMetaRPCHandler(api, streamTest);
|
|
handler({
|
|
id: 1,
|
|
method: 'foo',
|
|
params: ['bar'],
|
|
});
|
|
streamTest.on('data', (data) => {
|
|
assert.strictEqual(data.error.message, 'foo-error');
|
|
streamTest.end();
|
|
done();
|
|
});
|
|
});
|
|
it('can not throw an error for writing an error after end', function (done) {
|
|
const api = {
|
|
foo: (param1, cb) => {
|
|
assert.strictEqual(param1, 'bar');
|
|
cb(new Error('foo-error'));
|
|
},
|
|
};
|
|
const streamTest = createThoughStream();
|
|
const handler = createMetaRPCHandler(api, streamTest);
|
|
streamTest.end();
|
|
handler({
|
|
id: 1,
|
|
method: 'foo',
|
|
params: ['bar'],
|
|
});
|
|
done();
|
|
});
|
|
it('can not throw an error for write after end', function (done) {
|
|
const api = {
|
|
foo: (param1, cb) => {
|
|
assert.strictEqual(param1, 'bar');
|
|
cb(undefined, {
|
|
foo: 'bar',
|
|
});
|
|
},
|
|
};
|
|
const streamTest = createThoughStream();
|
|
const handler = createMetaRPCHandler(api, streamTest);
|
|
streamTest.end();
|
|
handler({
|
|
id: 1,
|
|
method: 'foo',
|
|
params: ['bar'],
|
|
});
|
|
done();
|
|
});
|
|
});
|