|
| 1 | +import { SecretValue } from '@aztec/foundation/config'; |
| 2 | + |
| 3 | +import { getBeaconNodeFetchOptions } from './beacon_api.js'; |
| 4 | +import type { BlobSinkConfig } from './config.js'; |
| 5 | + |
| 6 | +describe('getBeaconNodeFetchOptions', () => { |
| 7 | + const mockConfig: BlobSinkConfig = {}; |
| 8 | + |
| 9 | + describe('URL construction', () => { |
| 10 | + it('should construct URL from string base URL', () => { |
| 11 | + const result = getBeaconNodeFetchOptions('http://localhost:3000', '/api/v1/blobs', mockConfig); |
| 12 | + expect(result.url.href).toBe('http://localhost:3000/api/v1/blobs'); |
| 13 | + }); |
| 14 | + |
| 15 | + it('should handle base URLs with paths - absolute API replaces path', () => { |
| 16 | + const result = getBeaconNodeFetchOptions('http://localhost:3000/base', '/api/v1/blobs', mockConfig); |
| 17 | + expect(result.url.href).toBe('http://localhost:3000/api/v1/blobs'); |
| 18 | + }); |
| 19 | + |
| 20 | + it('should handle base URLs with paths - relative API appends to path', () => { |
| 21 | + const result = getBeaconNodeFetchOptions('http://localhost:3000/base/', 'api/v1/blobs', mockConfig); |
| 22 | + expect(result.url.href).toBe('http://localhost:3000/base/api/v1/blobs'); |
| 23 | + }); |
| 24 | + }); |
| 25 | + |
| 26 | + describe('search params preservation', () => { |
| 27 | + it('should preserve search params from string base URL', () => { |
| 28 | + const result = getBeaconNodeFetchOptions( |
| 29 | + 'http://localhost:3000?existing=value&another=param', |
| 30 | + '/api/v1/blobs', |
| 31 | + mockConfig, |
| 32 | + ); |
| 33 | + |
| 34 | + expect(result.url.searchParams.get('existing')).toBe('value'); |
| 35 | + expect(result.url.searchParams.get('another')).toBe('param'); |
| 36 | + expect(result.url.href).toBe('http://localhost:3000/api/v1/blobs?existing=value&another=param'); |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + describe('API key as query parameter', () => { |
| 41 | + it('should add API key as query parameter when no header is specified', () => { |
| 42 | + const config: BlobSinkConfig = { |
| 43 | + l1ConsensusHostApiKeys: [new SecretValue('test-api-key')], |
| 44 | + }; |
| 45 | + |
| 46 | + const result = getBeaconNodeFetchOptions('http://localhost:3000', '/api/v1/blobs', config, 0); |
| 47 | + |
| 48 | + expect(result.url.searchParams.get('key')).toBe('test-api-key'); |
| 49 | + expect(result.url.href).toBe('http://localhost:3000/api/v1/blobs?key=test-api-key'); |
| 50 | + expect(result.headers).toBeUndefined(); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should add API key to existing search params', () => { |
| 54 | + const config: BlobSinkConfig = { |
| 55 | + l1ConsensusHostApiKeys: [new SecretValue('test-api-key')], |
| 56 | + }; |
| 57 | + |
| 58 | + const result = getBeaconNodeFetchOptions('http://localhost:3000?existing=value', '/api/v1/blobs', config, 0); |
| 59 | + |
| 60 | + expect(result.url.searchParams.get('existing')).toBe('value'); |
| 61 | + expect(result.url.searchParams.get('key')).toBe('test-api-key'); |
| 62 | + expect(result.url.href).toBe('http://localhost:3000/api/v1/blobs?existing=value&key=test-api-key'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should use correct API key based on index', () => { |
| 66 | + const config: BlobSinkConfig = { |
| 67 | + l1ConsensusHostApiKeys: [ |
| 68 | + new SecretValue('first-key'), |
| 69 | + new SecretValue('second-key'), |
| 70 | + new SecretValue('third-key'), |
| 71 | + ], |
| 72 | + }; |
| 73 | + |
| 74 | + const result1 = getBeaconNodeFetchOptions('http://localhost:3000', '/api/v1/blobs', config, 0); |
| 75 | + expect(result1.url.searchParams.get('key')).toBe('first-key'); |
| 76 | + |
| 77 | + const result2 = getBeaconNodeFetchOptions('http://localhost:3000', '/api/v1/blobs', config, 1); |
| 78 | + expect(result2.url.searchParams.get('key')).toBe('second-key'); |
| 79 | + |
| 80 | + const result3 = getBeaconNodeFetchOptions('http://localhost:3000', '/api/v1/blobs', config, 2); |
| 81 | + expect(result3.url.searchParams.get('key')).toBe('third-key'); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + describe('API key as header', () => { |
| 86 | + it('should add API key as header when header name is specified', () => { |
| 87 | + const config: BlobSinkConfig = { |
| 88 | + l1ConsensusHostApiKeys: [new SecretValue('test-api-key')], |
| 89 | + l1ConsensusHostApiKeyHeaders: ['X-API-Key'], |
| 90 | + }; |
| 91 | + |
| 92 | + const result = getBeaconNodeFetchOptions('http://localhost:3000', '/api/v1/blobs', config, 0); |
| 93 | + |
| 94 | + expect(result.url.searchParams.has('key')).toBe(false); |
| 95 | + expect(result.url.href).toBe('http://localhost:3000/api/v1/blobs'); |
| 96 | + expect(result.headers).toEqual({ |
| 97 | + 'X-API-Key': 'test-api-key', |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should use correct header name and API key based on index', () => { |
| 102 | + const config: BlobSinkConfig = { |
| 103 | + l1ConsensusHostApiKeys: [new SecretValue('first-key'), new SecretValue('second-key')], |
| 104 | + l1ConsensusHostApiKeyHeaders: ['X-API-Key-1', 'X-API-Key-2'], |
| 105 | + }; |
| 106 | + |
| 107 | + const result1 = getBeaconNodeFetchOptions('http://localhost:3000', '/api/v1/blobs', config, 0); |
| 108 | + expect(result1.headers).toEqual({ |
| 109 | + 'X-API-Key-1': 'first-key', |
| 110 | + }); |
| 111 | + |
| 112 | + const result2 = getBeaconNodeFetchOptions('http://localhost:3000', '/api/v1/blobs', config, 1); |
| 113 | + expect(result2.headers).toEqual({ |
| 114 | + 'X-API-Key-2': 'second-key', |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should preserve existing search params when using headers', () => { |
| 119 | + const config: BlobSinkConfig = { |
| 120 | + l1ConsensusHostApiKeys: [new SecretValue('test-api-key')], |
| 121 | + l1ConsensusHostApiKeyHeaders: ['Authorization'], |
| 122 | + }; |
| 123 | + |
| 124 | + const result = getBeaconNodeFetchOptions('http://localhost:3000?existing=value', '/api/v1/blobs', config, 0); |
| 125 | + |
| 126 | + expect(result.url.searchParams.get('existing')).toBe('value'); |
| 127 | + expect(result.url.searchParams.has('key')).toBe(false); |
| 128 | + expect(result.url.href).toBe('http://localhost:3000/api/v1/blobs?existing=value'); |
| 129 | + expect(result.headers).toEqual({ |
| 130 | + Authorization: 'test-api-key', |
| 131 | + }); |
| 132 | + }); |
| 133 | + }); |
| 134 | + |
| 135 | + describe('edge cases', () => { |
| 136 | + it('should handle URLs with special characters', () => { |
| 137 | + const result = getBeaconNodeFetchOptions( |
| 138 | + 'http://localhost:3000?query=hello%20world', |
| 139 | + '/api/v1/blobs', |
| 140 | + mockConfig, |
| 141 | + ); |
| 142 | + |
| 143 | + expect(result.url.searchParams.get('query')).toBe('hello world'); |
| 144 | + }); |
| 145 | + |
| 146 | + it('should handle complex URL combinations', () => { |
| 147 | + const config: BlobSinkConfig = { |
| 148 | + l1ConsensusHostApiKeys: [new SecretValue('complex-key')], |
| 149 | + l1ConsensusHostApiKeyHeaders: ['Authorization'], |
| 150 | + }; |
| 151 | + |
| 152 | + const result = getBeaconNodeFetchOptions( |
| 153 | + 'https://api.example.com:8080/base?existing=value&another=test', |
| 154 | + '/beacon/api/v1/blobs', |
| 155 | + config, |
| 156 | + 0, |
| 157 | + ); |
| 158 | + |
| 159 | + expect(result.url.href).toBe('https://api.example.com:8080/beacon/api/v1/blobs?existing=value&another=test'); |
| 160 | + expect(result.headers).toEqual({ |
| 161 | + Authorization: 'complex-key', |
| 162 | + }); |
| 163 | + }); |
| 164 | + }); |
| 165 | +}); |
0 commit comments