forked from near/near-api-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add test fetch_json_error.test.ts, add: 503 handler
- Loading branch information
Showing
2 changed files
with
82 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { describe, expect, test, jest, afterEach } from '@jest/globals'; | ||
import { fetchJsonRpc } from '../src/fetch_json'; | ||
import unfetch from 'isomorphic-unfetch'; | ||
import { ProviderError } from '../src/fetch_json'; | ||
|
||
jest.mock('isomorphic-unfetch'); | ||
|
||
describe('fetchJsonError', () => { | ||
const RPC_URL = 'https://rpc.testnet.near.org'; | ||
const statusRequest = { | ||
jsonrpc: '2.0', | ||
id: 'dontcare', | ||
method: 'status', | ||
params: [], | ||
}; | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test('handles 500 Internal Server Error', async () => { | ||
(unfetch as jest.Mock).mockReturnValue({ | ||
ok: false, | ||
status: 500, | ||
text: '', | ||
json: {}, | ||
}); | ||
|
||
// @ts-expect-error test input | ||
await expect(fetchJsonRpc(RPC_URL, statusRequest, undefined)) | ||
.rejects | ||
.toThrowError(new ProviderError('Internal server error', { cause: 500 })); | ||
}); | ||
test('handles 408 Timeout Error', async () => { | ||
(unfetch as jest.Mock).mockReturnValue({ | ||
ok: false, | ||
status: 408, | ||
text: '', | ||
json: {}, | ||
}); | ||
// @ts-expect-error test input | ||
await expect(fetchJsonRpc(RPC_URL, statusRequest, undefined)) | ||
.rejects | ||
.toThrowError(new ProviderError('Timeout error', { cause: 408 })); | ||
}); | ||
// }); | ||
|
||
test('handles 400 Request Validation Error', async () => { | ||
(unfetch as jest.Mock).mockReturnValue({ | ||
ok: false, | ||
status: 400, | ||
text: '', | ||
json: {}, | ||
}); | ||
// @ts-expect-error test input | ||
await expect(fetchJsonRpc(RPC_URL, statusRequest, undefined)) | ||
.rejects | ||
.toThrowError(new ProviderError('Request validation error', { cause: 400 })); | ||
}); | ||
|
||
test('handles 503 Service Unavailable', async () => { | ||
(unfetch as jest.Mock).mockReturnValue({ | ||
ok: false, | ||
status: 503, | ||
text: '', | ||
json: {}, | ||
}); | ||
|
||
// @ts-expect-error test input | ||
await expect(fetchJsonRpc(RPC_URL, statusRequest, undefined)) | ||
.rejects | ||
.toThrowError(new ProviderError(`${RPC_URL} unavailable`, { cause: 503 })); | ||
}); | ||
}); |