|
| 1 | +/** |
| 2 | + * @jest-environment jsdom |
| 3 | + */ |
| 4 | + |
| 5 | +/** |
| 6 | + * Internal dependencies |
| 7 | + */ |
| 8 | +import { determineUrlType } from '../url-type'; |
| 9 | + |
| 10 | +describe( 'determineUrlType', () => { |
| 11 | + test( 'should detect the correct type for absolute URLs', () => { |
| 12 | + expect( determineUrlType( 'http://example.com/' ) ).toBe( 'ABSOLUTE' ); |
| 13 | + expect( determineUrlType( 'http://www.example.com' ) ).toBe( 'ABSOLUTE' ); |
| 14 | + expect( determineUrlType( 'http://example.com/bar' ) ).toBe( 'ABSOLUTE' ); |
| 15 | + expect( determineUrlType( 'http://example.com/bar?baz=1' ) ).toBe( 'ABSOLUTE' ); |
| 16 | + expect( determineUrlType( new URL( 'http://example.com' ) ) ).toBe( 'ABSOLUTE' ); |
| 17 | + // From https://url.spec.whatwg.org/#urls |
| 18 | + expect( determineUrlType( 'https:example.org' ) ).toBe( 'ABSOLUTE' ); |
| 19 | + expect( determineUrlType( 'https://////example.com///' ) ).toBe( 'ABSOLUTE' ); |
| 20 | + expect( determineUrlType( 'https://example.com/././foo' ) ).toBe( 'ABSOLUTE' ); |
| 21 | + expect( determineUrlType( 'hello:world' ) ).toBe( 'ABSOLUTE' ); |
| 22 | + expect( determineUrlType( 'file:///C|/demo' ) ).toBe( 'ABSOLUTE' ); |
| 23 | + expect( determineUrlType( 'file://loc%61lhost/' ) ).toBe( 'ABSOLUTE' ); |
| 24 | + expect( determineUrlType( 'https://user:[email protected]/' ) ).toBe( 'ABSOLUTE' ); |
| 25 | + expect( determineUrlType( 'https://example.org/foo bar' ) ).toBe( 'ABSOLUTE' ); |
| 26 | + expect( determineUrlType( 'https://EXAMPLE.com/../x' ) ).toBe( 'ABSOLUTE' ); |
| 27 | + } ); |
| 28 | + |
| 29 | + test( 'should detect the correct type for protocol-relative URLs', () => { |
| 30 | + expect( determineUrlType( '//example.com/' ) ).toBe( 'SCHEME_RELATIVE' ); |
| 31 | + expect( determineUrlType( '//www.example.com' ) ).toBe( 'SCHEME_RELATIVE' ); |
| 32 | + expect( determineUrlType( '//example.com/bar' ) ).toBe( 'SCHEME_RELATIVE' ); |
| 33 | + expect( determineUrlType( '//example.com/bar?baz=1' ) ).toBe( 'SCHEME_RELATIVE' ); |
| 34 | + } ); |
| 35 | + |
| 36 | + test( 'should detect the correct type for root-relative URLs', () => { |
| 37 | + expect( determineUrlType( '/' ) ).toBe( 'PATH_ABSOLUTE' ); |
| 38 | + expect( determineUrlType( '/bar' ) ).toBe( 'PATH_ABSOLUTE' ); |
| 39 | + expect( determineUrlType( '/bar?baz=1' ) ).toBe( 'PATH_ABSOLUTE' ); |
| 40 | + } ); |
| 41 | + |
| 42 | + test( 'should detect the correct type for path-relative URLs', () => { |
| 43 | + expect( determineUrlType( '' ) ).toBe( 'PATH_RELATIVE' ); |
| 44 | + expect( determineUrlType( 'bar' ) ).toBe( 'PATH_RELATIVE' ); |
| 45 | + expect( determineUrlType( 'bar?baz=1' ) ).toBe( 'PATH_RELATIVE' ); |
| 46 | + expect( determineUrlType( 'bar#anchor' ) ).toBe( 'PATH_RELATIVE' ); |
| 47 | + expect( determineUrlType( '?query=param' ) ).toBe( 'PATH_RELATIVE' ); |
| 48 | + expect( determineUrlType( '#fragment' ) ).toBe( 'PATH_RELATIVE' ); |
| 49 | + } ); |
| 50 | + |
| 51 | + test( 'should detect the correct type for invalid URLs', () => { |
| 52 | + expect( determineUrlType( null ) ).toBe( 'INVALID' ); |
| 53 | + expect( determineUrlType( 0 ) ).toBe( 'INVALID' ); |
| 54 | + expect( determineUrlType( '///' ) ).toBe( 'INVALID' ); |
| 55 | + // From https://url.spec.whatwg.org/#urls |
| 56 | + expect( determineUrlType( 'https://ex ample.org/' ) ).toBe( 'INVALID' ); |
| 57 | + expect( determineUrlType( 'https://example.com:demo' ) ).toBe( 'INVALID' ); |
| 58 | + expect( determineUrlType( 'http://[www.example.com]/' ) ).toBe( 'INVALID' ); |
| 59 | + } ); |
| 60 | +} ); |
0 commit comments