|
| 1 | +/** |
| 2 | + * Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com). |
| 3 | + * |
| 4 | + * WSO2 LLC. licenses this file to you under the Apache License, |
| 5 | + * Version 2.0 (the "License"); you may not use this file except |
| 6 | + * in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, |
| 12 | + * software distributed under the License is distributed on an |
| 13 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | + * KIND, either express or implied. See the License for the |
| 15 | + * specific language governing permissions and limitations |
| 16 | + * under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +import {describe, it, expect, vi} from 'vitest'; |
| 20 | +import isRecognizedBaseUrlPattern from '../isRecognizedBaseUrlPattern'; |
| 21 | +import AsgardeoRuntimeError from '../../errors/AsgardeoRuntimeError'; |
| 22 | + |
| 23 | +vi.mock('../logger', () => ({default: {warn: vi.fn()}})); |
| 24 | + |
| 25 | +describe('isRecognizedBaseUrlPattern', () => { |
| 26 | + it('returns true for recognized Asgardeo base URL pattern', () => { |
| 27 | + expect(isRecognizedBaseUrlPattern('https://dev.asgardeo.io/t/dxlab')).toBe(true); |
| 28 | + expect(isRecognizedBaseUrlPattern('https://example.com/t/org')).toBe(true); |
| 29 | + expect(isRecognizedBaseUrlPattern('https://foo.com/t/bar/')).toBe(true); |
| 30 | + expect(isRecognizedBaseUrlPattern('https://foo.com/t/bar/extra')).toBe(true); |
| 31 | + }); |
| 32 | + |
| 33 | + it('returns false for unrecognized base URL pattern', () => { |
| 34 | + expect(isRecognizedBaseUrlPattern('https://dev.asgardeo.io/tenant/dxlab')).toBe(false); |
| 35 | + expect(isRecognizedBaseUrlPattern('https://dev.asgardeo.io/')).toBe(false); |
| 36 | + expect(isRecognizedBaseUrlPattern('https://dev.asgardeo.io/t')).toBe(false); |
| 37 | + expect(isRecognizedBaseUrlPattern('https://dev.asgardeo.io/other/path')).toBe(false); |
| 38 | + }); |
| 39 | + |
| 40 | + it('throws AsgardeoRuntimeError if baseUrl is undefined', () => { |
| 41 | + expect(() => isRecognizedBaseUrlPattern(undefined)).toThrow(AsgardeoRuntimeError); |
| 42 | + |
| 43 | + try { |
| 44 | + isRecognizedBaseUrlPattern(undefined); |
| 45 | + } catch (e: any) { |
| 46 | + expect(e).toBeInstanceOf(AsgardeoRuntimeError); |
| 47 | + expect(e.message).toMatch(/Base URL is required/); |
| 48 | + } |
| 49 | + }); |
| 50 | + |
| 51 | + it('throws AsgardeoRuntimeError for invalid URL format', () => { |
| 52 | + expect(() => isRecognizedBaseUrlPattern('not-a-valid-url')).toThrow(AsgardeoRuntimeError); |
| 53 | + |
| 54 | + try { |
| 55 | + isRecognizedBaseUrlPattern('not-a-valid-url'); |
| 56 | + } catch (e: any) { |
| 57 | + expect(e).toBeInstanceOf(AsgardeoRuntimeError); |
| 58 | + expect(e.message).toMatch(/Invalid base URL format/); |
| 59 | + } |
| 60 | + }); |
| 61 | +}); |
0 commit comments