Debian Codegraph @

tests/phpunit/includes/http/MWHttpRequestTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\MediaWikiServices;
4 use Wikimedia\Http\TelemetryHeadersInterface;
5 use Wikimedia\TestingAccessWrapper;
6
7 /**
8 * @covers \MWHttpRequest
9 */
10 class MWHttpRequestTest extends PHPUnit\Framework\TestCase {
11
12 /**
13 * Feeds URI to test a long regular expression in MWHttpRequest::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 MWHttpRequest::isValidURI so it can handle them
45 // [ false, 'http://!"èèè¿¿¿~~\'', 'hostname is made of any non whitespace' ],
46 // [ false, 'http://exam:ple.org/', 'hostname cannot 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 * T29854 : MWHttpRequest::isValidURI is too lax
81 * @dataProvider provideURI
82 * @covers \MWHttpRequest::isValidURI
83 */
84 public function testIsValidUri( $expect, $uri, $message = '' ) {
85 $this->assertSame( $expect, MWHttpRequest::isValidURI( $uri ), $message );
86 }
87
88 public function testSetReverseProxy() {
89 $req = TestingAccessWrapper::newFromObject(
90 MediaWikiServices::getInstance()->getHttpRequestFactory()->create( 'https://example.org/path?query=string' )
91 );
92 $req->setReverseProxy( 'http://localhost:1234' );
93 $this->assertSame( 'http://localhost:1234/path?query=string', $req->url );
94 $this->assertSame( 'example.org', $req->reqHeaders['Host'] );
95 }
96
97 public function testItInjectsTelemetryHeaders() {
98 $telemetry = $this->createMock( TelemetryHeadersInterface::class );
99 $telemetry->expects( $this->once() )
100 ->method( 'getRequestHeaders' )
101 ->willReturn( [
102 'X-Request-Id' => 'request_identifier',
103 'tracestate' => 'tracestate_value',
104 'traceparent' => 'traceparent_value',
105 ] );
106
107 $httpRequest = $this->getMockForAbstractClass(
108 MWHttpRequest::class,
109 [
110 'http://localhost/test',
111 [
112 'timeout' => 30,
113 'connectTimeout' => 30
114 ]
115 ]
116 );
117 $httpRequest->addTelemetry( $telemetry );
118
119 $accessWrapper = TestingAccessWrapper::newFromObject( $httpRequest );
120 $requestHeaders = $accessWrapper->reqHeaders;
121
122 $this->assertEquals( 'request_identifier', $requestHeaders['X-Request-Id'] );
123 $this->assertEquals( 'tracestate_value', $requestHeaders['tracestate'] );
124 $this->assertEquals( 'traceparent_value', $requestHeaders['traceparent'] );
125 }
126 }