Split providers into @polkadot/api-provider (#5)
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
# Polkadot JavaScript API
|
||||
# API wrapper for RPC calls
|
||||
|
||||
[](https://travis-ci.org/polkadot-js/api)
|
||||
[](https://coveralls.io/github/polkadot-js/api?branch=master)
|
||||
@@ -21,8 +21,9 @@ Initialisation -
|
||||
|
||||
```
|
||||
import Api from '@polkadot/api';
|
||||
import HttpProvider from '@polkadot/api-provider/http';
|
||||
|
||||
const provider = new Api.HttpProvider('http://127.0.0.1:9933');
|
||||
const provider = new HttpProvider('http://127.0.0.1:9933');
|
||||
const api = new Api(provider);
|
||||
```
|
||||
|
||||
|
||||
Generated
+937
-79
File diff suppressed because it is too large
Load Diff
+3
-5
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@polkadot/api",
|
||||
"version": "0.3.1",
|
||||
"version": "0.4.0",
|
||||
"description": "A JavaScript wrapper for the Polkadot JsonRPC interface",
|
||||
"main": "index.js",
|
||||
"keywords": [
|
||||
@@ -35,6 +35,7 @@
|
||||
"test": "cross-env NODE_ENV=test babel-istanbul cover _mocha 'src/*.spec.js' 'src/**/*.spec.js'"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@polkadot/api-provider": "~0.1.0",
|
||||
"babel-cli": "^6.26.0",
|
||||
"babel-core": "^6.26.0",
|
||||
"babel-eslint": "^8.0.2",
|
||||
@@ -60,8 +61,6 @@
|
||||
"flow-bin": "^0.59.0",
|
||||
"makeshift": "^1.1.0",
|
||||
"mocha": "^4.0.1",
|
||||
"mock-socket": "^7.1.0",
|
||||
"nock": "^9.1.0",
|
||||
"rimraf": "^2.6.2",
|
||||
"sinon": "^4.1.2",
|
||||
"sinon-chai": "^2.14.0"
|
||||
@@ -70,7 +69,6 @@
|
||||
"@polkadot/api-format": "~0.3.0",
|
||||
"@polkadot/jsonrpc": "~0.3.0",
|
||||
"@polkadot/util": "~0.3.0",
|
||||
"babel-runtime": "^6.26.0",
|
||||
"isomorphic-fetch": "^2.2.1"
|
||||
"babel-runtime": "^6.26.0"
|
||||
}
|
||||
}
|
||||
|
||||
+1
-5
@@ -1,17 +1,15 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
import type { ProviderInterface } from '@polkadot/api-provider/types';
|
||||
import type { InterfaceDefinition } from '@polkadot/jsonrpc/types';
|
||||
import type { ApiInterface } from './types';
|
||||
import type { ProviderInterface } from './provider/types';
|
||||
|
||||
const { formatInputs, formatOutput } = require('@polkadot/api-format');
|
||||
const interfaces = require('@polkadot/jsonrpc');
|
||||
const { callSignature } = require('@polkadot/jsonrpc/util');
|
||||
const { isFunction } = require('@polkadot/util/is');
|
||||
|
||||
const { HttpProvider } = require('./provider');
|
||||
|
||||
module.exports = class Api implements ApiInterface {
|
||||
_provider: ProviderInterface;
|
||||
_chainInterface: any = null;
|
||||
@@ -67,6 +65,4 @@ module.exports = class Api implements ApiInterface {
|
||||
return container;
|
||||
}, {});
|
||||
}
|
||||
|
||||
static HttpProvider = HttpProvider;
|
||||
};
|
||||
|
||||
@@ -109,10 +109,4 @@ describe('Api', () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('static', () => {
|
||||
it('exports Api.HttpProvider', () => {
|
||||
expect(Api.HttpProvider).to.be.ok;
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
require('./polyfill');
|
||||
|
||||
const Api = require('./api');
|
||||
|
||||
module.exports = Api;
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
if (typeof fetch === 'undefined') {
|
||||
require('isomorphic-fetch');
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
/* eslint-disable no-unused-expressions */
|
||||
|
||||
describe('polyfill', () => {
|
||||
it('adds polyfills with no exceptions', () => {
|
||||
expect(require('./polyfill')).to.be.ok;
|
||||
});
|
||||
|
||||
describe('interfaces', () => {
|
||||
it('fetch is available', () => {
|
||||
expect(fetch).to.be.ok;
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,37 +0,0 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
import type { ProviderInterface } from './types';
|
||||
|
||||
const JsonRpcCoder = require('./jsonRpcCoder');
|
||||
|
||||
module.exports = class HttpProvider extends JsonRpcCoder implements ProviderInterface {
|
||||
_endpoint: string;
|
||||
|
||||
constructor (endpoint: string) {
|
||||
super();
|
||||
|
||||
this._endpoint = endpoint;
|
||||
}
|
||||
|
||||
async send (method: string, params: Array<any>): Promise<any> {
|
||||
const body = this.encodeJson(method, params);
|
||||
const response = await fetch(this._endpoint, {
|
||||
body,
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
'Content-Length': `${body.length}`,
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
method: 'POST'
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`[${response.status}]: ${response.statusText}`);
|
||||
}
|
||||
|
||||
const result = await response.json();
|
||||
|
||||
return this.decodeResponse(result);
|
||||
}
|
||||
};
|
||||
@@ -1,97 +0,0 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
/* eslint-disable no-unused-expressions */
|
||||
|
||||
const { mockHttp, TEST_HTTP_URL } = require('../../test/mockHttp');
|
||||
|
||||
const sinon = require('sinon');
|
||||
|
||||
const Http = require('./http');
|
||||
|
||||
describe('provider/Http', () => {
|
||||
let http;
|
||||
let mock;
|
||||
|
||||
beforeEach(() => {
|
||||
http = new Http(TEST_HTTP_URL);
|
||||
|
||||
sinon.spy(http, 'encodeJson');
|
||||
sinon.spy(http, 'decodeResponse');
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
http.encodeJson.restore();
|
||||
http.decodeResponse.restore();
|
||||
|
||||
mock.done();
|
||||
});
|
||||
|
||||
describe('send', () => {
|
||||
it('encodes requests', () => {
|
||||
mock = mockHttp([{
|
||||
method: 'test_encoding',
|
||||
reply: {
|
||||
result: 'ok'
|
||||
}
|
||||
}]);
|
||||
|
||||
return http
|
||||
.send('test_encoding', ['param'])
|
||||
.then((result) => {
|
||||
expect(http.encodeJson).to.have.been.calledWith('test_encoding', ['param']);
|
||||
});
|
||||
});
|
||||
|
||||
it('decodes responses', () => {
|
||||
mock = mockHttp([{
|
||||
method: 'test_encoding',
|
||||
reply: {
|
||||
result: 'ok'
|
||||
}
|
||||
}]);
|
||||
|
||||
return http
|
||||
.send('test_encoding', ['param'])
|
||||
.then((result) => {
|
||||
expect(http.decodeResponse).to.have.been.calledWith({
|
||||
id: 1,
|
||||
jsonrpc: '2.0',
|
||||
result: 'ok'
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('passes the body through correctly', () => {
|
||||
mock = mockHttp([{
|
||||
method: 'test_body',
|
||||
reply: {
|
||||
result: 'ok'
|
||||
}
|
||||
}]);
|
||||
|
||||
return http
|
||||
.send('test_body', ['param'])
|
||||
.then((result) => {
|
||||
expect(mock.body['test_body']).to.deep.equal({
|
||||
id: 1,
|
||||
jsonrpc: '2.0',
|
||||
method: 'test_body',
|
||||
params: ['param']
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('throws error when !response.ok', () => {
|
||||
mock = mockHttp([{
|
||||
code: 500,
|
||||
method: 'test_error'
|
||||
}]);
|
||||
|
||||
return http
|
||||
.send('test_error', [])
|
||||
.catch((error) => {
|
||||
expect(error).to.match(/\[500\]: Internal Server/);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,8 +0,0 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
const HttpProvider = require('./http');
|
||||
|
||||
module.exports = {
|
||||
HttpProvider
|
||||
};
|
||||
@@ -1,55 +0,0 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
import type { JsonRpcRequest, JsonRpcResponse } from './types';
|
||||
|
||||
const { isNumber, isUndefined } = require('@polkadot/util/is');
|
||||
|
||||
module.exports = class JsonRpcCoder {
|
||||
_id: number = 0;
|
||||
|
||||
decodeResponse (response: JsonRpcResponse): any {
|
||||
if (!response) {
|
||||
throw new Error('Empty response object received');
|
||||
}
|
||||
|
||||
if (response.jsonrpc !== '2.0') {
|
||||
throw new Error('Invalid jsonrpc field in decoded object');
|
||||
}
|
||||
|
||||
if (!isNumber(response.id)) {
|
||||
throw new Error('Invalid id field in decoded object');
|
||||
}
|
||||
|
||||
if (response.error) {
|
||||
const { code, message } = response.error;
|
||||
|
||||
throw new Error(`[${code}]: ${message}`);
|
||||
}
|
||||
|
||||
if (isUndefined(response.result)) {
|
||||
throw new Error('No result found in JsonRpc response');
|
||||
}
|
||||
|
||||
return response.result;
|
||||
}
|
||||
|
||||
encodeObject (method: string, params: Array<any>): JsonRpcRequest {
|
||||
return {
|
||||
id: ++this._id,
|
||||
jsonrpc: '2.0',
|
||||
method,
|
||||
params
|
||||
};
|
||||
}
|
||||
|
||||
encodeJson (method: string, params: Array<any>): string {
|
||||
return JSON.stringify(
|
||||
this.encodeObject(method, params)
|
||||
);
|
||||
}
|
||||
|
||||
get id (): number {
|
||||
return this._id;
|
||||
}
|
||||
};
|
||||
@@ -1,77 +0,0 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const JsonRpcCoder = require('./jsonRpcCoder');
|
||||
|
||||
describe('provider/JsonRpcCoder', () => {
|
||||
let coder;
|
||||
|
||||
beforeEach(() => {
|
||||
coder = new JsonRpcCoder();
|
||||
});
|
||||
|
||||
describe('id', () => {
|
||||
it('starts with id === 0 (nothing sent)', () => {
|
||||
expect(coder.id).to.equal(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('decodeResponse', () => {
|
||||
it('expects a non-empty input object', () => {
|
||||
expect(
|
||||
() => coder.decodeResponse()
|
||||
).to.throw(/Empty response/);
|
||||
});
|
||||
|
||||
it('expects a valid jsonrpc field', () => {
|
||||
expect(
|
||||
() => coder.decodeResponse({})
|
||||
).to.throw(/Invalid jsonrpc/);
|
||||
});
|
||||
|
||||
it('expects a valid id field', () => {
|
||||
expect(
|
||||
() => coder.decodeResponse({ jsonrpc: '2.0' })
|
||||
).to.throw(/Invalid id/);
|
||||
});
|
||||
|
||||
it('expects a valid result field', () => {
|
||||
expect(
|
||||
() => coder.decodeResponse({ id: 1, jsonrpc: '2.0' })
|
||||
).to.throw(/No result/);
|
||||
});
|
||||
|
||||
it('throws any error found', () => {
|
||||
expect(
|
||||
() => coder.decodeResponse({ id: 1, jsonrpc: '2.0', error: { code: 123, message: 'test error' } })
|
||||
).to.throw(/\[123\]: test error/);
|
||||
});
|
||||
|
||||
it('returns the result', () => {
|
||||
expect(
|
||||
coder.decodeResponse({ id: 1, jsonrpc: '2.0', result: 'some result' })
|
||||
).to.equal('some result');
|
||||
});
|
||||
});
|
||||
|
||||
describe('encodeObject', () => {
|
||||
it('encodes a valid JsonRPC object', () => {
|
||||
expect(
|
||||
coder.encodeObject('method', 'params')
|
||||
).to.deep.equal({
|
||||
id: 1,
|
||||
jsonrpc: '2.0',
|
||||
method: 'method',
|
||||
params: 'params'
|
||||
});
|
||||
expect(coder.id).to.equal(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('encodeJson', () => {
|
||||
it('encodes a valid JsonRPC JSON string', () => {
|
||||
expect(
|
||||
coder.encodeJson('method', 'params')
|
||||
).to.equal('{"id":1,"jsonrpc":"2.0","method":"method","params":"params"}');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,26 +0,0 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
export type JsonRpcObject = {
|
||||
id: number;
|
||||
jsonrpc: '2.0';
|
||||
};
|
||||
|
||||
export type JsonRpcRequest = JsonRpcObject & {
|
||||
method: string;
|
||||
params: Array<any>;
|
||||
};
|
||||
|
||||
export type JsonRpcResponseBase = {
|
||||
error?: {
|
||||
code: number,
|
||||
message: string
|
||||
};
|
||||
result?: any;
|
||||
}
|
||||
|
||||
export type JsonRpcResponse = JsonRpcObject & JsonRpcResponseBase;
|
||||
|
||||
export interface ProviderInterface {
|
||||
send (method: string, params: Array<any>): Promise<any>;
|
||||
}
|
||||
@@ -1,101 +0,0 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
import type { JsonRpcResponse, ProviderInterface } from './types';
|
||||
|
||||
type AwaitingType = {
|
||||
callback: (error: ?Error, result: any) => void
|
||||
};
|
||||
|
||||
type WsMessageType = {
|
||||
data: any
|
||||
};
|
||||
|
||||
const JsonRpcCoder = require('./jsonRpcCoder');
|
||||
|
||||
module.exports = class WsProvider extends JsonRpcCoder implements ProviderInterface {
|
||||
_autoConnect: boolean = true;
|
||||
_endpoint: string;
|
||||
_handlers: { [number]: AwaitingType } = {};
|
||||
_websocket: WebSocket;
|
||||
|
||||
constructor (endpoint: string, autoConnect: boolean = true) {
|
||||
super();
|
||||
|
||||
this._endpoint = endpoint;
|
||||
this._autoConnect = autoConnect;
|
||||
|
||||
if (autoConnect) {
|
||||
this.connect();
|
||||
}
|
||||
}
|
||||
|
||||
connect = () => {
|
||||
this._handlers = {};
|
||||
|
||||
this._websocket = new WebSocket(this._endpoint);
|
||||
|
||||
this._websocket.onclose = this._onClose;
|
||||
this._websocket.onerror = this._onError;
|
||||
this._websocket.onmessage = this._onMessage;
|
||||
this._websocket.onopen = this._onOpen;
|
||||
}
|
||||
|
||||
_onClose = () => {
|
||||
// console.log('disconnected from', this._endpoint);
|
||||
|
||||
if (this._autoConnect) {
|
||||
setTimeout(this.connect, 1000);
|
||||
}
|
||||
}
|
||||
|
||||
_onError = (error: Event) => {
|
||||
console.error(error);
|
||||
}
|
||||
|
||||
_onOpen = () => {
|
||||
// console.log('connected to', this._endpoint);
|
||||
}
|
||||
|
||||
_onMessage = (message: WsMessageType) => {
|
||||
const response: JsonRpcResponse = JSON.parse(message.data);
|
||||
const handler = this._handlers[response.id];
|
||||
|
||||
if (!handler) {
|
||||
console.error(`Unable to find handler for id=${response.id}`);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const result = this.decodeResponse(response);
|
||||
|
||||
handler.callback(null, result);
|
||||
} catch (error) {
|
||||
handler.callback(error);
|
||||
}
|
||||
|
||||
delete this._handlers[response.id];
|
||||
}
|
||||
|
||||
send (method: string, params: Array<any>): Promise<any> {
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
this._websocket.send(
|
||||
this.encodeObject(method, params)
|
||||
);
|
||||
|
||||
this._handlers[this.id] = {
|
||||
callback: (error: ?Error, result: any) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
} else {
|
||||
resolve(result);
|
||||
}
|
||||
}
|
||||
};
|
||||
} catch (error) {
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -1,194 +0,0 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
/* eslint-disable no-unused-expressions */
|
||||
|
||||
const { mockWs, TEST_WS_URL } = require('../../test/mockWs');
|
||||
|
||||
const { isUndefined } = require('@polkadot/util/is');
|
||||
const sinon = require('sinon');
|
||||
|
||||
const Ws = require('./ws');
|
||||
|
||||
let ws;
|
||||
let mock;
|
||||
|
||||
function createWs (requests, autoConnect) {
|
||||
mock = autoConnect || isUndefined(autoConnect)
|
||||
? mockWs(requests)
|
||||
: null;
|
||||
|
||||
ws = new Ws(TEST_WS_URL, autoConnect);
|
||||
|
||||
sinon.spy(ws, 'encodeObject');
|
||||
sinon.spy(ws, 'decodeResponse');
|
||||
|
||||
return ws;
|
||||
}
|
||||
|
||||
describe('provider/Ws', () => {
|
||||
afterEach(() => {
|
||||
ws.encodeObject.restore();
|
||||
ws.decodeResponse.restore();
|
||||
|
||||
if (mock) {
|
||||
mock.done();
|
||||
}
|
||||
});
|
||||
|
||||
describe('websocket', () => {
|
||||
beforeEach(() => {
|
||||
ws = createWs([]);
|
||||
|
||||
sinon.spy(console, 'error');
|
||||
sinon.spy(console, 'log');
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
console.error.restore();
|
||||
console.log.restore();
|
||||
});
|
||||
|
||||
it('sets up the on* handlers', () => {
|
||||
expect(ws._websocket.onclose[0]).to.equal(ws._onClose);
|
||||
expect(ws._websocket.onerror[0]).to.equal(ws._onError);
|
||||
expect(ws._websocket.onmessage[0]).to.equal(ws._onMessage);
|
||||
expect(ws._websocket.onopen[0]).to.equal(ws._onOpen);
|
||||
});
|
||||
|
||||
describe('_onClose', () => {
|
||||
it('reconnects after delay', () => {
|
||||
const clock = sinon.useFakeTimers();
|
||||
|
||||
ws.connect = sinon.stub();
|
||||
ws._onClose();
|
||||
|
||||
expect(ws.connect).not.to.have.been.called;
|
||||
|
||||
clock.tick(1001);
|
||||
|
||||
expect(ws.connect).to.have.been.called;
|
||||
|
||||
clock.restore();
|
||||
});
|
||||
|
||||
it('does not reconnect when autoConnect false', () => {
|
||||
mock.done();
|
||||
ws = createWs([], false);
|
||||
|
||||
const clock = sinon.useFakeTimers();
|
||||
|
||||
ws.connect = sinon.stub();
|
||||
ws._onClose();
|
||||
|
||||
expect(ws.connect).not.to.have.been.called;
|
||||
|
||||
clock.tick(1001);
|
||||
|
||||
expect(ws.connect).not.to.have.been.called;
|
||||
|
||||
clock.restore();
|
||||
});
|
||||
});
|
||||
|
||||
describe('_onError', () => {
|
||||
it('logs the error', () => {
|
||||
ws._onError('test error');
|
||||
|
||||
expect(console.error).to.have.been.calledWith('test error');
|
||||
});
|
||||
});
|
||||
|
||||
describe('_onMessage', () => {
|
||||
it('fails with log when handler not found', () => {
|
||||
ws._onMessage({ data: '{"id":2}' });
|
||||
|
||||
expect(console.error).to.have.been.calledWith('Unable to find handler for id=2');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('send', () => {
|
||||
it('handles internal errors', () => {
|
||||
ws = createWs([], false);
|
||||
|
||||
return ws
|
||||
.send('test_encoding', ['param'])
|
||||
.catch((error) => {
|
||||
expect(error).to.be.ok;
|
||||
});
|
||||
});
|
||||
|
||||
it('encodes requests', () => {
|
||||
ws = createWs([{
|
||||
id: 1,
|
||||
method: 'test_encoding',
|
||||
reply: {
|
||||
result: 'ok'
|
||||
}
|
||||
}]);
|
||||
|
||||
return ws
|
||||
.send('test_encoding', ['param'])
|
||||
.then((result) => {
|
||||
expect(ws.encodeObject).to.have.been.calledWith('test_encoding', ['param']);
|
||||
});
|
||||
});
|
||||
|
||||
it('decodes responses', () => {
|
||||
ws = createWs([{
|
||||
id: 1,
|
||||
method: 'test_encoding',
|
||||
reply: {
|
||||
result: 'ok'
|
||||
}
|
||||
}]);
|
||||
|
||||
return ws
|
||||
.send('test_encoding', ['param'])
|
||||
.then((result) => {
|
||||
expect(ws.decodeResponse).to.have.been.calledWith({
|
||||
id: 1,
|
||||
jsonrpc: '2.0',
|
||||
result: 'ok'
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('passes the body through correctly', () => {
|
||||
ws = createWs([{
|
||||
id: 1,
|
||||
method: 'test_body',
|
||||
reply: {
|
||||
result: 'ok'
|
||||
}
|
||||
}]);
|
||||
|
||||
return ws
|
||||
.send('test_body', ['param'])
|
||||
.then((result) => {
|
||||
expect(mock.body['test_body']).to.deep.equal({
|
||||
id: 1,
|
||||
jsonrpc: '2.0',
|
||||
method: 'test_body',
|
||||
params: ['param']
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('throws error when !response.ok', () => {
|
||||
ws = createWs([{
|
||||
id: 1,
|
||||
error: {
|
||||
code: 666,
|
||||
message: 'error'
|
||||
}
|
||||
}]);
|
||||
|
||||
return ws
|
||||
.send('test_error', [])
|
||||
.catch((error) => {
|
||||
expect(error).to.match(/\[666\]: error/);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,7 +1,5 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
require('../src/polyfill');
|
||||
|
||||
const chai = require('chai');
|
||||
const sinonChai = require('sinon-chai');
|
||||
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const nock = require('nock');
|
||||
|
||||
const TEST_HTTP_URL = 'http://localhost:9944';
|
||||
|
||||
function mockHttp (requests) {
|
||||
nock.cleanAll();
|
||||
|
||||
return requests.reduce((scope, request, index) => {
|
||||
return scope
|
||||
.post('/')
|
||||
.reply(request.code || 200, (uri, body) => {
|
||||
if (body.method !== request.method) {
|
||||
return {
|
||||
error: `Invalid method ${body.method}, expected ${request.method}`
|
||||
};
|
||||
}
|
||||
|
||||
scope.body = scope.body || {};
|
||||
scope.body[request.method] = body;
|
||||
|
||||
return Object.assign({ id: body.id, jsonrpc: '2.0' }, request.reply || {});
|
||||
});
|
||||
}, nock(TEST_HTTP_URL));
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
TEST_HTTP_URL,
|
||||
mockHttp
|
||||
};
|
||||
@@ -1,55 +0,0 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const { Server } = require('mock-socket');
|
||||
|
||||
const TEST_WS_URL = 'ws://localhost:9955';
|
||||
|
||||
let server;
|
||||
|
||||
function mockWs (requests) {
|
||||
server = new Server(TEST_WS_URL);
|
||||
let requestCount = 0;
|
||||
|
||||
server.on('message', (body) => {
|
||||
try {
|
||||
const request = requests[requestCount];
|
||||
const result = request.reply;
|
||||
const response = request.error
|
||||
? {
|
||||
id: request.id,
|
||||
jsonrpc: '2.0',
|
||||
error: {
|
||||
code: request.error.code,
|
||||
message: request.error.message
|
||||
}
|
||||
}
|
||||
: {
|
||||
id: request.id,
|
||||
jsonrpc: '2.0',
|
||||
result: result.result
|
||||
};
|
||||
|
||||
scope.body[request.method] = body;
|
||||
requestCount++;
|
||||
|
||||
server.send(JSON.stringify(response));
|
||||
} catch (error) {
|
||||
console.error('mock:onmessage', body, error);
|
||||
}
|
||||
});
|
||||
|
||||
const scope = {
|
||||
body: {},
|
||||
requests: 0,
|
||||
server,
|
||||
done: () => server.stop(),
|
||||
isDone: () => requestCount === requests.length
|
||||
};
|
||||
|
||||
return scope;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
TEST_WS_URL,
|
||||
mockWs
|
||||
};
|
||||
Reference in New Issue
Block a user