-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
httpprovider.js
151 lines (127 loc) · 5.42 KB
/
httpprovider.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
var chai = require('chai');
var chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);
var assert = chai.assert;
var expect = chai.expect;
var http = require('http');
var https = require('https');
var Web3 = require('../packages/web3');
var HttpProvider = require('../packages/web3-providers-http');
var fetchMock = require('fetch-mock');
function isObject(object) {
return object != null && typeof object === 'object';
}
function deepEqual(object1, object2) {
const keys1 = Object.keys(object1);
const keys2 = Object.keys(object2);
if (keys1.length !== keys2.length) {
return false;
}
for (const key of keys1) {
const val1 = object1[key];
const val2 = object2[key];
const areObjects = isObject(val1) && isObject(val2);
if (areObjects && !deepEqual(val1, val2) || !areObjects && val1 !== val2) {
return false;
}
}
return true;
}
describe('web3-providers-http', function () {
describe('prepareRequest', function () {
it('should set request header', function () {
var options = {headers: [{name: 'Access-Control-Allow-Origin', value: '*'}]}
var provider = new HttpProvider('http://localhost:8545', options);
assert.equal(provider.headers, options.headers);
assert.equal(provider.httpAgent instanceof http.Agent, true);
});
it('should have https agent', function () {
var provider = new HttpProvider('https://localhost');
assert.equal(provider.httpsAgent instanceof https.Agent, true);
});
it('should use the passed custom http agent', function () {
var agent = new http.Agent();
var options = {agent: {http: agent}};
var provider = new HttpProvider('http://localhost:8545', options);
assert.equal(provider.agent.http, agent);
assert.equal(provider.httpAgent, undefined);
assert.equal(provider.httpsAgent, undefined);
assert.equal(provider.agent, options.agent);
});
it('should use the passed custom https agent', function () {
var agent = new https.Agent();
var options = {agent: {https: agent}};
var provider = new HttpProvider('http://localhost:8545', options);
assert.equal(provider.agent.https, agent);
assert.equal(provider.httpAgent, undefined);
assert.equal(provider.httpsAgent, undefined);
assert.equal(provider.agent, options.agent);
});
});
describe('send', function () {
it('should fail with invalid remote node connection', async function () {
var provider = new HttpProvider('http://localhost:8545');
var web3 = new Web3(provider);
await expect(web3.eth.getChainId()).to.be.rejectedWith(Error, "CONNECTION ERROR: Couldn't connect to node http://localhost:8545.");
});
it('should fail for non-json format response', async function () {
var provider = new HttpProvider('/fetchMock');
var web3 = new Web3(provider);
fetchMock.mock('/fetchMock', 'Testing non-json format response');
await expect(web3.eth.getChainId()).to.be.rejectedWith(Error, /Invalid JSON RPC response/);
fetchMock.restore();
});
it('should timeout by delayed response', async function () {
var provider = new HttpProvider('/fetchMock', { timeout: 500 });
var web3 = new Web3(provider);
fetchMock.mock('/fetchMock', 'Testing non-json format response', { delay: 1000 });
await expect(web3.eth.getChainId()).to.be.rejectedWith(Error, 'CONNECTION TIMEOUT: timeout of 500 ms achived');
fetchMock.restore();
});
it('should send basic async request', async function () {
var provider = new HttpProvider('/fetchMock');
var reqObject = {
'jsonrpc': '2.0',
'id': 0,
'method': 'eth_chainId',
'params': []
};
var resObject = {
'jsonrpc': '2.0',
'id': 0,
'result': '0x1'
};
fetchMock.mock((url, opts) => {
const reqCount = JSON.parse(opts.body).id;
reqObject = JSON.stringify((() => {
const obj = reqObject;
obj.id = reqCount;
return obj;
})());
resObject = (() => {
const obj = resObject;
obj.id = reqCount;
return obj;
})();
const matcher = {
url: '/fetchMock',
method: 'POST',
credentials: 'omit',
headers: {
'Content-Type': 'application/json'
},
body: reqObject
};
return url === matcher.url
&& opts.method === matcher.method
&& opts.credentials === matcher.credentials
&& deepEqual(opts.headers, matcher.headers)
&& opts.body === matcher.body;
}, resObject);
var web3 = new Web3(provider);
var chainId = await web3.eth.getChainId();
assert.equal(chainId, 1);
fetchMock.restore();
});
});
});