-
-
Notifications
You must be signed in to change notification settings - Fork 377
Expand file tree
/
Copy pathCommonHttpTest.php
More file actions
610 lines (532 loc) · 19.9 KB
/
CommonHttpTest.php
File metadata and controls
610 lines (532 loc) · 19.9 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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
<?php declare(strict_types=1);
namespace Bref\Test\Event\Http;
use Bref\Test\HttpRequestProxyTest;
use PHPUnit\Framework\TestCase;
abstract class CommonHttpTest extends TestCase implements HttpRequestProxyTest
{
public function provide API Gateway versions(): array
{
return [
'v1' => [1],
'v2' => [2],
];
}
public function test request with no version fallbacks to v1()
{
$this->fromFixture(__DIR__ . '/Fixture/ag-no-version.json');
$this->assertBody('');
$this->assertContentType(null);
$this->assertHeaders([
'accept' => ['*/*'],
'accept-encoding' => ['gzip, deflate'],
'cache-control' => ['no-cache'],
'host' => ['example.org'],
'user-agent' => ['PostmanRuntime/7.20.1'],
'x-amzn-trace-id' => ['Root=1-ffffffff-ffffffffffffffffffffffff'],
'x-forwarded-for' => ['1.1.1.1'],
'x-forwarded-port' => ['443'],
'x-forwarded-proto' => ['https'],
]);
$this->assertMethod('GET');
$this->assertUri('/path');
$this->assertSourceIp('1.1.1.1');
}
/**
* @dataProvider provide API Gateway versions
*/
public function test simple request(int $version)
{
$this->fromFixture(__DIR__ . "/Fixture/ag-v$version-simple.json");
$this->assertBody('');
$this->assertContentType(null);
$this->assertCookies([]);
$this->assertHeaders([
'accept' => ['*/*'],
'accept-encoding' => ['gzip, deflate'],
'cache-control' => ['no-cache'],
'host' => ['example.org'],
'user-agent' => ['PostmanRuntime/7.20.1'],
'x-amzn-trace-id' => ['Root=1-ffffffff-ffffffffffffffffffffffff'],
'x-forwarded-for' => ['1.1.1.1'],
'x-forwarded-port' => ['443'],
'x-forwarded-proto' => ['https'],
]);
$this->assertMethod('GET');
$this->assertPath('/path');
$this->assertProtocol('HTTP/1.1');
$this->assertProtocolVersion('1.1');
$this->assertQueryParameters([]);
$this->assertQueryString('');
$this->assertRemotePort(443);
$this->assertServerName('example.org');
$this->assertServerPort(443);
$this->assertUri('/path');
$this->assertHasMultiHeader(false);
$this->assertSourceIp('1.1.1.1');
}
public function test v1 stage prefix is not included in the URL()
{
$this->fromFixture(__DIR__ . '/Fixture/ag-v1-stage-prefix.json');
$this->assertPath('/path');
$this->assertUri('/path');
}
/**
* @dataProvider provide API Gateway versions
*/
public function test request with query string(int $version)
{
$this->fromFixture(__DIR__ . "/Fixture/ag-v$version-query-string.json");
$this->assertPath('/path');
$this->assertQueryParameters(['foo' => 'bar', 'baz.bar' => 'foo']);
$this->assertQueryString('foo=bar&baz.bar=foo');
$this->assertUri('/path?foo=bar&baz.bar=foo');
}
/**
* @dataProvider provide API Gateway versions
*/
public function test request with multivalues query string have basic support(int $version)
{
$this->fromFixture(__DIR__ . "/Fixture/ag-v$version-query-string-multivalue.json");
$this->assertQueryParameters([
'foo' => ['bar', 'baz'],
'cards' => ['birthday'],
'colors' => [['red'], ['blue']],
'shapes' => ['a' => ['square', 'triangle']],
'myvar' => 'abc',
'foo.bar' => ['baz'],
'array' => ['one', 'two'],
]);
$this->assertQueryString('foo%5B0%5D=bar&foo%5B1%5D=baz&cards%5B0%5D=birthday&colors%5B0%5D%5B0%5D=red&colors%5B1%5D%5B0%5D=blue&shapes%5Ba%5D%5B0%5D=square&shapes%5Ba%5D%5B1%5D=triangle&myvar=abc&foo.bar%5B0%5D=baz&array%5B0%5D=one&array%5B1%5D=two');
$this->assertUri('/path?foo%5B0%5D=bar&foo%5B1%5D=baz&cards%5B0%5D=birthday&colors%5B0%5D%5B0%5D=red&colors%5B1%5D%5B0%5D=blue&shapes%5Ba%5D%5B0%5D=square&shapes%5Ba%5D%5B1%5D=triangle&myvar=abc&foo.bar%5B0%5D=baz&array%5B0%5D=one&array%5B1%5D=two');
}
/**
* @dataProvider provide API Gateway versions
*/
public function test request with arrays in query string(int $version)
{
$this->fromFixture(__DIR__ . "/Fixture/ag-v$version-query-string-arrays.json");
$this->assertQueryParameters([
'vars' => [
'val1' => 'foo',
'val2' => ['bar'],
],
'foo.bar' => ['baz'],
]);
if ($version === 2) {
// Numeric keys are added as an artifact of us parsing the query string
// Both format are valid and semantically identical
$this->assertQueryString('vars%5Bval1%5D=foo&vars%5Bval2%5D%5B0%5D=bar&foo.bar%5B0%5D=baz');
$this->assertUri('/path?vars%5Bval1%5D=foo&vars%5Bval2%5D%5B0%5D=bar&foo.bar%5B0%5D=baz');
} else {
$this->assertQueryString('vars%5Bval1%5D=foo&vars%5Bval2%5D%5B%5D=bar&foo.bar%5B%5D=baz');
$this->assertUri('/path?vars%5Bval1%5D=foo&vars%5Bval2%5D%5B%5D=bar&foo.bar%5B%5D=baz');
}
}
/**
* @dataProvider provide API Gateway versions
*/
public function test request with custom header(int $version)
{
$this->fromFixture(__DIR__ . "/Fixture/ag-v$version-header-custom.json");
$this->assertHeader('x-my-header', ['Hello world']);
}
/**
* @dataProvider provide API Gateway versions
*/
public function test request with custom multi header(int $version)
{
$this->fromFixture(__DIR__ . "/Fixture/ag-v$version-header-custom-multivalue.json");
if ($version === 2) {
// In v2, multi-value headers are joined by a comma
// See https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
$this->assertHeader('x-my-header', ['Hello world,Hello john']);
} else {
$this->assertHeader('x-my-header', ['Hello world', 'Hello john']);
$this->assertHasMultiHeader(true);
}
}
/**
* @dataProvider provide API Gateway versions
*/
public function test request with numeric header(int $version)
{
$this->fromFixture(__DIR__ . "/Fixture/ag-v$version-header-numeric.json");
$this->assertHeader('12345', ['Hello world']);
}
/**
* @dataProvider provide API Gateway versions
*/
public function test request with numeric multi header(int $version)
{
$this->fromFixture(__DIR__ . "/Fixture/ag-v$version-header-numeric-multivalue.json");
if ($version === 2) {
// In v2, multi-value headers are joined by a comma
// See https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
$this->assertHeader('12345', ['Hello world,Hello john']);
} else {
$this->assertHeader('12345', ['Hello world', 'Hello john']);
$this->assertHasMultiHeader(true);
}
}
/**
* @dataProvider provide API Gateway versions
*/
public function test POST request with raw body(int $version)
{
$this->fromFixture(__DIR__ . "/Fixture/ag-v$version-body-json.json");
$this->assertMethod('PUT');
$this->assertContentType('application/json');
$this->assertHeader('content-length', [13]);
$this->assertBody('{"foo":"bar"}');
}
/**
* @dataProvider provide API Gateway versions
*/
public function test POST request with form data(int $version)
{
$this->fromFixture(__DIR__ . "/Fixture/ag-v$version-body-form.json");
$this->assertMethod('POST');
$this->assertContentType('application/x-www-form-urlencoded');
$this->assertHeader('content-length', [15]);
$this->assertBody('foo=bar&bim=baz');
$this->assertParsedBody([
'foo' => 'bar',
'bim' => 'baz',
]);
}
/**
* @dataProvider provide API Gateway versions
*/
public function test POST request with form data and content type(int $version)
{
$this->fromFixture(__DIR__ . "/Fixture/ag-v$version-body-form-content-type.json");
$this->assertContentType('application/x-www-form-urlencoded;charset=UTF-8');
$this->assertParsedBody([
'foo' => 'bar',
'bim' => 'baz',
]);
}
public function provideHttpMethodsWithRequestBodySupport(): array
{
return [
'POST v1' => [
'version' => 1,
'method' => 'POST',
],
'POST v2' => [
'version' => 2,
'method' => 'POST',
],
'PUT v1' => [
'version' => 1,
'method' => 'PUT',
],
'PUT v2' => [
'version' => 2,
'method' => 'PUT',
],
'PATCH v1' => [
'version' => 1,
'method' => 'PATCH',
],
'PATCH v2' => [
'version' => 2,
'method' => 'PATCH',
],
];
}
/**
* @see https://github.com/brefphp/bref/issues/162
*
* @dataProvider provideHttpMethodsWithRequestBodySupport
*/
public function test request with body and no content length(int $version, string $method)
{
// These requests do not have a Content-Length header on purpose
$this->fromFixture(__DIR__ . "/Fixture/ag-v$version-missing-content-length-$method.json");
$this->assertMethod($method);
// We check the header is added automatically
$this->assertHeader('content-length', [13]);
}
/**
* @dataProvider provide API Gateway versions
*/
public function test request supports utf8 characters in body(int $version)
{
// These requests have a multibyte body: 'Hello 🌍'
$this->fromFixture(__DIR__ . "/Fixture/ag-v$version-body-utf8.json");
$this->assertBody('Hello 🌍');
// We check the header is added automatically and takes multibyte into account
$this->assertHeader('content-length', [10]);
}
/**
* @dataProvider provide API Gateway versions
*/
public function test the content type header is not case sensitive(int $version)
{
$this->fromFixture(__DIR__ . "/Fixture/ag-v$version-content-type-lower-case.json");
$this->assertContentType('application/json');
$this->assertHeader('content-length', [13]);
}
/**
* @dataProvider provide API Gateway versions
*/
public function test POST request with multipart form data(int $version)
{
$this->fromFixture(__DIR__ . "/Fixture/ag-v$version-body-form-multipart.json");
$this->assertContentType('multipart/form-data; boundary=testBoundary');
$this->assertHeader('content-length', [152]);
$body = "--testBoundary\r
Content-Disposition: form-data; name=\"foo\"\r
\r
bar\r
--testBoundary\r
Content-Disposition: form-data; name=\"bim\"\r
\r
baz\r
--testBoundary--\r
";
$this->assertBody($body);
$this->assertParsedBody([
'foo' => 'bar',
'bim' => 'baz',
]);
}
/**
* @dataProvider provide API Gateway versions
*/
public function test POST request with multipart form data containing arrays(int $version)
{
$this->fromFixture(__DIR__ . "/Fixture/ag-v$version-body-form-multipart-arrays.json");
$this->assertContentType('multipart/form-data; boundary=testBoundary');
$this->assertHeader('content-length', [186]);
$body = "--testBoundary\r
Content-Disposition: form-data; name=\"delete[categories][]\"\r
\r
123\r
--testBoundary\r
Content-Disposition: form-data; name=\"delete[categories][]\"\r
\r
456\r
--testBoundary--\r
";
$this->assertBody($body);
$this->assertParsedBody([
'delete' => [
'categories' => [
'123',
'456',
],
],
]);
}
/**
* @dataProvider provide API Gateway versions
*/
public function test POST request with malformed multipart form data(int $version)
{
$this->fromFixture(__DIR__ . "/Fixture/ag-v$version-body-form-multipart-arrays-malformed.json");
$this->assertContentType('multipart/form-data; boundary=testBoundary');
$body = "--testBoundary\r
Content-Disposition: form-data; name=\"key0[key1][key2][\"\r
\r
123\r
--testBoundary--\r
";
$this->assertBody($body);
$this->assertParsedBody(['key0' => ['key1' => ['key2' => '123']]]);
}
/**
* @dataProvider provide API Gateway versions
*/
public function test POST request with multipart file uploads(int $version)
{
$this->fromFixture(__DIR__ . "/Fixture/ag-v$version-body-form-multipart-files.json");
$this->assertContentType('multipart/form-data; boundary=testBoundary');
$this->assertHeader('content-length', [323]);
$body = "--testBoundary\r
Content-Disposition: form-data; name=\"foo\"; filename=\"lorem.txt\"\r
Content-Type: text/plain\r
\r
Lorem ipsum dolor sit amet,
consectetur adipiscing elit.
\r
--testBoundary\r
Content-Disposition: form-data; name=\"bar\"; filename=\"cars.csv\"\r
\r
Year,Make,Model
1997,Ford,E350
2000,Mercury,Cougar
\r
--testBoundary--\r
";
$this->assertBody($body);
$this->assertParsedBody(null);
$this->assertUploadedFile(
'foo',
'lorem.txt',
'text/plain',
0,
57,
"Lorem ipsum dolor sit amet,\nconsectetur adipiscing elit.\n"
);
$this->assertUploadedFile(
'bar',
'cars.csv',
'application/octet-stream',
0,
51,
"Year,Make,Model\n1997,Ford,E350\n2000,Mercury,Cougar\n"
);
}
/**
* @dataProvider provide API Gateway versions
*/
public function test POST request with multipart form data containing structured arrays(int $version)
{
var_dump($version);
$this->fromFixture(__DIR__ . "/Fixture/ag-v$version-body-form-multipart-structured-arrays.json");
$this->assertContentType('multipart/form-data; boundary=testBoundary');
$this->assertMethod('POST');
$this->assertParsedBody([
'content' => '<h1>Test content</h1>',
'some_id' => '3034',
'references' => [
[
'other_id' => '4390954279',
'url' => '',
],
[
'other_id' => '4313323164',
'url' => '',
],
[
'other_id' => '',
'url' => 'https://someurl.com/node/745911',
],
],
'tags' => [
'public health',
'public finance',
],
'_method' => 'PATCH',
]);
}
/**
* @dataProvider provide API Gateway versions
*/
public function test request with cookies(int $version)
{
$this->fromFixture(__DIR__ . "/Fixture/ag-v$version-cookies.json");
$this->assertCookies([
'tz' => 'Europe/Paris',
'four' => 'two + 2',
'theme' => 'light',
]);
}
/**
* @dataProvider provide API Gateway versions
*/
public function test request with invalid cookies(int $version)
{
$this->fromFixture(__DIR__ . "/Fixture/ag-v$version-cookies-invalid.json");
// See https://stackoverflow.com/a/61695783/245552
$this->assertCookies([], 'foo');
}
/**
* @dataProvider provide API Gateway versions
*/
public function test POST request with base64 encoded body(int $version)
{
$this->fromFixture(__DIR__ . "/Fixture/ag-v$version-body-base64.json");
$this->assertMethod('POST');
$this->assertContentType('application/x-www-form-urlencoded');
$this->assertHeader('content-length', [7]);
$this->assertBody('foo=bar');
$this->assertParsedBody([
'foo' => 'bar',
]);
}
/**
* @dataProvider provide API Gateway versions
*/
public function test PUT request(int $version)
{
$this->fromFixture(__DIR__ . "/Fixture/ag-v$version-method-PUT.json");
$this->assertMethod('PUT');
}
/**
* @dataProvider provide API Gateway versions
*/
public function test PATCH request(int $version)
{
$this->fromFixture(__DIR__ . "/Fixture/ag-v$version-method-PATCH.json");
$this->assertMethod('PATCH');
}
/**
* @dataProvider provide API Gateway versions
*/
public function test DELETE request(int $version)
{
$this->fromFixture(__DIR__ . "/Fixture/ag-v$version-method-DELETE.json");
$this->assertMethod('DELETE');
}
/**
* @dataProvider provide API Gateway versions
*/
public function test OPTIONS request(int $version)
{
$this->fromFixture(__DIR__ . "/Fixture/ag-v$version-method-OPTIONS.json");
$this->assertMethod('OPTIONS');
}
/**
* @dataProvider provide API Gateway versions
*/
public function test path parameters(int $version)
{
$this->fromFixture(__DIR__ . "/Fixture/ag-v$version-path-parameters.json");
$this->assertPathParameters([
'bar' => 'abc',
'baz' => 'def',
]);
}
/**
* @dataProvider provide API Gateway versions
*/
public function test request with basic auth(int $version)
{
$this->fromFixture(__DIR__ . "/Fixture/ag-v$version-header-basic-auth.json");
$this->assertBasicAuthUser('fake');
$this->assertBasicAuthPassword('secret');
}
abstract protected function fromFixture(string $file): void;
abstract protected function assertBody(string $expected): void;
abstract protected function assertContentType(?string $expected): void;
abstract protected function assertCookies(array $expected, string |null $expectedHeader = null): void;
abstract protected function assertHeaders(array $expected): void;
abstract protected function assertHeader(string $header, array $expectedValue): void;
abstract protected function assertMethod(string $expected): void;
abstract protected function assertPath(string $expected): void;
abstract protected function assertQueryString(string $expected): void;
abstract protected function assertQueryParameters(array $expected): void;
abstract protected function assertProtocol(string $expected): void;
abstract protected function assertProtocolVersion(string $expected): void;
abstract protected function assertRemotePort(int $expected): void;
abstract protected function assertServerName(string $expected): void;
abstract protected function assertServerPort(int $expected): void;
abstract protected function assertUri(string $expected): void;
abstract protected function assertHasMultiHeader(bool $expected): void;
abstract protected function assertParsedBody(array | null $expected): void;
abstract protected function assertSourceIp(string $expected): void;
abstract protected function assertBasicAuthUser(string $expected): void;
abstract protected function assertBasicAuthPassword(string $expected): void;
abstract protected function assertUploadedFile(
string $key,
string $filename,
string $mimeType,
int $error,
int $size,
string $content
): void;
abstract protected function assertPathParameters(array $expected): void;
}