forked from ruimarinho/bitcoin-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_test.js
87 lines (71 loc) · 2.67 KB
/
parser_test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
* Module dependencies.
*/
import { defaults } from 'lodash';
import Client from '../src/index';
import RpcError from '../src/errors/rpc-error';
import config from './config';
import nock from 'nock';
import should from 'should';
/**
* Test `Parser`.
*/
afterEach(() => {
if (nock.pendingMocks().length) {
throw new Error('Unexpected pending mocks');
}
nock.cleanAll();
});
describe('Parser', () => {
it('should throw an error with a generic message if one is not returned on the response', async () => {
nock(`http://${config.bitcoin.host}:${config.bitcoin.port}/`)
.post('/')
.reply(200, '{ "result": null, "error": { "code": -32601 }, "id": "69837016239933"}');
try {
await new Client(config.bitcoin).command('foobar');
should.fail();
} catch (e) {
e.should.be.an.instanceOf(RpcError);
e.message.should.equal('An error occurred while processing the RPC call to bitcoind');
e.code.should.equal(-32601);
}
});
it('should throw an error if the response does not include a `result`', async () => {
nock(`http://${config.bitcoin.host}:${config.bitcoin.port}/`)
.post('/')
.reply(200, '{ "error": null, "id": "69837016239933"}');
try {
await new Client(config.bitcoin).command('foobar2');
should.fail();
} catch (e) {
e.should.be.an.instanceOf(RpcError);
e.message.should.equal('Missing `result` on the RPC call result');
e.code.should.equal(-32700);
}
});
it('should throw an error if the response is not successful but is json-formatted', async () => {
try {
await new Client(defaults({ wallet: 'foobar' }, config.bitcoinMultiWallet)).getWalletInfo();
} catch (e) {
e.should.be.an.instanceOf(RpcError);
e.message.should.equal('Requested wallet does not exist or is not loaded');
e.code.should.equal(-18);
}
});
describe('headers', () => {
it('should return the response headers if `headers` is enabled', async () => {
const [info, headers] = await new Client(defaults({ headers: true }, config.bitcoin)).getNetworkInfo();
info.should.be.an.Object();
headers.should.have.keys('date', 'connection', 'content-length', 'content-type');
});
it('should return the response headers if `headers` is enabled and batching is used', async () => {
const batch = [
{ method: 'getbalance' },
{ method: 'getbalance' }
];
const [addresses, headers] = await new Client(defaults({ headers: true }, config.bitcoin)).command(batch);
addresses.should.have.length(batch.length);
headers.should.have.keys('date', 'connection', 'content-length', 'content-type');
});
});
});