|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest' |
| 2 | +import { createClient } from '../client' |
| 3 | +import { MAINNET_RELAY_API } from '../constants' |
| 4 | +import { axios } from '../utils' |
| 5 | +import { zeroAddress } from 'viem' |
| 6 | + |
| 7 | +/** |
| 8 | + * Verifies that the x-api-key header is included in requests |
| 9 | + * when apiKey is configured in createClient. |
| 10 | + */ |
| 11 | + |
| 12 | +let axiosRequestSpy: ReturnType<typeof mockAxiosRequest> |
| 13 | + |
| 14 | +const mockAxiosRequest = () => { |
| 15 | + return vi.spyOn(axios, 'request').mockImplementation(() => { |
| 16 | + return Promise.resolve({ |
| 17 | + data: { status: 'success', balances: [], steps: [] }, |
| 18 | + status: 200 |
| 19 | + }) |
| 20 | + }) |
| 21 | +} |
| 22 | + |
| 23 | +const mockWallet = { |
| 24 | + vmType: 'evm' as const, |
| 25 | + getChainId: () => Promise.resolve(1), |
| 26 | + address: () => Promise.resolve(zeroAddress), |
| 27 | + handleSignMessageStep: vi.fn().mockResolvedValue('0x'), |
| 28 | + handleSendTransactionStep: vi.fn().mockResolvedValue('0x'), |
| 29 | + handleConfirmTransactionStep: vi |
| 30 | + .fn() |
| 31 | + .mockResolvedValue({ status: 'success' }), |
| 32 | + switchChain: vi.fn().mockResolvedValue(undefined) |
| 33 | +} |
| 34 | + |
| 35 | +describe('API Key Header Tests', () => { |
| 36 | + beforeEach(() => { |
| 37 | + vi.clearAllMocks() |
| 38 | + vi.resetAllMocks() |
| 39 | + axiosRequestSpy = mockAxiosRequest() |
| 40 | + }) |
| 41 | + |
| 42 | + it('Should include x-api-key header in getQuote when apiKey is configured', async () => { |
| 43 | + const client = createClient({ |
| 44 | + baseApiUrl: MAINNET_RELAY_API, |
| 45 | + apiKey: 'test-api-key' |
| 46 | + }) |
| 47 | + |
| 48 | + await client.actions.getQuote( |
| 49 | + { |
| 50 | + toChainId: 1, |
| 51 | + chainId: 8453, |
| 52 | + currency: '0x0000000000000000000000000000000000000000', |
| 53 | + toCurrency: '0x0000000000000000000000000000000000000000', |
| 54 | + tradeType: 'EXACT_INPUT', |
| 55 | + amount: '1000000000000000' |
| 56 | + }, |
| 57 | + true |
| 58 | + ) |
| 59 | + |
| 60 | + expect(axiosRequestSpy).toHaveBeenCalledWith( |
| 61 | + expect.objectContaining({ |
| 62 | + url: expect.stringContaining('/quote'), |
| 63 | + headers: expect.objectContaining({ |
| 64 | + 'x-api-key': 'test-api-key' |
| 65 | + }) |
| 66 | + }) |
| 67 | + ) |
| 68 | + }) |
| 69 | + |
| 70 | + it('Should include x-api-key header in getAppFees when apiKey is configured', async () => { |
| 71 | + const client = createClient({ |
| 72 | + baseApiUrl: MAINNET_RELAY_API, |
| 73 | + apiKey: 'test-api-key' |
| 74 | + }) |
| 75 | + |
| 76 | + await client.actions.getAppFees({ |
| 77 | + wallet: '0x0000000000000000000000000000000000000000' |
| 78 | + }) |
| 79 | + |
| 80 | + expect(axiosRequestSpy).toHaveBeenCalledWith( |
| 81 | + expect.objectContaining({ |
| 82 | + url: expect.stringContaining('/app-fees/'), |
| 83 | + headers: expect.objectContaining({ |
| 84 | + 'x-api-key': 'test-api-key' |
| 85 | + }) |
| 86 | + }) |
| 87 | + ) |
| 88 | + }) |
| 89 | + |
| 90 | + it('Should include x-api-key header in claimAppFees when apiKey is configured', async () => { |
| 91 | + const client = createClient({ |
| 92 | + baseApiUrl: MAINNET_RELAY_API, |
| 93 | + apiKey: 'test-api-key' |
| 94 | + }) |
| 95 | + |
| 96 | + try { |
| 97 | + await client.actions.claimAppFees({ |
| 98 | + wallet: mockWallet, |
| 99 | + chainId: 1, |
| 100 | + currency: '0x0000000000000000000000000000000000000000' |
| 101 | + }) |
| 102 | + } catch {} |
| 103 | + |
| 104 | + expect(axiosRequestSpy).toHaveBeenCalledWith( |
| 105 | + expect.objectContaining({ |
| 106 | + url: expect.stringContaining('/claim'), |
| 107 | + headers: expect.objectContaining({ |
| 108 | + 'x-api-key': 'test-api-key' |
| 109 | + }) |
| 110 | + }) |
| 111 | + ) |
| 112 | + }) |
| 113 | +}) |
0 commit comments