Skip to content

Commit

Permalink
Merge pull request #2564 from ethereum/issue/2559
Browse files Browse the repository at this point in the history
XMLHttpRequest options handling improved
  • Loading branch information
nivida authored Mar 25, 2019
2 parents 22a190c + 9116b45 commit 432df2a
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 17 deletions.
2 changes: 1 addition & 1 deletion packages/web3-providers/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 12 additions & 4 deletions packages/web3-providers/src/factories/ProvidersModuleFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,25 @@ export default class ProvidersModuleFactory {
* @param {Number} timeout
* @param {Array} headers
* @param {Object} agent
* @param {Boolean} withCredentials
*
* @returns {XMLHttpRequest}
*/
createXMLHttpRequest(host, timeout = 0, headers, agent) {
const request = new XHR();
request.nodejsSet(agent);
createXMLHttpRequest(host, timeout = 0, headers, agent, withCredentials) {
let request;

// runtime is of type node
if (typeof process !== 'undefined' && process.versions != null && process.versions.node != null) {
request = new XHR();
request.nodejsSet(agent);
} else {
request = new XMLHttpRequest();
}

request.open('POST', host, true);
request.setRequestHeader('Content-Type', 'application/json');
request.timeout = timeout;
request.withCredentials = true;
request.withCredentials = withCredentials;

if (headers) {
headers.forEach((header) => {
Expand Down
4 changes: 3 additions & 1 deletion packages/web3-providers/src/providers/HttpProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export default class HttpProvider {
this.host = host;
this.timeout = options.timeout || 0;
this.headers = options.headers;
this.withCredentials = options.withCredentials || false;
this.connected = true;
this.providersModuleFactory = providersModuleFactory;
this.agent = {};
Expand Down Expand Up @@ -135,7 +136,8 @@ export default class HttpProvider {
this.host,
this.timeout,
this.headers,
this.agent
this.agent,
this.withCredentials
);

request.onreadystatechange = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('ProvidersModuleFactoryTest', () => {
});

it('createXMLHttpRequest returns instance of XMLHttpRequest', () => {
expect(providersModuleFactory.createXMLHttpRequest('', 0, [{name: 'name', value: 'value'}], {})).toBeInstanceOf(
expect(providersModuleFactory.createXMLHttpRequest('', 0, [{name: 'name', value: 'value'}], {}, true)).toBeInstanceOf(
XHR
);

Expand Down
53 changes: 44 additions & 9 deletions packages/web3-providers/tests/src/providers/HttpProviderTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('HttpProviderTest', () => {

httpProvider = new HttpProvider(
'https',
{headers: [], timeout: 1, keepAlive: true},
{headers: [], timeout: 1, keepAlive: true, withCredentials: true},
providersModuleFactoryMock
);
});
Expand All @@ -47,7 +47,33 @@ describe('HttpProviderTest', () => {
});

it('constructor check with http', () => {
httpProvider = new HttpProvider('http', {headers: [], timeout: 1}, providersModuleFactoryMock);
httpProvider = new HttpProvider(
'http',
{headers: [], timeout: 1, keepAlive: true, withCredentials: true},
providersModuleFactoryMock
);

expect(httpProvider.host).toEqual('http');

expect(httpProvider.headers).toEqual([]);

expect(httpProvider.timeout).toEqual(1);

expect(httpProvider.connected).toEqual(true);

expect(httpProvider.withCredentials).toEqual(true);

expect(httpProvider.providersModuleFactory).toEqual(providersModuleFactoryMock);

expect(httpProvider.agent.httpAgent).toBeInstanceOf(http.Agent);
});

it('constructor check without the property withCredentials in the options', () => {
httpProvider = new HttpProvider(
'http',
{headers: [], timeout: 1},
providersModuleFactoryMock
);

expect(httpProvider.host).toEqual('http');

Expand All @@ -57,6 +83,8 @@ describe('HttpProviderTest', () => {

expect(httpProvider.connected).toEqual(true);

expect(httpProvider.withCredentials).toEqual(false);

expect(httpProvider.providersModuleFactory).toEqual(providersModuleFactoryMock);

expect(httpProvider.agent.httpAgent).toBeInstanceOf(http.Agent);
Expand Down Expand Up @@ -110,7 +138,8 @@ describe('HttpProviderTest', () => {
httpProvider.host,
httpProvider.timeout,
httpProvider.headers,
httpProvider.agent
httpProvider.agent,
httpProvider.withCredentials,
);

expect(xhrMock.send).toHaveBeenCalledWith('{"id":"0x0"}');
Expand Down Expand Up @@ -148,7 +177,8 @@ describe('HttpProviderTest', () => {
httpProvider.host,
httpProvider.timeout,
httpProvider.headers,
httpProvider.agent
httpProvider.agent,
httpProvider.withCredentials
);

expect(xhrMock.send).toHaveBeenCalledWith('{"id":"0x0"}');
Expand Down Expand Up @@ -189,7 +219,8 @@ describe('HttpProviderTest', () => {
httpProvider.host,
httpProvider.timeout,
httpProvider.headers,
httpProvider.agent
httpProvider.agent,
httpProvider.withCredentials
);

expect(xhrMock.send).toHaveBeenCalledWith('[{"id":"0x0"}]');
Expand Down Expand Up @@ -219,7 +250,8 @@ describe('HttpProviderTest', () => {
httpProvider.host,
httpProvider.timeout,
httpProvider.headers,
httpProvider.agent
httpProvider.agent,
httpProvider.withCredentials
);

expect(xhrMock.send).toHaveBeenCalledWith('{"id":"0x0"}');
Expand Down Expand Up @@ -247,7 +279,8 @@ describe('HttpProviderTest', () => {
httpProvider.host,
httpProvider.timeout,
httpProvider.headers,
httpProvider.agent
httpProvider.agent,
httpProvider.withCredentials
);

expect(xhrMock.send).toHaveBeenCalledWith('{"id":"0x0"}');
Expand All @@ -273,7 +306,8 @@ describe('HttpProviderTest', () => {
httpProvider.host,
httpProvider.timeout,
httpProvider.headers,
httpProvider.agent
httpProvider.agent,
httpProvider.withCredentials
);

expect(xhrMock.send).toHaveBeenCalledWith('{"id":"0x0"}');
Expand All @@ -297,7 +331,8 @@ describe('HttpProviderTest', () => {
httpProvider.host,
httpProvider.timeout,
httpProvider.headers,
httpProvider.agent
httpProvider.agent,
httpProvider.withCredentials
);

expect(xhrMock.send).toHaveBeenCalledWith('{"id":"0x0"}');
Expand Down
1 change: 1 addition & 0 deletions packages/web3-providers/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ export interface HttpProviderOptions {
host?: string;
timeout?: number;
headers?: {};
withCredentials?: boolean;
}

export interface WebsocketProviderOptions {
Expand Down
3 changes: 2 additions & 1 deletion packages/web3-providers/types/tests/http-provider-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ const httpProvider = new HttpProvider('http://localhost:8545', {
name: 'Access-Control-Allow-Origin',
value: '*'
}
]
],
withCredentials: false
});

// $ExpectType Promise<any>
Expand Down

0 comments on commit 432df2a

Please sign in to comment.