1<?php
2
3namespace Sabre\HTTP;
4
5class URLUtilTest extends \PHPUnit_Framework_TestCase{
6
7    function testEncodePath() {
8
9        $str = '';
10        for ($i = 0;$i < 128;$i++) $str .= chr($i);
11
12        $newStr = URLUtil::encodePath($str);
13
14        $this->assertEquals(
15            '%00%01%02%03%04%05%06%07%08%09%0a%0b%0c%0d%0e%0f' .
16            '%10%11%12%13%14%15%16%17%18%19%1a%1b%1c%1d%1e%1f' .
17            '%20%21%22%23%24%25%26%27()%2a%2b%2c-./' .
18            '0123456789:%3b%3c%3d%3e%3f' .
19            '@ABCDEFGHIJKLMNO' .
20            'PQRSTUVWXYZ%5b%5c%5d%5e_' .
21            '%60abcdefghijklmno' .
22            'pqrstuvwxyz%7b%7c%7d~%7f',
23            $newStr);
24
25        $this->assertEquals($str, URLUtil::decodePath($newStr));
26
27    }
28
29    function testEncodePathSegment() {
30
31        $str = '';
32        for ($i = 0;$i < 128;$i++) $str .= chr($i);
33
34        $newStr = URLUtil::encodePathSegment($str);
35
36        // Note: almost exactly the same as the last test, with the
37        // exception of the encoding of / (ascii code 2f)
38        $this->assertEquals(
39            '%00%01%02%03%04%05%06%07%08%09%0a%0b%0c%0d%0e%0f' .
40            '%10%11%12%13%14%15%16%17%18%19%1a%1b%1c%1d%1e%1f' .
41            '%20%21%22%23%24%25%26%27()%2a%2b%2c-.%2f' .
42            '0123456789:%3b%3c%3d%3e%3f' .
43            '@ABCDEFGHIJKLMNO' .
44            'PQRSTUVWXYZ%5b%5c%5d%5e_' .
45            '%60abcdefghijklmno' .
46            'pqrstuvwxyz%7b%7c%7d~%7f',
47            $newStr);
48
49        $this->assertEquals($str, URLUtil::decodePathSegment($newStr));
50
51    }
52
53    function testDecode() {
54
55        $str = 'Hello%20Test+Test2.txt';
56        $newStr = URLUtil::decodePath($str);
57        $this->assertEquals('Hello Test+Test2.txt', $newStr);
58
59    }
60
61    /**
62     * @depends testDecode
63     */
64    function testDecodeUmlaut() {
65
66        $str = 'Hello%C3%BC.txt';
67        $newStr = URLUtil::decodePath($str);
68        $this->assertEquals("Hello\xC3\xBC.txt", $newStr);
69
70    }
71
72    /**
73     * @depends testDecodeUmlaut
74     */
75    function testDecodeUmlautLatin1() {
76
77        $str = 'Hello%FC.txt';
78        $newStr = URLUtil::decodePath($str);
79        $this->assertEquals("Hello\xC3\xBC.txt", $newStr);
80
81    }
82
83    /**
84     * This testcase was sent by a bug reporter
85     *
86     * @depends testDecode
87     */
88    function testDecodeAccentsWindows7() {
89
90        $str = '/webdav/%C3%A0fo%C3%B3';
91        $newStr = URLUtil::decodePath($str);
92        $this->assertEquals(strtolower($str), URLUtil::encodePath($newStr));
93
94    }
95
96    function testSplitPath() {
97
98        $strings = [
99
100            // input                    // expected result
101            '/foo/bar'                 => ['/foo','bar'],
102            '/foo/bar/'                => ['/foo','bar'],
103            'foo/bar/'                 => ['foo','bar'],
104            'foo/bar'                  => ['foo','bar'],
105            'foo/bar/baz'              => ['foo/bar','baz'],
106            'foo/bar/baz/'             => ['foo/bar','baz'],
107            'foo'                      => ['','foo'],
108            'foo/'                     => ['','foo'],
109            '/foo/'                    => ['','foo'],
110            '/foo'                     => ['','foo'],
111            ''                         => [null,null],
112
113            // UTF-8
114            "/\xC3\xA0fo\xC3\xB3/bar"  => ["/\xC3\xA0fo\xC3\xB3",'bar'],
115            "/\xC3\xA0foo/b\xC3\xBCr/" => ["/\xC3\xA0foo","b\xC3\xBCr"],
116            "foo/\xC3\xA0\xC3\xBCr"    => ["foo","\xC3\xA0\xC3\xBCr"],
117
118        ];
119
120        foreach ($strings as $input => $expected) {
121
122            $output = URLUtil::splitPath($input);
123            $this->assertEquals($expected, $output, 'The expected output for \'' . $input . '\' was incorrect');
124
125
126        }
127
128    }
129
130    /**
131     * @dataProvider resolveData
132     */
133    function testResolve($base, $update, $expected) {
134
135        $this->assertEquals(
136            $expected,
137            URLUtil::resolve($base, $update)
138        );
139
140    }
141
142    function resolveData() {
143
144        return [
145            [
146                'http://example.org/foo/baz',
147                '/bar',
148                'http://example.org/bar',
149            ],
150            [
151                'https://example.org/foo',
152                '//example.net/',
153                'https://example.net/',
154            ],
155            [
156                'https://example.org/foo',
157                '?a=b',
158                'https://example.org/foo?a=b',
159            ],
160            [
161                '//example.org/foo',
162                '?a=b',
163                '//example.org/foo?a=b',
164            ],
165            // Ports and fragments
166            [
167                'https://example.org:81/foo#hey',
168                '?a=b#c=d',
169                'https://example.org:81/foo?a=b#c=d',
170            ],
171            // Relative.. in-directory paths
172            [
173                'http://example.org/foo/bar',
174                'bar2',
175                'http://example.org/foo/bar2',
176            ],
177            // Now the base path ended with a slash
178            [
179                'http://example.org/foo/bar/',
180                'bar2/bar3',
181                'http://example.org/foo/bar/bar2/bar3',
182            ],
183        ];
184
185    }
186
187}
188