From c6f3517ea4dfc0a6216152f275c7288edffca825 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Thu, 2 Jun 2016 10:58:40 +0200 Subject: [PATCH 1/5] setDebug for transport --- lib/transport/jsonRpc.js | 17 +++++++++ lib/transport/jsonRpc.spec.js | 68 +++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) diff --git a/lib/transport/jsonRpc.js b/lib/transport/jsonRpc.js index 630cf35..f0615aa 100644 --- a/lib/transport/jsonRpc.js +++ b/lib/transport/jsonRpc.js @@ -4,6 +4,7 @@ export default class JsonRpc { this._host = host; this._port = port; this._id = 1; + this._debug = false; } _encodeBody (method, params) { @@ -37,6 +38,10 @@ export default class JsonRpc { return fetch(this._getEndpoint(), this._encodeOptions(method, params)) .then((response) => { if (response.status !== 200) { + if (this._debug) { + console.error(' ', `${method}(${params}) throws`, response.status, response.statusText); + } + throw new Error(response.statusText); } @@ -44,10 +49,22 @@ export default class JsonRpc { }) .then((result) => { if (result.error) { + if (this._debug) { + console.error(' ', `${method}(${params}) =`, result); + } + throw new Error(result.error); } + if (this._debug) { + console.log(' ', `${method}(${params}) =`, result.result); + } + return result.result; }); } + + setDebug (flag) { + this._debug = flag; + } } diff --git a/lib/transport/jsonRpc.spec.js b/lib/transport/jsonRpc.spec.js index 7682434..325e045 100644 --- a/lib/transport/jsonRpc.spec.js +++ b/lib/transport/jsonRpc.spec.js @@ -1,3 +1,5 @@ +import sinon from 'sinon'; + import { TEST_HOST, TEST_PORT, mockRpc } from '../../test/mockRpc'; import JsonRpc from './jsonRpc'; @@ -114,4 +116,70 @@ describe('transport/JsonRPC', () => { expect(error.message).to.equal(ERROR); }); }); + + describe('setDebug', () => { + it('starts with disabled flag', () => { + expect(transport._debug).to.be.false; + }); + + it('true flag switches on', () => { + transport.setDebug(true); + expect(transport._debug).to.be.true; + }); + + it('false flag switches off', () => { + transport.setDebug(true); + expect(transport._debug).to.be.true; + transport.setDebug(false); + expect(transport._debug).to.be.false; + }); + + describe('logging', () => { + beforeEach(() => { + sinon.spy(console, 'log'); + sinon.spy(console, 'error'); + }); + + afterEach(() => { + console.log.restore(); + console.error.restore(); + }); + + it('logs the HTTP error', () => { + const scope = mockRpc([{ method: 'eth_call', reply: {}, code: 500 }]); + + transport.setDebug(true); + return transport + .execute('eth_call') + .catch(() => { + expect(scope.isDone()).to.be.true; + expect(console.error).to.be.called; + }); + }); + + it('logs the RPC error', () => { + const scope = mockRpc([{ method: 'eth_call', reply: { error: 'test error' } }]); + + transport.setDebug(true); + return transport + .execute('eth_call') + .catch(() => { + expect(scope.isDone()).to.be.true; + expect(console.error).to.be.called; + }); + }); + + it('logs the RPC result', () => { + const scope = mockRpc([{ method: 'eth_call', reply: { result: '0x123456789' } }]); + + transport.setDebug(true); + return transport + .execute('eth_call') + .then(() => { + expect(scope.isDone()).to.be.true; + expect(console.log).to.be.called; + }); + }); + }); + }); }); From d9d019d79b729ba5594145b610e019439993927b Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Thu, 2 Jun 2016 10:58:56 +0200 Subject: [PATCH 2/5] add testE2E npm run --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index f07a9ee..8ca6a30 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,8 @@ "lint": "eslint --ignore-path .gitignore ./lib/", "release": "./scripts/release.sh", "testCoverage": "istanbul cover _mocha -- 'lib/**/*.spec.js'", - "testOnce": "mocha 'lib/**/*.spec.js'" + "testOnce": "mocha 'lib/**/*.spec.js'", + "testE2E": "mocha 'test/e2e/**/*.spec.js'" }, "devDependencies": { "babel-cli": "^6.9.0", From 86766822f6146e2a1da346786b6bb1f3d8905045 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Thu, 2 Jun 2016 10:59:10 +0200 Subject: [PATCH 3/5] expand description --- README.md | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a5333ac..e30a4b5 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,22 @@ # ethapi-js -A thin, fast low-level Promise-based wrapper around the Eth APIs. +A thin, fast, low-level Promise-based wrapper around the Ethereum APIs. [![Build Status](https://travis-ci.org/jacogr/ethapi-js.svg?branch=master)](https://travis-ci.org/jacogr/ethapi-js) [![Coverage Status](https://coveralls.io/repos/github/jacogr/ethapi-js/badge.svg?branch=master)](https://coveralls.io/github/jacogr/ethapi-js?branch=master) [![Dependency Status](https://david-dm.org/jacogr/ethapi-js.svg)](https://david-dm.org/jacogr/ethapi-js) [![devDependency Status](https://david-dm.org/jacogr/ethapi-js/dev-status.svg)](https://david-dm.org/jacogr/ethapi-js#info=devDependencies) -## getting going +## contributing -- clone -- `npm install` -- `npm run testOnce` +Clone the repo and install dependencies via `npm install`. Tests can be executed via + +- `npm run testOnce` (100% covered unit tests) +- `npm run testE2E` (E2E against a running RPC-enabled testnet Parity/Geth instance, `parity --testnet --jsonrpc`) + +## installation + +Install the package with `npm install --save ethapi-js` from the [npm registry ethapi-js](https://www.npmjs.com/package/ethapi-js) ## usage From d1cf47b4e724bc7b9edac12cf91ed28a1e374c07 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Thu, 2 Jun 2016 10:59:27 +0200 Subject: [PATCH 4/5] ethapi instantaition for e2e tests --- test/ethapi.js | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 test/ethapi.js diff --git a/test/ethapi.js b/test/ethapi.js new file mode 100644 index 0000000..897ac47 --- /dev/null +++ b/test/ethapi.js @@ -0,0 +1,8 @@ +import EthApi from '../lib'; + +const transport = new EthApi.Transports.JsonRpc('127.0.0.1', 8545); +const ethapi = new EthApi(transport); + +transport.setDebug(true); + +export default ethapi; From 73e73ecb42ab33329d78bbd15892eddb990ff192 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Thu, 2 Jun 2016 10:59:47 +0200 Subject: [PATCH 5/5] basis e2e tests for accounts & blockNumber --- test/e2e/rpc/eth.spec.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 test/e2e/rpc/eth.spec.js diff --git a/test/e2e/rpc/eth.spec.js b/test/e2e/rpc/eth.spec.js new file mode 100644 index 0000000..b309567 --- /dev/null +++ b/test/e2e/rpc/eth.spec.js @@ -0,0 +1,21 @@ +import ethapi from '../../ethapi'; + +describe('ethapi.eth.accounts()', () => { + it('returns the available accounts', () => { + return ethapi.eth + .accounts() + .then((accounts) => { + expect(accounts).to.be.ok; + }); + }); +}); + +describe('ethapi.eth.blockNumber()', () => { + it('returns the current blockNumber', () => { + return ethapi.eth + .blockNumber() + .then((blockNumber) => { + expect(blockNumber).to.be.ok; + }); + }); +});