|
| 1 | +/// <reference types="mocha" /> |
| 2 | +import * as assert from 'assert'; |
| 3 | + |
| 4 | +// Use require to get the actual module with latest exports |
| 5 | +import * as jfrogUtils from '@jfrog/tasks-utils'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Simulates the platformUrl resolution logic from utils.js lines 441-449: |
| 9 | + * |
| 10 | + * let platformUrl = ""; |
| 11 | + * try { |
| 12 | + * platformUrl = tl.getEndpointAuthorizationParameter(service, 'jfrogPlatformUrl', true); |
| 13 | + * } catch (error) { |
| 14 | + * console.warn('Failed to get platform url from field: ' + error + "\nparsing from url instead"); |
| 15 | + * } |
| 16 | + * if (!platformUrl || !platformUrl.trim()) { |
| 17 | + * platformUrl = parsePlatformUrlFromServiceUrl(serviceUrl); |
| 18 | + * } |
| 19 | + * |
| 20 | + * @param getEndpointResult - Simulated result from tl.getEndpointAuthorizationParameter |
| 21 | + * @param shouldThrow - Whether the call should throw an error |
| 22 | + * @param serviceUrl - The service URL to parse from if platformUrl is not available |
| 23 | + * @returns The resolved platform URL |
| 24 | + */ |
| 25 | +function simulatePlatformUrlResolution( |
| 26 | + getEndpointResult: string | undefined | null, |
| 27 | + shouldThrow: boolean, |
| 28 | + serviceUrl: string, |
| 29 | +): string { |
| 30 | + let platformUrl: string | undefined | null = ''; |
| 31 | + try { |
| 32 | + if (shouldThrow) { |
| 33 | + throw new Error('Simulated error'); |
| 34 | + } |
| 35 | + platformUrl = getEndpointResult; |
| 36 | + } catch { |
| 37 | + // Simulates: console.warn('Failed to get platform url from field...') |
| 38 | + } |
| 39 | + if (!platformUrl || !platformUrl.trim()) { |
| 40 | + platformUrl = jfrogUtils.parsePlatformUrlFromServiceUrl(serviceUrl); |
| 41 | + } |
| 42 | + return platformUrl; |
| 43 | +} |
| 44 | + |
| 45 | +describe('Utils Unit Tests', (): void => { |
| 46 | + describe('platformUrl resolution (simulating tl.getEndpointAuthorizationParameter)', (): void => { |
| 47 | + const serviceUrl: string = 'https://example.jfrog.io/artifactory'; |
| 48 | + |
| 49 | + it('should use platformUrl directly when getEndpointAuthorizationParameter returns valid URL', (): void => { |
| 50 | + const result: string = simulatePlatformUrlResolution('https://my-platform.jfrog.io', false, serviceUrl); |
| 51 | + assert.strictEqual(result, 'https://my-platform.jfrog.io'); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should parse from serviceUrl when getEndpointAuthorizationParameter returns empty string', (): void => { |
| 55 | + const result: string = simulatePlatformUrlResolution('', false, serviceUrl); |
| 56 | + assert.strictEqual(result, 'https://example.jfrog.io'); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should parse from serviceUrl when getEndpointAuthorizationParameter returns undefined', (): void => { |
| 60 | + const result: string = simulatePlatformUrlResolution(undefined, false, serviceUrl); |
| 61 | + assert.strictEqual(result, 'https://example.jfrog.io'); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should parse from serviceUrl when getEndpointAuthorizationParameter returns null', (): void => { |
| 65 | + const result: string = simulatePlatformUrlResolution(null, false, serviceUrl); |
| 66 | + assert.strictEqual(result, 'https://example.jfrog.io'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should parse from serviceUrl when getEndpointAuthorizationParameter returns whitespace only', (): void => { |
| 70 | + const result: string = simulatePlatformUrlResolution(' ', false, serviceUrl); |
| 71 | + assert.strictEqual(result, 'https://example.jfrog.io'); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should parse from serviceUrl when getEndpointAuthorizationParameter throws error', (): void => { |
| 75 | + const result: string = simulatePlatformUrlResolution('', true, serviceUrl); |
| 76 | + assert.strictEqual(result, 'https://example.jfrog.io'); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should parse from xray serviceUrl when platformUrl not available', (): void => { |
| 80 | + const result: string = simulatePlatformUrlResolution('', false, 'https://example.jfrog.io/xray'); |
| 81 | + assert.strictEqual(result, 'https://example.jfrog.io'); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should parse from distribution serviceUrl when platformUrl not available', (): void => { |
| 85 | + const result: string = simulatePlatformUrlResolution('', false, 'https://example.jfrog.io/distribution'); |
| 86 | + assert.strictEqual(result, 'https://example.jfrog.io'); |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + describe('parsePlatformUrlFromServiceUrl', (): void => { |
| 91 | + it('should strip /artifactory suffix', (): void => { |
| 92 | + assert.strictEqual( |
| 93 | + jfrogUtils.parsePlatformUrlFromServiceUrl('https://example.jfrog.io/artifactory'), |
| 94 | + 'https://example.jfrog.io', |
| 95 | + ); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should strip /xray suffix', (): void => { |
| 99 | + assert.strictEqual( |
| 100 | + jfrogUtils.parsePlatformUrlFromServiceUrl('https://example.jfrog.io/xray'), |
| 101 | + 'https://example.jfrog.io', |
| 102 | + ); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should strip /distribution suffix', (): void => { |
| 106 | + assert.strictEqual( |
| 107 | + jfrogUtils.parsePlatformUrlFromServiceUrl('https://example.jfrog.io/distribution'), |
| 108 | + 'https://example.jfrog.io', |
| 109 | + ); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should handle case-insensitive matching for /Artifactory', (): void => { |
| 113 | + assert.strictEqual( |
| 114 | + jfrogUtils.parsePlatformUrlFromServiceUrl('https://example.jfrog.io/Artifactory'), |
| 115 | + 'https://example.jfrog.io', |
| 116 | + ); |
| 117 | + }); |
| 118 | + |
| 119 | + it('should handle case-insensitive matching for /XRAY', (): void => { |
| 120 | + assert.strictEqual( |
| 121 | + jfrogUtils.parsePlatformUrlFromServiceUrl('https://example.jfrog.io/XRAY'), |
| 122 | + 'https://example.jfrog.io', |
| 123 | + ); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should return URL as-is when no known suffix', (): void => { |
| 127 | + assert.strictEqual( |
| 128 | + jfrogUtils.parsePlatformUrlFromServiceUrl('https://example.jfrog.io/other'), |
| 129 | + 'https://example.jfrog.io/other', |
| 130 | + ); |
| 131 | + }); |
| 132 | + |
| 133 | + it('should handle URL with port', (): void => { |
| 134 | + assert.strictEqual( |
| 135 | + jfrogUtils.parsePlatformUrlFromServiceUrl('https://example.jfrog.io:8080/artifactory'), |
| 136 | + 'https://example.jfrog.io:8080', |
| 137 | + ); |
| 138 | + }); |
| 139 | + |
| 140 | + it('should handle URL with trailing slash on suffix', (): void => { |
| 141 | + assert.strictEqual( |
| 142 | + jfrogUtils.parsePlatformUrlFromServiceUrl('https://example.jfrog.io/artifactory/'), |
| 143 | + 'https://example.jfrog.io', |
| 144 | + ); |
| 145 | + }); |
| 146 | + |
| 147 | + it('should handle URL with path prefix', (): void => { |
| 148 | + assert.strictEqual( |
| 149 | + jfrogUtils.parsePlatformUrlFromServiceUrl('https://example.com/jfrog/artifactory'), |
| 150 | + 'https://example.com/jfrog', |
| 151 | + ); |
| 152 | + }); |
| 153 | + |
| 154 | + it('should handle URL without any prefix', (): void => { |
| 155 | + assert.strictEqual(jfrogUtils.parsePlatformUrlFromServiceUrl('https://example.com/jfrog'), 'https://example.com/jfrog'); |
| 156 | + }); |
| 157 | + |
| 158 | + it('should handle hostnames as well', (): void => { |
| 159 | + assert.strictEqual(jfrogUtils.parsePlatformUrlFromServiceUrl('https://artifactory.com/jfrog/artifactory'), 'https://artifactory.com/jfrog'); |
| 160 | + }); |
| 161 | + }); |
| 162 | +}); |
0 commit comments