-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerRequestTest.php
More file actions
370 lines (336 loc) · 11.8 KB
/
Copy pathServerRequestTest.php
File metadata and controls
370 lines (336 loc) · 11.8 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<?php
namespace bdk\Test\HttpMessage;
use bdk\HttpMessage\Message;
use bdk\HttpMessage\Request;
use bdk\HttpMessage\ServerRequest;
use bdk\HttpMessage\Utility\ParseStr;
use ReflectionObject;
use RuntimeException;
/**
* @covers \bdk\HttpMessage\AssertionTrait
* @covers \bdk\HttpMessage\ServerRequest
* @covers \bdk\HttpMessage\Utility\ParseStr
*/
class ServerRequestTest extends TestCase
{
public function testConstruct()
{
$serverRequest = $this->createServerRequest();
$this->assertTrue($serverRequest instanceof Message);
$this->assertTrue($serverRequest instanceof Request);
$this->assertTrue($serverRequest instanceof ServerRequest);
}
public function testAuthHeaders()
{
$serverRequest = $this->createServerRequest('GET', 'http://www.test.com/', array(
'REDIRECT_HTTP_AUTHORIZATION' => 'Basic ' . \base64_encode('username:password'),
));
$this->assertSame(array(
'Host' => array('www.test.com'),
'Authorization' => array('Basic ' . \base64_encode('username:password')),
), $serverRequest->getHeaders());
$digestVal = 'Digest username="Mufasa", realm="testrealm@host.com", nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093", uri="/dir/index.html", qop=auth, nc=00000001, cnonce="0a4f113b", response="6629fae49393a05397450978507c4ef1", opaque="5ccc069c403ebaf9f0171e9517f40e41';
$serverRequest = $this->createServerRequest('GET', 'http://www.test.com/', array(
'PHP_AUTH_DIGEST' => $digestVal,
));
$this->assertSame(array(
'Host' => array('www.test.com'),
'Authorization' => array($digestVal),
), $serverRequest->getHeaders());
}
public function testConstructWithUri()
{
$serverRequest = $this->createServerRequest(
'GET',
'/some/page?foo=bar&dingle.berry=brown&a%20b=c&d+e=f&g h=i',
array(
'SERVER_PROTOCOL' => 'HTTP/1.0',
'CONTENT_TYPE' => 'text/html',
)
);
$this->assertSame(array(
'foo' => 'bar',
'dingle.berry' => 'brown',
'a b' => 'c',
'd e' => 'f',
'g h' => 'i',
), $serverRequest->getQueryParams());
$this->assertSame('1.0', $serverRequest->getProtocolVersion());
$this->assertSame('text/html', $serverRequest->getHeaderLine('Content-Type'));
// test options parsing works on constructor
ParseStr::setOpts('convSpace', true);
$serverRequest = $this->createServerRequest(
'GET',
'/some/page?foo=bar&dingle.berry=brown&a%20b=c&d+e=f&g h=i'
);
$this->assertSame(array(
'foo' => 'bar',
'dingle.berry' => 'brown',
'a_b' => 'c',
'd_e' => 'f',
'g_h' => 'i',
), $serverRequest->getQueryParams());
ParseStr::setOpts('convSpace', false);
/*
Test new values replace
*/
$serverRequest = $serverRequest->withQueryParams(array(
'new' => 'new',
));
$this->assertSame(array(
'new' => 'new',
), $serverRequest->getQueryParams());
}
public function testProperties()
{
$serverRequest = $this->createServerRequest();
$properties = array(
'attributes' => array(),
'cookie' => array(),
'get' => array(),
'post' => null,
'server' => array(
'REQUEST_METHOD' => 'GET',
),
'files' => array(),
);
$reflection = new ReflectionObject($serverRequest);
foreach ($properties as $k => $vExpect) {
$prop = $reflection->getProperty($k);
$prop->setAccessible(true);
$this->assertSame($vExpect, $prop->getValue($serverRequest), $k);
unset($prop);
}
}
public function testGetMethods()
{
// Test 1
$serverRequest = $this->createServerRequest();
$this->assertSame('GET', $serverRequest->getMethod());
$this->assertSame([
'REQUEST_METHOD' => 'GET',
], $serverRequest->getServerParams());
$this->assertSame([], $serverRequest->getCookieParams());
$this->assertSame(null, $serverRequest->getParsedBody());
$this->assertSame([], $serverRequest->getQueryParams());
$this->assertSame([], $serverRequest->getUploadedFiles());
$this->assertSame([], $serverRequest->getAttributes());
// Test 2
$serverRequest = $this->createServerRequest('POST', '', array('what' => 'server'))
->withCookieParams(array('what' => 'cookie'))
->withParsedBody(array('what' => 'post'))
->withQueryParams(array('what' => 'get'))
->withAttribute('what', 'attribute')
->withUploadedFiles(self::mockFiles(1));
$this->assertSame('POST', $serverRequest->getMethod());
$this->assertEquals(array(
'what' => 'server',
'REQUEST_METHOD' => 'POST',
), $serverRequest->getServerParams());
$this->assertEquals(array('what' => 'cookie'), $serverRequest->getCookieParams());
$this->assertEquals(array('what' => 'post'), $serverRequest->getParsedBody());
$this->assertEquals(array('what' => 'get'), $serverRequest->getQueryParams());
$this->assertEquals(array('what' => 'attribute'), $serverRequest->getAttributes());
$this->assertEquals(
array(
'file1' => $this->createUploadedFile(
'/tmp/php1234.tmp',
100000,
UPLOAD_ERR_OK,
'file1.jpg',
'image/jpeg'
),
),
$serverRequest->getUploadedFiles()
);
}
public function testWithMethods()
{
$new = $this->createServerRequest()
->withCookieParams(['foo3' => 'bar3'])
->withParsedBody(['foo4' => 'bar4', 'foo5' => 'bar5'])
->withQueryParams(['foo6' => 'bar6', 'foo7' => 'bar7'])
->withAttribute('foo8', 'bar9')
->withUploadedFiles(self::mockFiles(2));
$this->assertSame('GET', $new->getMethod());
$this->assertEquals([
'REQUEST_METHOD' => 'GET',
], $new->getServerParams());
$this->assertEquals(['foo3' => 'bar3'], $new->getCookieParams());
$this->assertEquals(['foo4' => 'bar4', 'foo5' => 'bar5'], $new->getParsedBody());
$this->assertEquals(['foo6' => 'bar6', 'foo7' => 'bar7'], $new->getQueryParams());
$this->assertEquals('bar9', $new->getAttribute('foo8'));
$this->assertEquals(
array(
'file2' => $this->createUploadedFile(
'/tmp/php1235',
123456,
UPLOAD_ERR_OK,
'file2.png',
'image/png'
),
),
$new->getUploadedFiles()
);
$new2 = $new->withoutAttribute('foo8');
$this->assertEquals(null, $new2->getAttribute('foo8'));
$this->assertSame($new2, $new2->withoutAttribute('noSuch'));
$this->assertSame($new2, $new2->withoutAttribute(false));
}
/**
* @param $value
*
* @dataProvider validQueryParams
*/
public function testWithQueryParamsAcceptsValidValues($value)
{
$params = null;
\parse_str($value, $params);
$request = $this->createServerRequest()
->withQueryParams($params);
$this->assertSame($params, $request->getQueryParams());
}
/**
* @param $value
*
* @dataProvider invalidQueryParams
*/
public function testWithQueryParamsRejectsInvalidValues($value)
{
$exceptionClass = \is_array($value)
? 'InvalidArgumentException'
: (PHP_VERSION_ID >= 70000
? 'TypeError'
: 'RuntimeException');
$this->expectException($exceptionClass);
$this->createServerRequest()
->withQueryParams($value);
}
/**
* @param $value
*
* @dataProvider validCookieParams
*/
public function testWithCookieParamsAcceptsValidValues($value)
{
$request = $this->createServerRequest()
->withCookieParams($value);
$this->assertSame($value, $request->getCookieParams());
}
/**
* @param $value
*
* @dataProvider invalidCookieParams
*/
public function testWithCookieParamsRejectsInvalidValues($value)
{
$exceptionClass = \is_array($value)
? 'InvalidArgumentException'
: (PHP_VERSION_ID >= 70000
? 'TypeError'
: 'RuntimeException');
$this->expectException($exceptionClass);
$this->createServerRequest()
->withCookieParams($value);
}
/**
* @param $name
* @param $value
*
* @dataProvider validAttributeNamesAndValues
*/
public function testWithAttributeAcceptsValidNamesAndValues($name, $value)
{
$request = $this->createServerRequest()
->withAttribute($name, $value);
$this->assertSame($value, $request->getAttribute($name));
}
/**
* @param $name
* @param $value
*
* @dataProvider invalidAttributeNamesAndValues
*/
public function testWithAttributeRejectsInvalidValues($name, $value)
{
self::assertExceptionOrTypeError(function () use ($name, $value) {
$this->createServerRequest()
->withAttribute($name, $value);
});
}
/*
Exceptions
*/
public function testExceptionUploadedFilesArray()
{
// $value = (object) [];
$value = 'not array';
$exceptionClass = \is_array($value)
? 'InvalidArgumentException'
: (PHP_VERSION_ID >= 70000
? 'TypeError'
: 'RuntimeException');
$this->expectException($exceptionClass);
$this->createServerRequest()
->withUploadedFiles($value);
}
public function testExceptionUploadedFiles()
{
$this->expectException('InvalidArgumentException');
$this->expectExceptionMessage('Invalid file in uploaded files structure. Expected UploadedFileInterface, string provided');
$this->createServerRequest()
->withUploadedFiles([
[
['files' => ''],
],
]);
}
public function testExceptionParsedBody()
{
$this->expectException('InvalidArgumentException');
$this->expectExceptionMessage('ParsedBody must be array, object, or null. string provided.');
$serverRequest = $this->createServerRequest()
->withParsedBody('I am a string');
}
public function testExceptionParseStrOpts()
{
$this->expectException('InvalidArgumentException');
$this->expectExceptionMessage('parseStrOpts expects string or array. boolean provided.');
ParseStr::setOpts(false);
}
/*
Methods that help for testing.
*/
/**
* Moke a uploadedFiles array
*
* @param int $item which array to return
*
* @return array
*/
private static function mockFiles($item = 1)
{
if ($item === 1) {
return array(
'file1' => self::createUploadedFile(
'/tmp/php1234.tmp',
100000,
UPLOAD_ERR_OK,
'file1.jpg',
'image/jpeg'
),
);
}
if ($item === 2) {
return array(
'file2' => self::createUploadedFile(
'/tmp/php1235',
123456,
UPLOAD_ERR_OK,
'file2.png',
'image/png'
),
);
}
}
}