File size: 5,539 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
 * @jest-environment jsdom
 */

import { format } from '../src';

describe( 'getUrlType', () => {
	test( 'should format absolute URLs as absolute URLs', () => {
		expect( format( 'http://example.com/', 'ABSOLUTE' ) ).toBe( 'http://example.com/' );
		expect( format( 'http://example.com:8080/', 'ABSOLUTE' ) ).toBe( 'http://example.com:8080/' );
		expect( format( 'http://www.example.com', 'ABSOLUTE' ) ).toBe( 'http://www.example.com/' );
		expect( format( 'http://example.com/foo?b=a#z', 'ABSOLUTE' ) ).toBe(
			'http://example.com/foo?b=a#z'
		);
		expect( format( 'http://example.com/foo/bar/../baz', 'ABSOLUTE' ) ).toBe(
			'http://example.com/foo/baz'
		);
		// Normalization examples from https://url.spec.whatwg.org/#urls
		expect( format( 'https:example.org', 'ABSOLUTE' ) ).toBe( 'https://example.org/' );
		expect( format( 'https://////example.com///', 'ABSOLUTE' ) ).toBe( 'https://example.com///' );
		expect( format( 'https://example.com/././foo', 'ABSOLUTE' ) ).toBe( 'https://example.com/foo' );
		expect( format( 'file:///C|/demo', 'ABSOLUTE' ) ).toBe( 'file:///C:/demo' );
		expect( format( 'file://loc%61lhost/', 'ABSOLUTE' ) ).toBe( 'file:///' );
		expect( format( 'https://user:password@example.org/', 'ABSOLUTE' ) ).toBe(
			'https://user:password@example.org/'
		);
		expect( format( 'https://example.org/foo bar', 'ABSOLUTE' ) ).toBe(
			'https://example.org/foo%20bar'
		);
		expect( format( 'https://EXAMPLE.com/../x', 'ABSOLUTE' ) ).toBe( 'https://example.com/x' );
	} );

	test( 'should format absolute URLs as scheme-relative URLs', () => {
		expect( format( 'http://example.com/', 'SCHEME_RELATIVE' ) ).toBe( '//example.com/' );
		expect( format( 'http://example.com:8080/', 'SCHEME_RELATIVE' ) ).toBe( '//example.com:8080/' );
		expect( format( 'http://www.example.com', 'SCHEME_RELATIVE' ) ).toBe( '//www.example.com/' );
		expect( format( 'http://example.com/foo?b=a#z', 'SCHEME_RELATIVE' ) ).toBe(
			'//example.com/foo?b=a#z'
		);
		expect( format( 'http://example.com/foo/bar/../baz', 'SCHEME_RELATIVE' ) ).toBe(
			'//example.com/foo/baz'
		);
	} );

	test( 'should format scheme-relative URLs as scheme-relative URLs', () => {
		expect( format( '//example.com/', 'SCHEME_RELATIVE' ) ).toBe( '//example.com/' );
		expect( format( '//example.com:8080/', 'SCHEME_RELATIVE' ) ).toBe( '//example.com:8080/' );
		expect( format( '//www.example.com', 'SCHEME_RELATIVE' ) ).toBe( '//www.example.com/' );
		expect( format( '//example.com/foo?b=a#z', 'SCHEME_RELATIVE' ) ).toBe(
			'//example.com/foo?b=a#z'
		);
		expect( format( '//example.com/foo/bar/../baz', 'SCHEME_RELATIVE' ) ).toBe(
			'//example.com/foo/baz'
		);
	} );

	test( 'should format absolute URLs as path-absolute URLs', () => {
		expect( format( 'http://example.com/', 'PATH_ABSOLUTE' ) ).toBe( '/' );
		expect( format( 'http://example.com:8080/', 'PATH_ABSOLUTE' ) ).toBe( '/' );
		expect( format( 'http://www.example.com', 'PATH_ABSOLUTE' ) ).toBe( '/' );
		expect( format( 'http://example.com/foo?b=a#z', 'PATH_ABSOLUTE' ) ).toBe( '/foo?b=a#z' );
	} );

	test( 'should format scheme-relative URLs as path-absolute URLs', () => {
		expect( format( '//example.com/', 'PATH_ABSOLUTE' ) ).toBe( '/' );
		expect( format( '//example.com:8080/', 'PATH_ABSOLUTE' ) ).toBe( '/' );
		expect( format( '//www.example.com', 'PATH_ABSOLUTE' ) ).toBe( '/' );
		expect( format( '//example.com/foo?b=a#z', 'PATH_ABSOLUTE' ) ).toBe( '/foo?b=a#z' );
		expect( format( '//example.com/foo/bar/../baz', 'PATH_ABSOLUTE' ) ).toBe( '/foo/baz' );
	} );

	test( 'should format path-absolute URLs as path-absolute URLs', () => {
		expect( format( '/', 'PATH_ABSOLUTE' ) ).toBe( '/' );
		expect( format( '/foo?b=a#z', 'PATH_ABSOLUTE' ) ).toBe( '/foo?b=a#z' );
		expect( format( '/foo/bar/../baz', 'PATH_ABSOLUTE' ) ).toBe( '/foo/baz' );
	} );

	test( 'should format a URL into its own type when a format is not specified', () => {
		expect( format( 'http://example.com/foo' ) ).toBe( 'http://example.com/foo' );
		expect( format( '//example.com/foo' ) ).toBe( '//example.com/foo' );
		expect( format( '/foo' ) ).toBe( '/foo' );
	} );

	test( 'should throw if passed an invalid URL', () => {
		expect( () => format( null, 'PATH_ABSOLUTE' ) ).toThrow();
		expect( () => format( '///', 'PATH_ABSOLUTE' ) ).toThrow();
	} );

	test( 'should throw if passed an invalid url type into which to format', () => {
		expect( () => format( 'http://example.com/', 'INVALID' ) ).toThrow();
		expect( () => format( 'http://example.com/', 'NONEXISTENT' ) ).toThrow();
	} );

	test( 'should throw if attempting to format a URL as a path-relative URL', () => {
		expect( () => format( 'http://example.com/', 'PATH_RELATIVE' ) ).toThrow();
		expect( () => format( 'foo', 'PATH_RELATIVE' ) ).toThrow();
	} );

	test( 'should throw if passed a path-absolute URL to format as scheme-relative', () => {
		expect( () => format( '/', 'SCHEME_RELATIVE' ) ).toThrow();
	} );

	test( 'should throw if passed a path-relative URL to format as scheme-relative', () => {
		expect( () => format( 'foo', 'SCHEME_RELATIVE' ) ).toThrow();
	} );

	test( 'should throw if passed a path-absolute URL to format as absolute', () => {
		expect( () => format( '/', 'ABSOLUTE' ) ).toThrow();
	} );

	test( 'should throw if passed a pathrelative URL to format as absolute', () => {
		expect( () => format( 'foo', 'ABSOLUTE' ) ).toThrow();
	} );

	test( 'should throw if passed a scheme-relative URL to format as absolute', () => {
		expect( () => format( '//example.com/', 'ABSOLUTE' ) ).toThrow();
	} );
} );