|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Spiral Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* @license MIT |
|
7
|
|
|
* @author Anton Titov (Wolfy-J) |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Spiral\Tests\Http\Request; |
|
13
|
|
|
|
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
|
15
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
16
|
|
|
use Spiral\Core\Container; |
|
17
|
|
|
use Spiral\Core\Exception\ScopeException; |
|
18
|
|
|
use Spiral\Http\Exception\InputException; |
|
19
|
|
|
use Spiral\Http\Request\FilesBag; |
|
20
|
|
|
use Spiral\Http\Request\HeadersBag; |
|
21
|
|
|
use Spiral\Http\Request\InputBag; |
|
22
|
|
|
use Spiral\Http\Request\InputManager; |
|
23
|
|
|
use Spiral\Http\Request\ServerBag; |
|
24
|
|
|
use Laminas\Diactoros\ServerRequest; |
|
25
|
|
|
|
|
26
|
|
|
class InputManagerTest extends TestCase |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var Container |
|
30
|
|
|
*/ |
|
31
|
|
|
private $container; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var InputManager |
|
35
|
|
|
*/ |
|
36
|
|
|
private $input; |
|
37
|
|
|
|
|
38
|
|
|
public function setUp(): void |
|
39
|
|
|
{ |
|
40
|
|
|
$this->container = new Container(); |
|
41
|
|
|
$this->input = new InputManager($this->container); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function testCreateOutsideOfScope(): void |
|
45
|
|
|
{ |
|
46
|
|
|
$this->expectException(ScopeException::class); |
|
47
|
|
|
$this->input->request(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function testGetRequest(): void |
|
51
|
|
|
{ |
|
52
|
|
|
$this->container->bind(ServerRequestInterface::class, new ServerRequest()); |
|
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
$this->assertNotNull($this->input->request()); |
|
55
|
|
|
$this->assertSame($this->input->request(), $this->input->request()); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function testChangeRequest(): void |
|
59
|
|
|
{ |
|
60
|
|
|
$this->container->bind(ServerRequestInterface::class, new ServerRequest([], [], '/hello')); |
|
|
|
|
|
|
61
|
|
|
$this->assertSame('/hello', $this->input->path()); |
|
62
|
|
|
|
|
63
|
|
|
$this->container->bind(ServerRequestInterface::class, new ServerRequest([], [], '/other')); |
|
64
|
|
|
$this->assertSame('/other', $this->input->path()); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function testUri(): void |
|
68
|
|
|
{ |
|
69
|
|
|
$request = new ServerRequest([], [], 'http://domain.com/hello-world'); |
|
70
|
|
|
$this->container->bind(ServerRequestInterface::class, $request); |
|
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
$this->assertSame('/hello-world', $this->input->path()); |
|
73
|
|
|
|
|
74
|
|
|
$request = new ServerRequest([], [], 'http://domain.com/new-one'); |
|
75
|
|
|
$this->container->bind(ServerRequestInterface::class, $request); |
|
76
|
|
|
|
|
77
|
|
|
$this->assertSame('/new-one', $this->input->path()); |
|
78
|
|
|
|
|
79
|
|
|
$request = new ServerRequest([], [], ''); |
|
80
|
|
|
$this->container->bind(ServerRequestInterface::class, $request); |
|
81
|
|
|
|
|
82
|
|
|
$this->assertSame('/', $this->input->path()); |
|
83
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
$request = new ServerRequest([], [], 'hello'); |
|
86
|
|
|
$this->container->bind(ServerRequestInterface::class, $request); |
|
87
|
|
|
|
|
88
|
|
|
$this->assertSame('/hello', $this->input->path()); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function testMethod(): void |
|
92
|
|
|
{ |
|
93
|
|
|
$request = new ServerRequest([], [], 'http://domain.com/hello-world', 'GET'); |
|
94
|
|
|
$this->container->bind(ServerRequestInterface::class, $request); |
|
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
$this->assertSame('GET', $this->input->method()); |
|
97
|
|
|
|
|
98
|
|
|
$request = new ServerRequest([], [], 'http://domain.com/hello-world', 'POST'); |
|
99
|
|
|
$this->container->bind(ServerRequestInterface::class, $request); |
|
100
|
|
|
|
|
101
|
|
|
$this->assertSame('POST', $this->input->method()); |
|
102
|
|
|
|
|
103
|
|
|
//case fixing |
|
104
|
|
|
$request = new ServerRequest([], [], 'http://domain.com/hello-world', 'put'); |
|
105
|
|
|
$this->container->bind(ServerRequestInterface::class, $request); |
|
106
|
|
|
|
|
107
|
|
|
$this->assertSame('PUT', $this->input->method()); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function testIsSecure(): void |
|
111
|
|
|
{ |
|
112
|
|
|
$request = new ServerRequest([], [], 'http://domain.com/hello-world', 'GET'); |
|
113
|
|
|
$this->container->bind(ServerRequestInterface::class, $request); |
|
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
$this->assertFalse($this->input->isSecure()); |
|
116
|
|
|
|
|
117
|
|
|
$request = new ServerRequest([], [], 'https://domain.com/hello-world', 'POST'); |
|
118
|
|
|
$this->container->bind(ServerRequestInterface::class, $request); |
|
119
|
|
|
|
|
120
|
|
|
$this->assertTrue($this->input->isSecure()); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function testIsAjax(): void |
|
124
|
|
|
{ |
|
125
|
|
|
$request = new ServerRequest( |
|
126
|
|
|
[], |
|
127
|
|
|
[], |
|
128
|
|
|
'http://domain.com/hello-world', |
|
129
|
|
|
'GET', |
|
130
|
|
|
'php://input', |
|
131
|
|
|
[] |
|
132
|
|
|
); |
|
133
|
|
|
$this->container->bind(ServerRequestInterface::class, $request); |
|
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
$this->assertFalse($this->input->isAjax()); |
|
136
|
|
|
|
|
137
|
|
|
$request = new ServerRequest( |
|
138
|
|
|
[], |
|
139
|
|
|
[], |
|
140
|
|
|
'http://domain.com/hello-world', |
|
141
|
|
|
'GET', |
|
142
|
|
|
'php://input', |
|
143
|
|
|
['X-Requested-With' => 'xmlhttprequest'] |
|
144
|
|
|
); |
|
145
|
|
|
$this->container->bind(ServerRequestInterface::class, $request); |
|
146
|
|
|
|
|
147
|
|
|
$this->assertTrue($this->input->isAjax()); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @dataProvider isJsonExpectedProvider |
|
152
|
|
|
* @param bool $expected |
|
153
|
|
|
* @param string|null $acceptHeader |
|
154
|
|
|
*/ |
|
155
|
|
|
public function testIsJsonExpected(bool $expected, ?string $acceptHeader): void |
|
156
|
|
|
{ |
|
157
|
|
|
$input = $this->input->withJsonType('application/vnd.api+json'); |
|
158
|
|
|
|
|
159
|
|
|
$request = new ServerRequest( |
|
160
|
|
|
[], |
|
161
|
|
|
[], |
|
162
|
|
|
'http://domain.com/hello-world', |
|
163
|
|
|
'GET', |
|
164
|
|
|
'php://input', |
|
165
|
|
|
$acceptHeader !== null ? ['Accept' => $acceptHeader] : [] |
|
166
|
|
|
); |
|
167
|
|
|
$this->container->bind(ServerRequestInterface::class, $request); |
|
|
|
|
|
|
168
|
|
|
|
|
169
|
|
|
$this->assertSame($expected, $input->isJsonExpected()); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @return iterable |
|
174
|
|
|
*/ |
|
175
|
|
|
public function isJsonExpectedProvider(): iterable |
|
176
|
|
|
{ |
|
177
|
|
|
return [ |
|
178
|
|
|
[false, null], |
|
179
|
|
|
[false, 'text/html'], |
|
180
|
|
|
[true, 'application/json'], |
|
181
|
|
|
[true, 'application/vnd.api+json'], |
|
182
|
|
|
]; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* @dataProvider isJsonExpectedOnSoftMatchProvider |
|
187
|
|
|
* @param bool $expected |
|
188
|
|
|
* @param string|null $acceptHeader |
|
189
|
|
|
*/ |
|
190
|
|
|
public function testIsJsonExpectedOnSoftMatch(bool $expected, ?string $acceptHeader): void |
|
191
|
|
|
{ |
|
192
|
|
|
$request = new ServerRequest( |
|
193
|
|
|
[], |
|
194
|
|
|
[], |
|
195
|
|
|
'http://domain.com/hello-world', |
|
196
|
|
|
'GET', |
|
197
|
|
|
'php://input', |
|
198
|
|
|
$acceptHeader !== null ? ['Accept' => $acceptHeader] : [] |
|
199
|
|
|
); |
|
200
|
|
|
$this->container->bind(ServerRequestInterface::class, $request); |
|
|
|
|
|
|
201
|
|
|
|
|
202
|
|
|
$this->assertFalse($this->input->isJsonExpected()); |
|
203
|
|
|
$this->assertSame($expected, $this->input->isJsonExpected(true)); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* @return iterable |
|
208
|
|
|
*/ |
|
209
|
|
|
public function isJsonExpectedOnSoftMatchProvider(): iterable |
|
210
|
|
|
{ |
|
211
|
|
|
return [ |
|
212
|
|
|
[false, null], |
|
213
|
|
|
[false, 'text/html'], |
|
214
|
|
|
[true, 'text/json'], |
|
215
|
|
|
[true, 'application/vnd.api+json'], |
|
216
|
|
|
]; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
public function testRemoteIP(): void |
|
220
|
|
|
{ |
|
221
|
|
|
$request = new ServerRequest( |
|
222
|
|
|
['REMOTE_ADDR' => '127.0.0.1'], |
|
223
|
|
|
[], |
|
224
|
|
|
'http://domain.com/hello-world', |
|
225
|
|
|
'GET', |
|
226
|
|
|
'php://input', |
|
227
|
|
|
[] |
|
228
|
|
|
); |
|
229
|
|
|
$this->container->bind(ServerRequestInterface::class, $request); |
|
|
|
|
|
|
230
|
|
|
|
|
231
|
|
|
$this->assertSame('127.0.0.1', $this->input->remoteAddress()); |
|
232
|
|
|
|
|
233
|
|
|
$request = new ServerRequest( |
|
234
|
|
|
['REMOTE_ADDR' => null], |
|
235
|
|
|
[], |
|
236
|
|
|
'http://domain.com/hello-world', |
|
237
|
|
|
'GET', |
|
238
|
|
|
'php://input', |
|
239
|
|
|
['Accept' => 'application/json'] |
|
240
|
|
|
); |
|
241
|
|
|
$this->container->bind(ServerRequestInterface::class, $request); |
|
242
|
|
|
|
|
243
|
|
|
$this->assertTrue($this->input->isJsonExpected()); |
|
244
|
|
|
|
|
245
|
|
|
$this->assertNull($this->input->remoteAddress()); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
public function testGetBag(): void |
|
249
|
|
|
{ |
|
250
|
|
|
$request = new ServerRequest( |
|
251
|
|
|
[], |
|
252
|
|
|
[], |
|
253
|
|
|
'http://domain.com/hello-world', |
|
254
|
|
|
'GET', |
|
255
|
|
|
'php://input', |
|
256
|
|
|
[] |
|
257
|
|
|
); |
|
258
|
|
|
$this->container->bind(ServerRequestInterface::class, $request); |
|
|
|
|
|
|
259
|
|
|
|
|
260
|
|
|
$this->assertInstanceOf(ServerBag::class, $this->input->server); |
|
261
|
|
|
$this->assertInstanceOf(InputBag::class, $this->input->attributes); |
|
262
|
|
|
$this->assertInstanceOf(InputBag::class, $this->input->data); |
|
263
|
|
|
$this->assertInstanceOf(InputBag::class, $this->input->cookies); |
|
264
|
|
|
$this->assertInstanceOf(InputBag::class, $this->input->query); |
|
265
|
|
|
$this->assertInstanceOf(FilesBag::class, $this->input->files); |
|
266
|
|
|
$this->assertInstanceOf(HeadersBag::class, $this->input->headers); |
|
267
|
|
|
|
|
268
|
|
|
$this->assertInstanceOf(ServerBag::class, $this->input->server); |
|
269
|
|
|
$this->assertInstanceOf(InputBag::class, $this->input->attributes); |
|
270
|
|
|
$this->assertInstanceOf(InputBag::class, $this->input->data); |
|
271
|
|
|
$this->assertInstanceOf(InputBag::class, $this->input->cookies); |
|
272
|
|
|
$this->assertInstanceOf(InputBag::class, $this->input->query); |
|
273
|
|
|
$this->assertInstanceOf(FilesBag::class, $this->input->files); |
|
274
|
|
|
$this->assertInstanceOf(HeadersBag::class, $this->input->headers); |
|
275
|
|
|
|
|
276
|
|
|
$input = clone $this->input; |
|
277
|
|
|
$this->assertInstanceOf(ServerBag::class, $input->server); |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
public function testWrongBad(): void |
|
281
|
|
|
{ |
|
282
|
|
|
$this->expectException(InputException::class); |
|
283
|
|
|
$request = new ServerRequest( |
|
284
|
|
|
[], |
|
285
|
|
|
[], |
|
286
|
|
|
'http://domain.com/hello-world', |
|
287
|
|
|
'GET', |
|
288
|
|
|
'php://input', |
|
289
|
|
|
[] |
|
290
|
|
|
); |
|
291
|
|
|
|
|
292
|
|
|
$this->container->bind(ServerRequestInterface::class, $request); |
|
|
|
|
|
|
293
|
|
|
$this->input->invalid; |
|
|
|
|
|
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
public function testShortcuts(): void |
|
297
|
|
|
{ |
|
298
|
|
|
$this->container->bind(ServerRequestInterface::class, (new ServerRequest())->withParsedBody([ |
|
|
|
|
|
|
299
|
|
|
'array' => [ |
|
300
|
|
|
'key' => [ |
|
301
|
|
|
'name' => 'value' |
|
302
|
|
|
] |
|
303
|
|
|
], |
|
304
|
|
|
'name' => 'xx' |
|
305
|
|
|
])->withQueryParams([ |
|
306
|
|
|
'name' => 'value', |
|
307
|
|
|
'key' => ['name' => 'hi'] |
|
308
|
|
|
])->withAttribute('attr', 'value')->withCookieParams([ |
|
309
|
|
|
'cookie' => 'cookie-value' |
|
310
|
|
|
])); |
|
311
|
|
|
|
|
312
|
|
|
$this->assertSame('value', $this->input->data('array.key.name')); |
|
313
|
|
|
$this->assertSame('value', $this->input->post('array.key.name')); |
|
314
|
|
|
|
|
315
|
|
|
$this->assertSame('value', $this->input->query('name')); |
|
316
|
|
|
$this->assertSame('hi', $this->input->query('key.name')); |
|
317
|
|
|
|
|
318
|
|
|
$this->assertSame('xx', $this->input->input('name')); |
|
319
|
|
|
$this->assertSame('hi', $this->input->input('key.name')); |
|
320
|
|
|
$this->assertSame('value', $this->input->attribute('attr')); |
|
321
|
|
|
|
|
322
|
|
|
$this->assertSame('cookie-value', $this->input->cookie('cookie')); |
|
323
|
|
|
} |
|
324
|
|
|
} |
|
325
|
|
|
|