| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\MainConfigNames; |
| 4 |
use MediaWiki\Request\FauxRequest; |
| 5 |
use MediaWiki\Request\WebRequest; |
| 6 |
use MediaWiki\Session\SessionManager; |
| 7 |
|
| 8 |
class FauxRequestTest extends MediaWikiIntegrationTestCase { |
| 9 |
|
| 10 |
protected function setUp(): void { |
| 11 |
parent::setUp(); |
| 12 |
$this->overrideConfigValue( MainConfigNames::Server, '//wiki.test' ); |
| 13 |
} |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
public function testConstructInvalidSession() { |
| 19 |
$this->expectException( InvalidArgumentException::class ); |
| 20 |
$this->expectExceptionMessage( 'bogus session' ); |
| 21 |
new FauxRequest( [], false, 'x' ); |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public function testConstructWithSession() { |
| 28 |
$session = SessionManager::singleton()->getEmptySession( new FauxRequest( [] ) ); |
| 29 |
$this->assertInstanceOf( |
| 30 |
FauxRequest::class, |
| 31 |
new FauxRequest( [], false, $session ) |
| 32 |
); |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public function testGetText() { |
| 39 |
$req = new FauxRequest( [ 'x' => 'Value' ] ); |
| 40 |
$this->assertSame( 'Value', $req->getText( 'x' ) ); |
| 41 |
$this->assertSame( '', $req->getText( 'z' ) ); |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public function testGetVal() { |
| 49 |
$req = new FauxRequest( [ 'crlf' => "A\r\nb" ] ); |
| 50 |
$this->assertSame( "A\r\nb", $req->getVal( 'crlf' ), 'CRLF' ); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public function testGetRawVal() { |
| 58 |
$req = new FauxRequest( [ |
| 59 |
'x' => 'Value', |
| 60 |
'y' => [ 'a' ], |
| 61 |
'crlf' => "A\r\nb" |
| 62 |
] ); |
| 63 |
$this->assertSame( 'Value', $req->getRawVal( 'x' ) ); |
| 64 |
$this->assertSame( null, $req->getRawVal( 'z' ), 'Not found' ); |
| 65 |
$this->assertSame( null, $req->getRawVal( 'y' ), 'Array is ignored' ); |
| 66 |
$this->assertSame( "A\r\nb", $req->getRawVal( 'crlf' ), 'CRLF' ); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public function testGetValues() { |
| 73 |
$values = [ 'x' => 'Value', 'y' => '' ]; |
| 74 |
$req = new FauxRequest( $values ); |
| 75 |
$this->assertSame( $values, $req->getValues() ); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public function testGetQueryValues() { |
| 82 |
$values = [ 'x' => 'Value', 'y' => '' ]; |
| 83 |
|
| 84 |
$req = new FauxRequest( $values ); |
| 85 |
$this->assertSame( $values, $req->getQueryValues() ); |
| 86 |
$req = new FauxRequest( $values, true ); |
| 87 |
$this->assertSame( [], $req->getQueryValues() ); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public function testGetMethod() { |
| 94 |
$req = new FauxRequest( [] ); |
| 95 |
$this->assertSame( 'GET', $req->getMethod() ); |
| 96 |
$req = new FauxRequest( [], true ); |
| 97 |
$this->assertSame( 'POST', $req->getMethod() ); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public function testWasPosted() { |
| 104 |
$req = new FauxRequest( [] ); |
| 105 |
$this->assertFalse( $req->wasPosted() ); |
| 106 |
$req = new FauxRequest( [], true ); |
| 107 |
$this->assertTrue( $req->wasPosted() ); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public function testCookies() { |
| 116 |
$req = new FauxRequest(); |
| 117 |
$this->assertSame( null, $req->getCookie( 'z', '' ) ); |
| 118 |
|
| 119 |
$req->setCookie( 'x', 'Value', '' ); |
| 120 |
$this->assertSame( 'Value', $req->getCookie( 'x', '' ) ); |
| 121 |
|
| 122 |
$req->setCookies( [ 'x' => 'One', 'y' => 'Two' ], '' ); |
| 123 |
$this->assertSame( 'One', $req->getCookie( 'x', '' ) ); |
| 124 |
$this->assertSame( 'Two', $req->getCookie( 'y', '' ) ); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public function testCookiesDefaultPrefix() { |
| 133 |
global $wgCookiePrefix; |
| 134 |
$oldPrefix = $wgCookiePrefix; |
| 135 |
$wgCookiePrefix = '_'; |
| 136 |
|
| 137 |
$req = new FauxRequest(); |
| 138 |
$this->assertSame( null, $req->getCookie( 'z' ) ); |
| 139 |
|
| 140 |
$req->setCookie( 'x', 'Value' ); |
| 141 |
$this->assertSame( 'Value', $req->getCookie( 'x' ) ); |
| 142 |
|
| 143 |
$wgCookiePrefix = $oldPrefix; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
public function testGetRequestURL_disallowed() { |
| 150 |
$req = new FauxRequest(); |
| 151 |
$this->expectException( MWException::class ); |
| 152 |
$req->getRequestURL(); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
public function testSetRequestURL() { |
| 160 |
$req = new FauxRequest(); |
| 161 |
$req->setRequestURL( 'https://example.org' ); |
| 162 |
$this->assertSame( 'https://example.org', $req->getRequestURL() ); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
public function testGetFullRequestURL_disallowed() { |
| 169 |
$req = new FauxRequest(); |
| 170 |
|
| 171 |
$this->expectException( MWException::class ); |
| 172 |
$req->getFullRequestURL(); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
public function testGetFullRequestURL_http() { |
| 179 |
$req = new FauxRequest(); |
| 180 |
$req->setRequestURL( '/path' ); |
| 181 |
|
| 182 |
$this->assertSame( |
| 183 |
'http://wiki.test/path', |
| 184 |
$req->getFullRequestURL() |
| 185 |
); |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
public function testGetFullRequestURL_https() { |
| 192 |
$req = new FauxRequest( [], false, null, 'https' ); |
| 193 |
$req->setRequestURL( '/path' ); |
| 194 |
|
| 195 |
$this->assertSame( |
| 196 |
'https://wiki.test/path', |
| 197 |
$req->getFullRequestURL() |
| 198 |
); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
public function testProtocol() { |
| 206 |
$req = new FauxRequest(); |
| 207 |
$this->assertSame( 'http', $req->getProtocol() ); |
| 208 |
$req = new FauxRequest( [], false, null, 'http' ); |
| 209 |
$this->assertSame( 'http', $req->getProtocol() ); |
| 210 |
$req = new FauxRequest( [], false, null, 'https' ); |
| 211 |
$this->assertSame( 'https', $req->getProtocol() ); |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
public function testGetSetHeader() { |
| 220 |
$value = 'text/plain, text/html'; |
| 221 |
|
| 222 |
$request = new FauxRequest(); |
| 223 |
$request->setHeader( 'Accept', $value ); |
| 224 |
|
| 225 |
$this->assertSame( false, $request->getHeader( 'Nonexistent' ) ); |
| 226 |
$this->assertSame( $value, $request->getHeader( 'Accept' ) ); |
| 227 |
$this->assertSame( $value, $request->getHeader( 'ACCEPT' ) ); |
| 228 |
$this->assertSame( $value, $request->getHeader( 'accept' ) ); |
| 229 |
$this->assertSame( |
| 230 |
[ 'text/plain', 'text/html' ], |
| 231 |
$request->getHeader( 'Accept', WebRequest::GETHEADER_LIST ) |
| 232 |
); |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
public function testGetAllHeaders() { |
| 239 |
$_SERVER['HTTP_TEST'] = 'Example'; |
| 240 |
|
| 241 |
$request = new FauxRequest(); |
| 242 |
|
| 243 |
$this->assertSame( [], $request->getAllHeaders() ); |
| 244 |
$this->assertSame( false, $request->getHeader( 'test' ) ); |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
public function testSessionData() { |
| 252 |
$values = [ 'x' => 'Value', 'y' => '' ]; |
| 253 |
|
| 254 |
$req = new FauxRequest( [], false, $values ); |
| 255 |
$this->assertSame( $values, $req->getSessionArray() ); |
| 256 |
|
| 257 |
$req = new FauxRequest(); |
| 258 |
$this->assertSame( null, $req->getSessionArray() ); |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
public function testGetPostValues() { |
| 265 |
$values = [ 'x' => 'Value', 'y' => '' ]; |
| 266 |
|
| 267 |
$req = new FauxRequest( $values, true ); |
| 268 |
$this->assertSame( $values, $req->getPostValues() ); |
| 269 |
|
| 270 |
$req = new FauxRequest( $values ); |
| 271 |
$this->assertSame( [], $req->getPostValues() ); |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
public function testDummies() { |
| 280 |
$req = new FauxRequest(); |
| 281 |
$this->assertSame( '', $req->getRawQueryString() ); |
| 282 |
$this->assertSame( '', $req->getRawPostString() ); |
| 283 |
$this->assertSame( '', $req->getRawInput() ); |
| 284 |
} |
| 285 |
} |