|
10 | 10 | */
|
11 | 11 | class BaseUrlTest extends TestCase
|
12 | 12 | {
|
| 13 | + /** @dataProvider relativeTrueUrlProvider */ |
| 14 | + public function testIsRelativeWillReturnTrue($url) |
| 15 | + { |
| 16 | + $this->assertTrue(BaseUrl::isRelative($url)); |
| 17 | + } |
13 | 18 |
|
14 |
| - public function testIsRelativeWithAbsoluteUrlWillReturnFalse() |
| 19 | + /** @dataProvider relativeFalseUrlProvider */ |
| 20 | + public function testIsRelativeWillReturnFalse($url) |
15 | 21 | {
|
16 |
| - $this->assertFalse(BaseUrl::isRelative('https://acme.com/tnt-room=123')); |
| 22 | + $this->assertFalse(BaseUrl::isRelative($url)); |
17 | 23 | }
|
18 | 24 |
|
19 |
| - public function testUrlStartingWithDoubleSlashesWillReturnFalse() |
| 25 | + public function testEnsureSchemeWithRelativeUrlWillReturnInputUrl() |
20 | 26 | {
|
21 |
| - $this->assertFalse(BaseUrl::isRelative('//example.com')); |
| 27 | + $url = 'acme.com?name=bugs.bunny'; |
| 28 | + $this->assertEquals('acme.com?name=bugs.bunny', BaseUrl::ensureScheme($url, 'https')); |
22 | 29 | }
|
23 | 30 |
|
24 |
| - public function testIsRelativeWithRelativeUrlWillReturnTrue() |
| 31 | + public function testEnsureSchemeWithRelativeUrlWithAnotherUrlAsParamWillReturnInputUrl() |
25 | 32 | {
|
26 |
| - $this->assertTrue( |
27 |
| - BaseUrl::isRelative('acme.com/tnt-room=123') |
| 33 | + $this->assertEquals('acme.com/test?tnt-link=https://tnt.com/', |
| 34 | + BaseUrl::ensureScheme('acme.com/test?tnt-link=https://tnt.com/', 'https') |
28 | 35 | );
|
29 | 36 | }
|
30 | 37 |
|
31 |
| - public function testIsRelativeWithRelativeUrlHavingHttpsUrlAsParamValueWillReturnTrue() |
| 38 | + public function testEnsureSchemeWithSchemeNotAStringWillReturnInputUrl() |
32 | 39 | {
|
33 |
| - $this->assertTrue(BaseUrl::isRelative( |
34 |
| - 'acme.com/tnt-room-link=https://asd.com' |
35 |
| - )); |
| 40 | + $url = 'acme.com?name=bugs.bunny'; |
| 41 | + $this->assertEquals('acme.com?name=bugs.bunny', BaseUrl::ensureScheme($url, 123)); |
36 | 42 | }
|
37 | 43 |
|
38 |
| - public function testIsRelativeWithAbsoluteUrlHavingHttpsUrlAsParamValueWillReturnFalse() |
| 44 | + public function testEnsureSchemeWithProtocolRelativeUrlAndHttpsSchemeWillBeNormalized() |
39 | 45 | {
|
40 |
| - $this->assertFalse( |
41 |
| - BaseUrl::isRelative('https://acme.com/tnt-link=https://tnt.com') |
42 |
| - ); |
| 46 | + $url = '//acme.com?characters/list'; |
| 47 | + $this->assertEquals('https://acme.com?characters/list', BaseUrl::ensureScheme($url, 'https')); |
43 | 48 | }
|
44 | 49 |
|
45 |
| - public function testIsRelativeWithA() |
| 50 | + public function testEnsureSchemeWithProtocolRelativeUrlAndEmptySchemeWillBeReturned() |
46 | 51 | {
|
47 |
| - $this->assertTrue( |
48 |
| - BaseUrl::isRelative('/name=bugs.bunny') |
49 |
| - ); |
| 52 | + $url = '//acme.com?characters/list'; |
| 53 | + $this->assertEquals('//acme.com?characters/list', BaseUrl::ensureScheme($url, '')); |
50 | 54 | }
|
51 | 55 |
|
52 |
| - public function testIsRelativeWithFtpProtocolUrlWillReturnFalse() |
| 56 | + public function testAbsoluteUrlProtocolAndEmptySchemeWillCreateProtocolRelativeUrl() |
53 | 57 | {
|
54 |
| - $this->assertFalse( |
55 |
| - BaseUrl::isRelative('ftp://ftp.acme.com/tnt-suppliers.txt') |
56 |
| - ); |
| 58 | + $url = 'https://acme.com?characters/list'; |
| 59 | + $this->assertEquals('//acme.com?characters/list', BaseUrl::ensureScheme($url, '')); |
57 | 60 | }
|
58 | 61 |
|
59 |
| - public function testIsRelativeWithHttpUrl() |
| 62 | + public function testEnsureSchemeWithAbsoluteUrlWithAnotherUrlAsParamWillReturnInputUrl() |
60 | 63 | {
|
61 |
| - $this->assertFalse( |
62 |
| - BaseUrl::isRelative('http://no-protection.acme.com') |
63 |
| - ); |
| 64 | + $url = 'ss://acme.com/test?tnt-link=https://tnt.com/'; |
| 65 | + $this->assertEquals('https://acme.com/test?tnt-link=https://tnt.com/', BaseUrl::ensureScheme($url, 'https')); |
64 | 66 | }
|
65 | 67 |
|
66 |
| - public function testIsRelativeWithFileUrl() |
| 68 | + public function relativeTrueUrlProvider() |
67 | 69 | {
|
68 |
| - $this->assertFalse( |
69 |
| - BaseUrl::isRelative('file:///home/User/2ndFile.html') |
70 |
| - ); |
| 70 | + return [ |
| 71 | + 'url url without protocol' => [ |
| 72 | + 'url' => 'acme.com/tnt-room=123', |
| 73 | + ], |
| 74 | + 'url without protocol and another url in a parameter value' => [ |
| 75 | + 'url' => 'acme.com?tnt-room-link=https://tnt.com', |
| 76 | + ], |
| 77 | + 'path only' => [ |
| 78 | + 'url' => '/path', |
| 79 | + ], |
| 80 | + 'path with param' => [ |
| 81 | + 'url' => '/path=/home/user', |
| 82 | + ], |
| 83 | + ]; |
71 | 84 | }
|
72 | 85 |
|
| 86 | + public function relativeFalseUrlProvider() |
| 87 | + { |
| 88 | + return [ |
| 89 | + 'url with https protocol' => [ |
| 90 | + 'url' => 'https://acme.com', |
| 91 | + ], |
| 92 | + 'url with https protocol and ending slash' => [ |
| 93 | + 'url' => 'https://acme.com/', |
| 94 | + ], |
| 95 | + 'url with https protocol and another url as param value' => [ |
| 96 | + 'url' => 'https://acme.com?tnt-link=https://tnt.com', |
| 97 | + ], |
| 98 | + 'url starting with two slashes' => [ |
| 99 | + 'url' => '//example.com', |
| 100 | + ], |
| 101 | + 'url with ftp protocol' => [ |
| 102 | + 'url' => 'ftp://ftp.acme.com/tnt-suppliers.txt', |
| 103 | + ], |
| 104 | + 'url with http protocol' => [ |
| 105 | + 'url' => 'http://no-protection.acme.com', |
| 106 | + ], |
| 107 | + 'file url' => [ |
| 108 | + 'url' => 'file:///home/User/2ndFile.html', |
| 109 | + ] |
| 110 | + ]; |
| 111 | + } |
73 | 112 | }
|
0 commit comments