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