Skip to content

Commit

Permalink
fix: change rpc url validation; add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gtsonevv committed Dec 8, 2023
1 parent b6cf38e commit 563cce5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
4 changes: 2 additions & 2 deletions packages/providers/src/fetch_json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ export async function fetchJson(connectionInfoOrUrl: string | ConnectionInfo, js
connectionInfo = connectionInfoOrUrl as ConnectionInfo;
}

if (!connectionInfo.url || !connectionInfo.url.length && !connectionInfo.url[0]) {
throw new Error('RPC Server URL or the prioritized array of such URLs should not be empty');
if (Array.isArray(connectionInfo.url) && !connectionInfo.url.length) {
throw new Error('The prioritized array of RPC Server URLs should not be empty');
}

const currentRpcServer = typeof connectionInfo.url === 'string' ? connectionInfo.url : connectionInfo.url[0];
Expand Down
22 changes: 20 additions & 2 deletions packages/providers/test/providers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,30 @@ test('near json rpc fetch node status', async () => {
expect(response.chain_id).toBeTruthy();
});

test('near json rpc empty url', async () => {
test('near json rpc url - empty array', async () => {
const provider = new JsonRpcProvider({ url: [] });
try {
await provider.status();
} catch (e) {
expect(e.message).toEqual('RPC Server URL or the prioritized array of such URLs should not be empty');
expect(e.message).toEqual('The prioritized array of RPC Server URLs should not be empty');
}
});

test('near json rpc url - empty string', async () => {
const provider = new JsonRpcProvider({ url: '' });
try {
await provider.status();
} catch (e) {
expect(e.message).toEqual('Invalid URL');
}
});

test('near json rpc url - empty string array', async () => {
const provider = new JsonRpcProvider({ url: [''] });
try {
await provider.status();
} catch (e) {
expect(e.message).toEqual('Invalid URL');
}
});

Expand Down

0 comments on commit 563cce5

Please sign in to comment.