nodejsSet added to FakeXHR2 and httpprovider test cases extended

This commit is contained in:
nivida
2020-01-07 10:28:36 +01:00
parent b038c6864b
commit af0c67443b
2 changed files with 55 additions and 5 deletions
+5 -2
View File
@@ -7,11 +7,16 @@ var FakeXHR2 = function () {
this.readyState = 4;
this.onreadystatechange = null;
this.async = true;
this.agents = {};
this.headers = {
'Content-Type': 'text/plain'
};
};
FakeXHR2.prototype.nodejsSet = function (agents) {
this.agents = agents;
};
FakeXHR2.prototype.open = function (method, host, async) {
assert.equal(method, 'POST');
assert.notEqual(host, null);
@@ -33,6 +38,4 @@ FakeXHR2.prototype.send = function (payload) {
}
};
FakeXHR2.prototype.nodejsSet = Function.prototype;
module.exports = {XMLHttpRequest: FakeXHR2};
+50 -3
View File
@@ -1,5 +1,7 @@
var chai = require('chai');
var assert = chai.assert;
var http = require('http');
var https = require('https');
var SandboxedModule = require('sandboxed-module');
SandboxedModule.registerBuiltInSourceTransformer('istanbul');
@@ -11,15 +13,60 @@ var HttpProvider = SandboxedModule.require('../packages/web3-providers-http', {
singleOnly: true
});
describe('web3-providers-http', function () {
describe.only('web3-providers-http', function () {
describe('prepareRequest', function () {
let provider;
let result;
let options;
let agent;
it('should set request header', function () {
var provider = new HttpProvider('http://localhost:8545', {headers: [{name: 'Access-Control-Allow-Origin', value: '*'}]});
var result = provider._prepareRequest();
provider = new HttpProvider('http://localhost:8545', {headers: [{name: 'Access-Control-Allow-Origin', value: '*'}]});
result = provider._prepareRequest();
assert.equal(typeof result, 'object');
assert.equal(result.headers['Access-Control-Allow-Origin'], '*');
});
it('should use the passed custom http agent', function () {
agent = new http.Agent();
options = {agent: {http: agent}};
provider = new HttpProvider('http://localhost:8545', options);
result = provider._prepareRequest();
assert.equal(typeof result, 'object');
assert.equal(result.agents.httpAgent, 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 () {
agent = new https.Agent();
options = {agent: {https: agent}};
provider = new HttpProvider('http://localhost:8545', options);
result = provider._prepareRequest();
assert.equal(typeof result, 'object');
assert.equal(result.agents.httpsAgent, agent);
assert.equal(provider.httpAgent, undefined);
assert.equal(provider.httpsAgent, undefined);
assert.equal(provider.agent, options.agent);
});
it('should use the passed baseUrl', function () {
agent = new https.Agent();
options = {agent: {https: agent, baseUrl: 'base'}};
provider = new HttpProvider('http://localhost:8545', options);
result = provider._prepareRequest();
assert.equal(typeof result, 'object');
assert.equal(result.agents.httpsAgent, agent);
assert.equal(result.agents.baseUrl, 'base');
assert.equal(provider.httpAgent, undefined);
assert.equal(provider.httpsAgent, undefined);
assert.equal(provider.agent, options.agent);
});
});
describe('send', function () {