Merge "Type hint against LinkTarget in WatchedItemStore"
[lhc/web/wiklou.git] / tests / phpunit / unit / includes / http / HttpUnitTest.php
1 <?php
2
3 /**
4 * @covers Http
5 * @group Http
6 * @group small
7 */
8 class HttpUnitTest extends MediaWikiUnitTestCase {
9
10 /**
11 * Test Http::isValidURI()
12 * T29854 : Http::isValidURI is too lax
13 * @dataProvider provideURI
14 * @covers Http::isValidURI
15 */
16 public function testIsValidUri( $expect, $URI, $message = '' ) {
17 $this->assertEquals(
18 $expect,
19 (bool)Http::isValidURI( $URI ),
20 $message
21 );
22 }
23
24 /**
25 * Feeds URI to test a long regular expression in Http::isValidURI
26 */
27 public static function provideURI() {
28 /** Format: 'boolean expectation', 'URI to test', 'Optional message' */
29 return [
30 [ false, '¿non sens before!! http://a', 'Allow anything before URI' ],
31
32 # (http|https) - only two schemes allowed
33 [ true, 'http://www.example.org/' ],
34 [ true, 'https://www.example.org/' ],
35 [ true, 'http://www.example.org', 'URI without directory' ],
36 [ true, 'http://a', 'Short name' ],
37 [ true, 'http://étoile', 'Allow UTF-8 in hostname' ], # 'étoile' is french for 'star'
38 [ false, '\\host\directory', 'CIFS share' ],
39 [ false, 'gopher://host/dir', 'Reject gopher scheme' ],
40 [ false, 'telnet://host', 'Reject telnet scheme' ],
41
42 # :\/\/ - double slashes
43 [ false, 'http//example.org', 'Reject missing colon in protocol' ],
44 [ false, 'http:/example.org', 'Reject missing slash in protocol' ],
45 [ false, 'http:example.org', 'Must have two slashes' ],
46 # Following fail since hostname can be made of anything
47 [ false, 'http:///example.org', 'Must have exactly two slashes, not three' ],
48
49 # (\w+:{0,1}\w*@)? - optional user:pass
50 [ true, 'http://user@host', 'Username provided' ],
51 [ true, 'http://user:@host', 'Username provided, no password' ],
52 [ true, 'http://user:pass@host', 'Username and password provided' ],
53
54 # (\S+) - host part is made of anything not whitespaces
55 // commented these out in order to remove @group Broken
56 // @todo are these valid tests? if so, fix Http::isValidURI so it can handle them
57 // [ false, 'http://!"èèè¿¿¿~~\'', 'hostname is made of any non whitespace' ],
58 // [ false, 'http://exam:ple.org/', 'hostname can not use colons!' ],
59
60 # (:[0-9]+)? - port number
61 [ true, 'http://example.org:80/' ],
62 [ true, 'https://example.org:80/' ],
63 [ true, 'http://example.org:443/' ],
64 [ true, 'https://example.org:443/' ],
65
66 # Part after the hostname is / or / with something else
67 [ true, 'http://example/#' ],
68 [ true, 'http://example/!' ],
69 [ true, 'http://example/:' ],
70 [ true, 'http://example/.' ],
71 [ true, 'http://example/?' ],
72 [ true, 'http://example/+' ],
73 [ true, 'http://example/=' ],
74 [ true, 'http://example/&' ],
75 [ true, 'http://example/%' ],
76 [ true, 'http://example/@' ],
77 [ true, 'http://example/-' ],
78 [ true, 'http://example//' ],
79 [ true, 'http://example/&' ],
80
81 # Fragment
82 [ true, 'http://exam#ple.org', ], # This one is valid, really!
83 [ true, 'http://example.org:80#anchor' ],
84 [ true, 'http://example.org/?id#anchor' ],
85 [ true, 'http://example.org/?#anchor' ],
86
87 [ false, 'http://a ¿non !!sens after', 'Allow anything after URI' ],
88 ];
89 }
90
91 }