|
| 1 | +/* |
| 2 | +Copyright 2025 Adobe. All rights reserved. |
| 3 | +This file is licensed to you under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. You may obtain a copy |
| 5 | +of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +Unless required by applicable law or agreed to in writing, software distributed under |
| 7 | +the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS |
| 8 | +OF ANY KIND, either express or implied. See the License for the specific language |
| 9 | +governing permissions and limitations under the License. |
| 10 | +*/ |
| 11 | + |
| 12 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 13 | +import getMtlsFetch from '../getMtlsFetch.js'; |
| 14 | +import { MTLS_BINDING } from '../constants.js'; |
| 15 | + |
| 16 | +describe('getMtlsFetch', () => { |
| 17 | + let mockEnv; |
| 18 | + let mockFetch; |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + mockFetch = vi.fn(); |
| 22 | + mockEnv = {}; |
| 23 | + }); |
| 24 | + |
| 25 | + it('should return the MTLS fetch function when the binding exists', () => { |
| 26 | + // Arrange |
| 27 | + mockEnv[MTLS_BINDING] = { fetch: mockFetch }; |
| 28 | + |
| 29 | + // Act |
| 30 | + const result = getMtlsFetch(mockEnv); |
| 31 | + |
| 32 | + // Assert |
| 33 | + expect(result).toBe(mockFetch); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should return a function that throws an error when the binding does not exist', () => { |
| 37 | + // Arrange |
| 38 | + mockEnv = {}; // Empty env without MTLS binding |
| 39 | + |
| 40 | + // Act |
| 41 | + const result = getMtlsFetch(mockEnv); |
| 42 | + |
| 43 | + // Assert |
| 44 | + expect(() => result()).toThrowError( |
| 45 | + 'MTLS certificate not found on the worker' |
| 46 | + ); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should handle undefined env gracefully', () => { |
| 50 | + // Act |
| 51 | + const result = getMtlsFetch(undefined); |
| 52 | + |
| 53 | + // Assert |
| 54 | + expect(result).toBeInstanceOf(Function); |
| 55 | + expect(() => result()).toThrowError( |
| 56 | + 'MTLS certificate not found on the worker' |
| 57 | + ); |
| 58 | + }); |
| 59 | +}); |
0 commit comments