|
| 1 | +const axios = require('axios'); |
| 2 | +const { createAxiosInstance } = require('./index'); |
| 3 | + |
| 4 | +// Mock axios |
| 5 | +jest.mock('axios', () => ({ |
| 6 | + interceptors: { |
| 7 | + request: { use: jest.fn(), eject: jest.fn() }, |
| 8 | + response: { use: jest.fn(), eject: jest.fn() }, |
| 9 | + }, |
| 10 | + create: jest.fn().mockReturnValue({ |
| 11 | + defaults: { |
| 12 | + proxy: null, |
| 13 | + }, |
| 14 | + get: jest.fn().mockResolvedValue({ data: {} }), |
| 15 | + post: jest.fn().mockResolvedValue({ data: {} }), |
| 16 | + put: jest.fn().mockResolvedValue({ data: {} }), |
| 17 | + delete: jest.fn().mockResolvedValue({ data: {} }), |
| 18 | + }), |
| 19 | + get: jest.fn().mockResolvedValue({ data: {} }), |
| 20 | + post: jest.fn().mockResolvedValue({ data: {} }), |
| 21 | + put: jest.fn().mockResolvedValue({ data: {} }), |
| 22 | + delete: jest.fn().mockResolvedValue({ data: {} }), |
| 23 | + reset: jest.fn().mockImplementation(function () { |
| 24 | + this.get.mockClear(); |
| 25 | + this.post.mockClear(); |
| 26 | + this.put.mockClear(); |
| 27 | + this.delete.mockClear(); |
| 28 | + this.create.mockClear(); |
| 29 | + }), |
| 30 | +})); |
| 31 | + |
| 32 | +describe('createAxiosInstance', () => { |
| 33 | + const originalEnv = process.env; |
| 34 | + |
| 35 | + beforeEach(() => { |
| 36 | + // Reset mocks |
| 37 | + jest.clearAllMocks(); |
| 38 | + // Create a clean copy of process.env |
| 39 | + process.env = { ...originalEnv }; |
| 40 | + // Default: no proxy |
| 41 | + delete process.env.proxy; |
| 42 | + }); |
| 43 | + |
| 44 | + afterAll(() => { |
| 45 | + // Restore original process.env |
| 46 | + process.env = originalEnv; |
| 47 | + }); |
| 48 | + |
| 49 | + test('creates an axios instance without proxy when no proxy env is set', () => { |
| 50 | + const instance = createAxiosInstance(); |
| 51 | + |
| 52 | + expect(axios.create).toHaveBeenCalledTimes(1); |
| 53 | + expect(instance.defaults.proxy).toBeNull(); |
| 54 | + }); |
| 55 | + |
| 56 | + test('configures proxy correctly with hostname and protocol', () => { |
| 57 | + process.env.proxy = 'http://example.com'; |
| 58 | + |
| 59 | + const instance = createAxiosInstance(); |
| 60 | + |
| 61 | + expect(axios.create).toHaveBeenCalledTimes(1); |
| 62 | + expect(instance.defaults.proxy).toEqual({ |
| 63 | + host: 'example.com', |
| 64 | + protocol: 'http', |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + test('configures proxy correctly with hostname, protocol and port', () => { |
| 69 | + process.env.proxy = 'https://proxy.example.com:8080'; |
| 70 | + |
| 71 | + const instance = createAxiosInstance(); |
| 72 | + |
| 73 | + expect(axios.create).toHaveBeenCalledTimes(1); |
| 74 | + expect(instance.defaults.proxy).toEqual({ |
| 75 | + host: 'proxy.example.com', |
| 76 | + protocol: 'https', |
| 77 | + port: 8080, |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + test('handles proxy URLs with authentication', () => { |
| 82 | + process.env.proxy = 'http://user:[email protected]:3128'; |
| 83 | + |
| 84 | + const instance = createAxiosInstance(); |
| 85 | + |
| 86 | + expect(axios.create).toHaveBeenCalledTimes(1); |
| 87 | + expect(instance.defaults.proxy).toEqual({ |
| 88 | + host: 'proxy.example.com', |
| 89 | + protocol: 'http', |
| 90 | + port: 3128, |
| 91 | + // Note: The current implementation doesn't handle auth - if needed, add this functionality |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + test('throws error when proxy URL is invalid', () => { |
| 96 | + process.env.proxy = 'invalid-url'; |
| 97 | + |
| 98 | + expect(() => createAxiosInstance()).toThrow('Invalid proxy URL'); |
| 99 | + expect(axios.create).toHaveBeenCalledTimes(1); |
| 100 | + }); |
| 101 | + |
| 102 | + // If you want to test the actual URL parsing more thoroughly |
| 103 | + test('handles edge case proxy URLs correctly', () => { |
| 104 | + // IPv6 address |
| 105 | + process.env.proxy = 'http://[::1]:8080'; |
| 106 | + |
| 107 | + let instance = createAxiosInstance(); |
| 108 | + |
| 109 | + expect(instance.defaults.proxy).toEqual({ |
| 110 | + host: '::1', |
| 111 | + protocol: 'http', |
| 112 | + port: 8080, |
| 113 | + }); |
| 114 | + |
| 115 | + // URL with path (which should be ignored for proxy config) |
| 116 | + process.env.proxy = 'http://proxy.example.com:8080/some/path'; |
| 117 | + |
| 118 | + instance = createAxiosInstance(); |
| 119 | + |
| 120 | + expect(instance.defaults.proxy).toEqual({ |
| 121 | + host: 'proxy.example.com', |
| 122 | + protocol: 'http', |
| 123 | + port: 8080, |
| 124 | + }); |
| 125 | + }); |
| 126 | +}); |
0 commit comments