| 1 |
<?php |
| 2 |
|
| 3 |
use GuzzleHttp\Psr7\UploadedFile; |
| 4 |
use MediaWiki\Rest\RequestFromGlobals; |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
class RequestFromGlobalsTest extends MediaWikiIntegrationTestCase { |
| 12 |
|
| 13 |
private RequestFromGlobals $reqFromGlobals; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
private $oldServer; |
| 19 |
|
| 20 |
protected function setUp(): void { |
| 21 |
parent::setUp(); |
| 22 |
$this->reqFromGlobals = new RequestFromGlobals(); |
| 23 |
|
| 24 |
$this->oldServer = $_SERVER; |
| 25 |
} |
| 26 |
|
| 27 |
protected function tearDown(): void { |
| 28 |
$_SERVER = $this->oldServer; |
| 29 |
parent::tearDown(); |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public function testGetMethod( $serverVars, $expected ) { |
| 36 |
$this->setServerVars( $serverVars ); |
| 37 |
$this->assertEquals( $expected, $this->reqFromGlobals->getMethod() ); |
| 38 |
} |
| 39 |
|
| 40 |
public static function provideGetMethod() { |
| 41 |
return [ |
| 42 |
[ |
| 43 |
[ |
| 44 |
'REQUEST_METHOD' => 'POST' |
| 45 |
], |
| 46 |
'POST', |
| 47 |
], |
| 48 |
[ |
| 49 |
[], |
| 50 |
'GET', |
| 51 |
], |
| 52 |
[ |
| 53 |
[ 'REQUEST_METHOD' => 'get' ], |
| 54 |
'GET', |
| 55 |
], |
| 56 |
]; |
| 57 |
} |
| 58 |
|
| 59 |
public function testGetUri() { |
| 60 |
$this->setServerVars( [ |
| 61 |
'REQUEST_URI' => '/test.php' |
| 62 |
] ); |
| 63 |
|
| 64 |
$this->assertEquals( '/test.php', $this->reqFromGlobals->getUri() ); |
| 65 |
} |
| 66 |
|
| 67 |
public function testGetUri2() { |
| 68 |
$this->setServerVars( [ |
| 69 |
'REQUEST_URI' => '/test.php/page/1:1', |
| 70 |
] ); |
| 71 |
|
| 72 |
$this->assertEquals( '/test.php/page/1:1', $this->reqFromGlobals->getUri() ); |
| 73 |
} |
| 74 |
|
| 75 |
public function testGetUri3() { |
| 76 |
$this->setServerVars( [ |
| 77 |
'REQUEST_URI' => '/w/rest.php/sandbox.semantic-mediawiki.org:8142/v3/page/html/Berlin', |
| 78 |
] ); |
| 79 |
|
| 80 |
$this->assertEquals( |
| 81 |
'/w/rest.php/sandbox.semantic-mediawiki.org:8142/v3/page/html/Berlin', |
| 82 |
$this->reqFromGlobals->getUri() |
| 83 |
); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public function testGetProtocolVersion( $serverVars, $expected ) { |
| 90 |
$this->setServerVars( $serverVars ); |
| 91 |
$this->assertEquals( $expected, $this->reqFromGlobals->getProtocolVersion() ); |
| 92 |
} |
| 93 |
|
| 94 |
public static function provideGetProtocolVersion() { |
| 95 |
return [ |
| 96 |
[ |
| 97 |
[ |
| 98 |
'SERVER_PROTOCOL' => 'HTTP/2' |
| 99 |
], |
| 100 |
2 |
| 101 |
], |
| 102 |
[ |
| 103 |
[], |
| 104 |
1.1 |
| 105 |
] |
| 106 |
]; |
| 107 |
} |
| 108 |
|
| 109 |
public function testGetHeaders() { |
| 110 |
$this->setServerVars( [ |
| 111 |
'HTTP_HOST' => '[::1]', |
| 112 |
'CONTENT_LENGTH' => 6, |
| 113 |
'CONTENT_TYPE' => 'application/json', |
| 114 |
'CONTENT_MD5' => 'rL0Y20zC+Fzt72VPzMSk2A==', |
| 115 |
] ); |
| 116 |
|
| 117 |
$this->assertEquals( [ |
| 118 |
'Host' => [ '[::1]' ], |
| 119 |
'Content-Length' => [ 6 ], |
| 120 |
'Content-Type' => [ 'application/json' ], |
| 121 |
'Content-Md5' => [ 'rL0Y20zC+Fzt72VPzMSk2A==' ], |
| 122 |
], $this->reqFromGlobals->getHeaders() ); |
| 123 |
} |
| 124 |
|
| 125 |
public function testGetHeaderKeyIsCaseInsensitive() { |
| 126 |
$cacheControl = 'private, must-revalidate, max-age=0'; |
| 127 |
$this->setServerVars( [ 'HTTP_CACHE_CONTROL' => $cacheControl ] ); |
| 128 |
|
| 129 |
$this->assertSame( [ $cacheControl ], $this->reqFromGlobals->getHeader( 'Cache-Control' ) ); |
| 130 |
$this->assertSame( [ $cacheControl ], $this->reqFromGlobals->getHeader( 'cache-control' ) ); |
| 131 |
} |
| 132 |
|
| 133 |
public function testGetBody() { |
| 134 |
$this->setServerVars( [ |
| 135 |
'REQUEST_METHOD' => 'POST', |
| 136 |
'HTTP_ACCEPT' => 'text/html' |
| 137 |
] ); |
| 138 |
|
| 139 |
$this->assertSame( '', $this->reqFromGlobals->getBody()->getContents() ); |
| 140 |
} |
| 141 |
|
| 142 |
public function testGetServerParams() { |
| 143 |
$serverVars = [ |
| 144 |
'SERVER_NAME' => 'www.mediawiki.org', |
| 145 |
'SERVER_PROTOCOL' => 'HTTP/1.1', |
| 146 |
'REQUEST_METHOD' => 'POST', |
| 147 |
'HTTP_HOST' => 'www.mediawiki.org', |
| 148 |
'HTTP_ACCEPT' => 'text/html', |
| 149 |
'REMOTE_PORT' => '1234', |
| 150 |
'SCRIPT_NAME' => '/index.php', |
| 151 |
]; |
| 152 |
$this->setServerVars( $serverVars ); |
| 153 |
|
| 154 |
$expectedServerParams = $this->reqFromGlobals->getServerParams(); |
| 155 |
|
| 156 |
$diffs = array_diff_assoc( $expectedServerParams, $serverVars ); |
| 157 |
|
| 158 |
$this->assertCount( 2, $diffs ); |
| 159 |
$this->assertArrayHasKey( 'REQUEST_TIME_FLOAT', $diffs ); |
| 160 |
$this->assertArrayHasKey( 'REQUEST_TIME', $diffs ); |
| 161 |
} |
| 162 |
|
| 163 |
public function testGetCookieParams() { |
| 164 |
$_COOKIE = [ |
| 165 |
'testcookie' => true |
| 166 |
]; |
| 167 |
|
| 168 |
$this->assertEquals( [ 'testcookie' => true ], $this->reqFromGlobals->getCookieParams() ); |
| 169 |
} |
| 170 |
|
| 171 |
public function testGetQueryParams() { |
| 172 |
$query = [ |
| 173 |
[ |
| 174 |
'title' => 'foo', |
| 175 |
'action' => 'query' |
| 176 |
] |
| 177 |
]; |
| 178 |
$_GET = $query; |
| 179 |
|
| 180 |
$this->assertEquals( $query, $this->reqFromGlobals->getQueryParams() ); |
| 181 |
} |
| 182 |
|
| 183 |
public function testGetUploadedFiles() { |
| 184 |
$_FILES = [ |
| 185 |
'file' => [ |
| 186 |
'name' => 'Foo.txt', |
| 187 |
'type' => 'text/plain', |
| 188 |
'tmp_name' => '/tmp/foobar', |
| 189 |
'error' => UPLOAD_ERR_OK, |
| 190 |
'size' => 20, |
| 191 |
] |
| 192 |
]; |
| 193 |
|
| 194 |
$this->assertEquals( |
| 195 |
[ |
| 196 |
'file' => new UploadedFile( |
| 197 |
'/tmp/foobar', |
| 198 |
20, UPLOAD_ERR_OK, |
| 199 |
'Foo.txt', |
| 200 |
'text/plain' |
| 201 |
) |
| 202 |
], |
| 203 |
$this->reqFromGlobals->getUploadedFiles() |
| 204 |
); |
| 205 |
} |
| 206 |
|
| 207 |
public function testGetPostParams() { |
| 208 |
$form = [ |
| 209 |
'token' => '983yh4edji', |
| 210 |
'action' => 'login' |
| 211 |
]; |
| 212 |
$_POST = $form; |
| 213 |
|
| 214 |
$this->assertEquals( $form, $this->reqFromGlobals->getPostParams() ); |
| 215 |
} |
| 216 |
|
| 217 |
protected function setServerVars( $vars ) { |
| 218 |
|
| 219 |
if ( !isset( $vars['REQUEST_TIME_FLOAT'] ) ) { |
| 220 |
$vars['REQUEST_TIME_FLOAT'] = $_SERVER['REQUEST_TIME_FLOAT']; |
| 221 |
} |
| 222 |
if ( !isset( $vars['REQUEST_TIME'] ) ) { |
| 223 |
$vars['REQUEST_TIME'] = $_SERVER['REQUEST_TIME']; |
| 224 |
} |
| 225 |
$_SERVER = $vars; |
| 226 |
} |
| 227 |
} |