Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/generated/changelog.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ <h1>Agent-JS Changelog</h1>
<section>
<h2>Version x.x.x</h2>
<ul>
<li></li>
<li>fix: default host logic fixed and tests added</li>
</ul>
<h2>Version 0.19.0</h2>
<ul>
Expand Down
34 changes: 32 additions & 2 deletions packages/agent/src/agent/http/http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { Principal } from '@dfinity/principal';
import { requestIdOf } from '../../request_id';

import { JSDOM } from 'jsdom';
import { AnonymousIdentity, SignIdentity, SubmitResponse, UpdateCallRejectedError } from '../..';
import { AnonymousIdentity, SignIdentity } from '../..';
import { Ed25519KeyIdentity } from '../../../../identity/src/identity/ed25519';
import { toHexString } from '../../../../identity/src/buffer';
import { AgentError } from '../../errors';
Expand All @@ -39,7 +39,12 @@ const originalWindow = global.window;
const originalFetch = global.fetch;
beforeEach(() => {
global.Date.now = jest.fn(() => new Date(NANOSECONDS_PER_MILLISECONDS).getTime());
global.window = originalWindow;
Object.assign(global, 'window', {
value: {
originalWindow,
},
writable: true,
});
global.fetch = originalFetch;
});

Expand Down Expand Up @@ -754,3 +759,28 @@ test('should fetch with given call options and fetch options', async () => {
expect(calls[0][1].reactNative).toStrictEqual({ textStreaming: true });
expect(calls[1][1].reactNative.__nativeResponseType).toBe('base64');
});

describe('default host', () => {
it('should use a default host of icp-api.io', () => {
const agent = new HttpAgent({ fetch: jest.fn() });
window.location.hostname; //?
expect((agent as any)._host.hostname).toBe('icp-api.io');
});
it('should use a default of icp-api.io if location is not available', () => {
delete (global as any).window;
const agent = new HttpAgent({ fetch: jest.fn() });
expect((agent as any)._host.hostname).toBe('icp-api.io');
});
it('should use the existing host if the agent is used on a known hostname', () => {
const knownHosts = ['ic0.app', 'icp0.io', 'localhost', '127.0.0.1'];
for (const host of knownHosts) {
delete window.location;
window.location = {
hostname: host,
protocol: 'https:',
} as any;
const agent = new HttpAgent({ fetch: jest.fn(), host });
expect((agent as any)._host.hostname).toBe(host);
}
});
});
12 changes: 11 additions & 1 deletion packages/agent/src/agent/http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,17 @@ export class HttpAgent implements Agent {
'Could not infer host from window.location, defaulting to mainnet gateway of https://icp-api.io. Please provide a host to the HttpAgent constructor to avoid this warning.',
);
}
this._host = new URL(location + '');
// Mainnet and local will have the api route available
const knownHosts = ['ic0.app', 'icp0.io', 'localhost', '127.0.0.1'];
if (location && knownHosts.includes(location.hostname)) {
// If the user is on a boundary-node provided host, we can use the same host for the agent
this._host = new URL(location + '');
} else {
this._host = new URL('https://icp-api.io');
console.warn(
'Could not infer host from window.location, defaulting to mainnet gateway of https://icp-api.io. Please provide a host to the HttpAgent constructor to avoid this warning.',
);
}
}
// Default is 3, only set from option if greater or equal to 0
this._retryTimes =
Expand Down