Debian Codegraph @

tests/phpunit/unit/includes/Request/FauxResponseTest.php

raw symbols SCIP
1 <?php
2 /**
3 * Copyright @ 2011 Alexandre Emsenhuber
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 use MediaWiki\Config\HashConfig;
24 use MediaWiki\MainConfigNames;
25 use MediaWiki\Request\FauxResponse;
26
27 class FauxResponseTest extends \MediaWikiUnitTestCase {
28 /** @var FauxResponse */
29 protected $response;
30
31 protected function setUp(): void {
32 parent::setUp();
33 $this->response = new FauxResponse;
34 }
35
36 /**
37 * @covers \MediaWiki\Request\FauxResponse::setCookie
38 * @covers \MediaWiki\Request\FauxResponse::getCookie
39 * @covers \MediaWiki\Request\FauxResponse::getCookieData
40 * @covers \MediaWiki\Request\FauxResponse::getCookies
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 * @covers \MediaWiki\Request\FauxResponse::getheader
80 * @covers \MediaWiki\Request\FauxResponse::header
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 * @covers \MediaWiki\Request\FauxResponse::getStatusCode
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 }