|
| 1 | +import { absoluteUrl, _relativeToSiteRootUrl } from './absoluteUrl'; |
| 2 | + |
| 3 | +jest.mock('./baseURI', () => ({ |
| 4 | + baseURI: 'http://localhost:3000/', |
| 5 | +})); |
| 6 | + |
| 7 | +beforeEach(() => { |
| 8 | + absoluteUrl.defaultOptions = { rootUrl: 'http://localhost:3000/' }; |
| 9 | +}); |
| 10 | + |
| 11 | +describe('absoluteUrl', () => { |
| 12 | + it('should return the root URL with a trailing slash when no path is given', () => { |
| 13 | + expect(absoluteUrl(undefined, { rootUrl: 'http://example.com' })).toBe('http://example.com/'); |
| 14 | + }); |
| 15 | + |
| 16 | + it('should append the path to the root URL', () => { |
| 17 | + expect(absoluteUrl('foo/bar', { rootUrl: 'http://example.com' })).toBe('http://example.com/foo/bar'); |
| 18 | + }); |
| 19 | + |
| 20 | + it('should strip leading slashes from the path', () => { |
| 21 | + expect(absoluteUrl('///foo', { rootUrl: 'http://example.com' })).toBe('http://example.com/foo'); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should prepend http:// when the rootUrl has no protocol', () => { |
| 25 | + expect(absoluteUrl(undefined, { rootUrl: 'example.com' })).toBe('http://example.com/'); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should preserve https:// when already present in rootUrl', () => { |
| 29 | + expect(absoluteUrl('path', { rootUrl: 'https://example.com' })).toBe('https://example.com/path'); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should throw when rootUrl is not provided and defaultOptions.rootUrl is unset', () => { |
| 33 | + absoluteUrl.defaultOptions = {}; |
| 34 | + expect(() => absoluteUrl()).toThrow('Must pass options.rootUrl or set ROOT_URL in the server environment'); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should use defaultOptions.rootUrl when no rootUrl option is given', () => { |
| 38 | + absoluteUrl.defaultOptions = { rootUrl: 'http://default.example.com' }; |
| 39 | + expect(absoluteUrl('test')).toBe('http://default.example.com/test'); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should upgrade http to https when secure is true and host is not localhost', () => { |
| 43 | + expect(absoluteUrl('path', { rootUrl: 'http://example.com', secure: true })).toBe('https://example.com/path'); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should not upgrade to https for localhost when secure is true', () => { |
| 47 | + expect(absoluteUrl('path', { rootUrl: 'http://localhost:3000', secure: true })).toBe('http://localhost:3000/path'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should not upgrade to https for 127.0.0.1 when secure is true', () => { |
| 51 | + expect(absoluteUrl('path', { rootUrl: 'http://127.0.0.1:3000', secure: true })).toBe('http://127.0.0.1:3000/path'); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should replace localhost with 127.0.0.1 when replaceLocalhost is true', () => { |
| 55 | + expect(absoluteUrl('path', { rootUrl: 'http://localhost:3000', replaceLocalhost: true })).toBe('http://127.0.0.1:3000/path'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should not replace localhost when replaceLocalhost is false', () => { |
| 59 | + expect(absoluteUrl('path', { rootUrl: 'http://localhost:3000', replaceLocalhost: false })).toBe('http://localhost:3000/path'); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should accept options as the first argument when path is omitted', () => { |
| 63 | + expect(absoluteUrl({ rootUrl: 'http://example.com' } as any)).toBe('http://example.com/'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should not duplicate trailing slash', () => { |
| 67 | + expect(absoluteUrl(undefined, { rootUrl: 'http://example.com/' })).toBe('http://example.com/'); |
| 68 | + }); |
| 69 | +}); |
| 70 | + |
| 71 | +describe('_relativeToSiteRootUrl', () => { |
| 72 | + const originalConfig = (globalThis as any).__meteor_runtime_config__; |
| 73 | + |
| 74 | + afterEach(() => { |
| 75 | + (globalThis as any).__meteor_runtime_config__ = originalConfig; |
| 76 | + }); |
| 77 | + |
| 78 | + it('should prepend ROOT_URL_PATH_PREFIX to links starting with /', () => { |
| 79 | + (globalThis as any).__meteor_runtime_config__ = { ROOT_URL_PATH_PREFIX: '/subdir' }; |
| 80 | + expect(_relativeToSiteRootUrl('/route')).toBe('/subdir/route'); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should return the link unchanged when it does not start with /', () => { |
| 84 | + (globalThis as any).__meteor_runtime_config__ = { ROOT_URL_PATH_PREFIX: '/subdir' }; |
| 85 | + expect(_relativeToSiteRootUrl('route')).toBe('route'); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should return the link unchanged when __meteor_runtime_config__ is not an object', () => { |
| 89 | + delete (globalThis as any).__meteor_runtime_config__; |
| 90 | + expect(_relativeToSiteRootUrl('/route')).toBe('/route'); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should handle empty ROOT_URL_PATH_PREFIX', () => { |
| 94 | + (globalThis as any).__meteor_runtime_config__ = { ROOT_URL_PATH_PREFIX: '' }; |
| 95 | + expect(_relativeToSiteRootUrl('/route')).toBe('/route'); |
| 96 | + }); |
| 97 | +}); |
| 98 | + |
| 99 | +describe('defaultOptions', () => { |
| 100 | + it('should initialize rootUrl from baseURI', () => { |
| 101 | + expect(absoluteUrl.defaultOptions.rootUrl).toBe('http://localhost:3000/'); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should initialize secure from window.isSecureContext', () => { |
| 105 | + expect(absoluteUrl.defaultOptions.secure).toBe(window.isSecureContext); |
| 106 | + }); |
| 107 | +}); |
0 commit comments