| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
use MediaWiki\Config\HashConfig; |
| 24 |
use MediaWiki\MainConfigNames; |
| 25 |
use MediaWiki\Request\FauxResponse; |
| 26 |
|
| 27 |
class FauxResponseTest extends \MediaWikiUnitTestCase { |
| 28 |
|
| 29 |
protected $response; |
| 30 |
|
| 31 |
protected function setUp(): void { |
| 32 |
parent::setUp(); |
| 33 |
$this->response = new FauxResponse; |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public function testCookie() { |
| 43 |
$expire = time() + 100; |
| 44 |
$cookie = [ |
| 45 |
'value' => 'val', |
| 46 |
'path' => '/path', |
| 47 |
'domain' => 'domain', |
| 48 |
'secure' => true, |
| 49 |
'httpOnly' => false, |
| 50 |
'raw' => false, |
| 51 |
'expire' => $expire, |
| 52 |
]; |
| 53 |
|
| 54 |
$this->response->setCookieConfig( new HashConfig( [ |
| 55 |
MainConfigNames::CookiePath => '/', |
| 56 |
MainConfigNames::CookiePrefix => false, |
| 57 |
MainConfigNames::CookieDomain => '', |
| 58 |
MainConfigNames::CookieSecure => 'detect', |
| 59 |
MainConfigNames::CookieExpiration => '60', |
| 60 |
MainConfigNames::CookieHttpOnly => true, |
| 61 |
] ) ); |
| 62 |
|
| 63 |
$this->assertNull( $this->response->getCookie( 'xkey' ), 'Non-existing cookie' ); |
| 64 |
$this->response->setCookie( 'key', 'val', $expire, [ |
| 65 |
'prefix' => 'x', |
| 66 |
'path' => '/path', |
| 67 |
'domain' => 'domain', |
| 68 |
'secure' => 1, |
| 69 |
'httpOnly' => 0, |
| 70 |
] ); |
| 71 |
$this->assertEquals( 'val', $this->response->getCookie( 'xkey' ), 'Existing cookie' ); |
| 72 |
$this->assertEquals( $cookie, $this->response->getCookieData( 'xkey' ), |
| 73 |
'Existing cookie (data)' ); |
| 74 |
$this->assertEquals( [ 'xkey' => $cookie ], $this->response->getCookies(), |
| 75 |
'Existing cookies' ); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public function testHeader() { |
| 83 |
$this->assertNull( $this->response->getHeader( 'Location' ), 'Non-existing header' ); |
| 84 |
|
| 85 |
$this->response->header( 'Location: http://localhost/' ); |
| 86 |
$this->assertEquals( |
| 87 |
'http://localhost/', |
| 88 |
$this->response->getHeader( 'Location' ), |
| 89 |
'Set header' |
| 90 |
); |
| 91 |
|
| 92 |
$this->response->header( 'Location: http://127.0.0.1/' ); |
| 93 |
$this->assertEquals( |
| 94 |
'http://127.0.0.1/', |
| 95 |
$this->response->getHeader( 'Location' ), |
| 96 |
'Same header' |
| 97 |
); |
| 98 |
|
| 99 |
$this->response->header( 'Location: http://127.0.0.2/', false ); |
| 100 |
$this->assertEquals( |
| 101 |
'http://127.0.0.1/', |
| 102 |
$this->response->getHeader( 'Location' ), |
| 103 |
'Same header with override disabled' |
| 104 |
); |
| 105 |
|
| 106 |
$this->response->header( 'Location: http://localhost/' ); |
| 107 |
$this->assertEquals( |
| 108 |
'http://localhost/', |
| 109 |
$this->response->getHeader( 'LOCATION' ), |
| 110 |
'Get header case insensitive' |
| 111 |
); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public function testResponseCode() { |
| 118 |
$this->response->header( 'HTTP/1.1 200' ); |
| 119 |
$this->assertEquals( 200, $this->response->getStatusCode(), 'Header with no message' ); |
| 120 |
|
| 121 |
$this->response->header( 'HTTP/1.x 201' ); |
| 122 |
$this->assertEquals( |
| 123 |
201, |
| 124 |
$this->response->getStatusCode(), |
| 125 |
'Header with no message and protocol 1.x' |
| 126 |
); |
| 127 |
|
| 128 |
$this->response->header( 'HTTP/1.1 202 OK' ); |
| 129 |
$this->assertEquals( 202, $this->response->getStatusCode(), 'Normal header' ); |
| 130 |
|
| 131 |
$this->response->header( 'HTTP/1.x 203 OK' ); |
| 132 |
$this->assertEquals( |
| 133 |
203, |
| 134 |
$this->response->getStatusCode(), |
| 135 |
'Normal header with no message and protocol 1.x' |
| 136 |
); |
| 137 |
|
| 138 |
$this->response->header( 'HTTP/1.x 204 OK', false, 205 ); |
| 139 |
$this->assertEquals( |
| 140 |
205, |
| 141 |
$this->response->getStatusCode(), |
| 142 |
'Third parameter overrides the HTTP/... header' |
| 143 |
); |
| 144 |
|
| 145 |
$this->response->statusHeader( 210 ); |
| 146 |
$this->assertEquals( |
| 147 |
210, |
| 148 |
$this->response->getStatusCode(), |
| 149 |
'Handle statusHeader method' |
| 150 |
); |
| 151 |
|
| 152 |
$this->response->header( 'Location: http://localhost/', false, 206 ); |
| 153 |
$this->assertEquals( |
| 154 |
206, |
| 155 |
$this->response->getStatusCode(), |
| 156 |
'Third parameter with another header' |
| 157 |
); |
| 158 |
} |
| 159 |
} |