|
| 1 | +import { describe, expect, test } from '@jest/globals'; |
| 2 | +import { type NetParams } from '../../src'; |
| 3 | +import { firstTestnetBlock, network, testAccount } from './fixture'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Timeout for each test. |
| 7 | + * Overrides the default timeout of 5 seconds due to cases where the network request takes longer than 5 seconds. |
| 8 | + */ |
| 9 | +const TIMEOUT = 10000; |
| 10 | + |
| 11 | +/** |
| 12 | + * SimpleNet class tests |
| 13 | + * @group integration/network |
| 14 | + */ |
| 15 | +describe('Test SimpleNet class on Testnet', () => { |
| 16 | + /** |
| 17 | + * HTTP Request tests |
| 18 | + */ |
| 19 | + test( |
| 20 | + 'Should perform an HTTP GET request and resolve with response data', |
| 21 | + async () => { |
| 22 | + // Perform an HTTP GET request using the SimpleNet instance |
| 23 | + const response = await network.http( |
| 24 | + 'GET', |
| 25 | + '/blocks/1?expanded=false' |
| 26 | + ); |
| 27 | + |
| 28 | + // Assert that the response matches the expected firstTestnetBlock |
| 29 | + expect(JSON.stringify(response)).toEqual( |
| 30 | + JSON.stringify(firstTestnetBlock) |
| 31 | + ); |
| 32 | + }, |
| 33 | + TIMEOUT |
| 34 | + ); |
| 35 | + |
| 36 | + /** |
| 37 | + * HTTP Request tests rejecting with an error |
| 38 | + */ |
| 39 | + test( |
| 40 | + 'Should reject with an error if the HTTP request fails', |
| 41 | + async () => { |
| 42 | + // Assert that the HTTP request fails with an error |
| 43 | + await expect( |
| 44 | + network.http('GET', '/error-test-path') |
| 45 | + ).rejects.toThrow('404 get /error-test-path: 404 page not found'); |
| 46 | + }, |
| 47 | + TIMEOUT |
| 48 | + ); |
| 49 | + |
| 50 | + /** |
| 51 | + * Request params validation |
| 52 | + */ |
| 53 | + test( |
| 54 | + 'Should validate response headers', |
| 55 | + async () => { |
| 56 | + const customParams: NetParams = { |
| 57 | + query: {}, |
| 58 | + body: {}, |
| 59 | + headers: { |
| 60 | + 'X-Custom-Header': 'custom-value' |
| 61 | + }, |
| 62 | + validateResponseHeader: function ( |
| 63 | + headers: Record<string, string> |
| 64 | + ): void { |
| 65 | + expect(headers).toBeDefined(); |
| 66 | + } |
| 67 | + }; |
| 68 | + |
| 69 | + // Make an actual HTTP GET request and pass the validateResponseHeaders function |
| 70 | + const response = await network.http( |
| 71 | + 'GET', |
| 72 | + '/accounts/' + testAccount, |
| 73 | + customParams |
| 74 | + ); |
| 75 | + |
| 76 | + // You can also check the response data if needed |
| 77 | + expect(response).toBeDefined(); |
| 78 | + }, |
| 79 | + TIMEOUT |
| 80 | + ); |
| 81 | +}); |
0 commit comments