forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResponseSendTest.php
More file actions
232 lines (195 loc) · 6.96 KB
/
Copy pathResponseSendTest.php
File metadata and controls
232 lines (195 loc) · 6.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\HTTP;
use CodeIgniter\Security\Exceptions\SecurityException;
use CodeIgniter\Test\CIUnitTestCase;
use Config\App;
use Config\Services;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\PreserveGlobalState;
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
use PHPUnit\Framework\Attributes\WithoutErrorHandler;
/**
* This test suite has been created separately from
* TestCaseTest because it messes with output
* buffering from PHPUnit, and the individual
* test cases need to be run as separate processes.
*
* @internal
*/
#[Group('SeparateProcess')]
final class ResponseSendTest extends CIUnitTestCase
{
/**
* These need to be run as a separate process, since phpunit
* has already captured the "normal" output, and we will get
* a "Cannot modify headers" message if we try to change
* headers or cookies now.
*
* Furthermore, these tests needs to flush the output buffering
* that might be in progress, and start our own output buffer
* capture.
*
* The tests includes a basic sanity check, to make sure that
* the body we thought would be sent actually was.
*/
#[PreserveGlobalState(false)]
#[RunInSeparateProcess]
#[WithoutErrorHandler]
public function testHeadersMissingDate(): void
{
$response = new Response(new App());
$response->pretend(false);
$body = 'Hello';
$response->setBody($body);
$response->setCookie('foo', 'bar');
$this->assertTrue($response->hasCookie('foo'));
$this->assertTrue($response->hasCookie('foo', 'bar'));
// Drop the date header, to make sure it gets put back in
$response->removeHeader('Date');
// send it
ob_start();
$response->send();
if (ob_get_level() > 0) {
ob_end_clean();
}
// and what actually got sent?
$this->assertHeaderEmitted('Date:');
}
/**
* This test does not test that CSP is handled properly -
* it makes sure that sending gives CSP a chance to do its thing.
*/
#[PreserveGlobalState(false)]
#[RunInSeparateProcess]
#[WithoutErrorHandler]
public function testHeadersWithCSP(): void
{
$this->resetFactories();
$this->resetServices();
$config = config('App');
$config->CSPEnabled = true;
$response = new Response($config);
$response->pretend(false);
$body = 'Hello';
$response->setBody($body);
$response->setCookie('foo', 'bar');
$this->assertTrue($response->hasCookie('foo'));
$this->assertTrue($response->hasCookie('foo', 'bar'));
// send it
ob_start();
$response->send();
if (ob_get_level() > 0) {
ob_end_clean();
}
// and what actually got sent?; test both ways
$this->assertHeaderEmitted('Content-Security-Policy:');
}
/**
* Make sure cookies are set by RedirectResponse this way
*
* @see https://github.com/codeigniter4/CodeIgniter4/issues/1393
*/
#[PreserveGlobalState(false)]
#[RunInSeparateProcess]
#[WithoutErrorHandler]
public function testRedirectResponseCookies(): void
{
$loginTime = time();
$response = new Response(new App());
$response->pretend(false);
$routes = service('routes');
$routes->add('user/login', 'Auth::verify', ['as' => 'login']);
$answer1 = $response->redirect('/login')
->setCookie('foo', 'bar', YEAR)
->setCookie('login_time', (string) $loginTime, YEAR);
$this->assertTrue($answer1->hasCookie('foo', 'bar'));
$this->assertTrue($answer1->hasCookie('login_time'));
$response->setBody('Hello');
// send it
ob_start();
$response->send();
if (ob_get_level() > 0) {
ob_end_clean();
}
// and what actually got sent?
$this->assertHeaderEmitted('Set-Cookie: foo=bar;');
$this->assertHeaderEmitted('Set-Cookie: login_time');
}
/**
* Make sure secure cookies are not sent with HTTP request
*/
public function testDoNotSendUnSecureCookie(): void
{
$this->expectException(SecurityException::class);
$this->expectExceptionMessage('Attempted to send a secure cookie over a non-secure connection.');
$request = $this->createMock(IncomingRequest::class);
$request->method('isSecure')->willReturn(false);
Services::injectMock('request', $request);
$response = new Response(new App());
$response->pretend(false);
$body = 'Hello';
$response->setBody($body);
$response->setCookie(
'foo',
'bar',
'',
'',
'/',
'',
true,
);
// send it
$response->send();
}
/**
* Make sure that the headers set by the header() function
* are overridden by the headers defined in the Response class.
*/
#[PreserveGlobalState(false)]
#[RunInSeparateProcess]
#[WithoutErrorHandler]
public function testHeaderOverride(): void
{
$response = new Response(new App());
$response->pretend(false);
$body = 'Hello';
$response->setBody($body);
// single header
$response->setHeader('Vary', 'Accept-Encoding');
$this->assertSame('Accept-Encoding', $response->header('Vary')->getValue());
// multiple headers
$response->setHeader('Access-Control-Expose-Headers', 'X-Custom-Header');
$response->addHeader('Access-Control-Expose-Headers', 'Content-Length');
$header = $response->header('Access-Control-Expose-Headers');
$this->assertIsArray($header);
$this->assertSame('X-Custom-Header', $header[0]->getValue());
$this->assertSame('Content-Length', $header[1]->getValue());
// send it
ob_start();
header('Vary: User-Agent');
header('Access-Control-Expose-Headers: Content-Encoding');
header('Allow: GET, POST');
$response->send();
if (ob_get_level() > 0) {
ob_end_clean();
}
// single header
$this->assertHeaderEmitted('Vary: Accept-Encoding');
$this->assertHeaderNotEmitted('Vary: User-Agent');
// multiple headers
$this->assertHeaderEmitted('Access-Control-Expose-Headers: X-Custom-Header');
$this->assertHeaderEmitted('Access-Control-Expose-Headers: Content-Length');
$this->assertHeaderNotEmitted('Access-Control-Expose-Headers: Content-Encoding');
// not overridden by the response class
$this->assertHeaderEmitted('Allow: GET, POST');
}
}