|
1 | 1 | import { describe, expect, test } from 'vitest'; |
| 2 | +import { base64UrlDecode } from '../lib/base64.js'; |
2 | 3 | import { buildPushHTTPRequest } from '../lib/main.js'; |
3 | 4 | import type { |
4 | 5 | BuilderOptions, |
@@ -217,6 +218,35 @@ describe('Subscription Keys Validation', () => { |
217 | 218 | }); |
218 | 219 | }); |
219 | 220 |
|
| 221 | +// ============================================================================ |
| 222 | +// Base64 Decoding Tests |
| 223 | +// ============================================================================ |
| 224 | +describe('Base64 URL Decoding', () => { |
| 225 | + test('decodes to exact byte length in default runtime path', () => { |
| 226 | + const decoded = new Uint8Array(base64UrlDecode('AQ')); |
| 227 | + |
| 228 | + expect(decoded).toEqual(new Uint8Array([1])); |
| 229 | + expect(decoded.byteLength).toBe(1); |
| 230 | + }); |
| 231 | + |
| 232 | + test('decodes to exact byte length in Buffer fallback path', () => { |
| 233 | + const originalAtob = globalThis.atob; |
| 234 | + // Force Node fallback path to verify sliced ArrayBuffer behavior. |
| 235 | + // `Buffer#buffer` alone would expose backing store, not exact bytes. |
| 236 | + // @ts-expect-error test override |
| 237 | + globalThis.atob = undefined; |
| 238 | + |
| 239 | + try { |
| 240 | + const decoded = new Uint8Array(base64UrlDecode('AQ')); |
| 241 | + |
| 242 | + expect(decoded).toEqual(new Uint8Array([1])); |
| 243 | + expect(decoded.byteLength).toBe(1); |
| 244 | + } finally { |
| 245 | + globalThis.atob = originalAtob; |
| 246 | + } |
| 247 | + }); |
| 248 | +}); |
| 249 | + |
220 | 250 | // ============================================================================ |
221 | 251 | // TTL Validation Tests |
222 | 252 | // ============================================================================ |
|
0 commit comments