/
HttpResponseTest.php
180 lines (159 loc) · 5.47 KB
/
HttpResponseTest.php
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
<?php declare(strict_types=1);
namespace Bref\Test\Event\Http;
use Bref\Event\Http\HttpResponse;
use PHPUnit\Framework\TestCase;
use stdClass;
class HttpResponseTest extends TestCase
{
public function test conversion to API Gateway format()
{
$response = new HttpResponse(
'<p>Hello world!</p>',
[
'Content-Type' => 'text/html; charset=utf-8',
]
);
self::assertSame([
'isBase64Encoded' => false,
'statusCode' => 200,
'headers' => [
'Content-Type' => 'text/html; charset=utf-8',
],
'body' => '<p>Hello world!</p>',
], $response->toApiGatewayFormat());
self::assertSame([
'cookies' => [],
'isBase64Encoded' => false,
'statusCode' => 200,
'headers' => [
'Content-Type' => 'text/html; charset=utf-8',
],
'body' => '<p>Hello world!</p>',
], $response->toApiGatewayFormatV2());
}
public function test headers are capitalized()
{
$response = new HttpResponse('', [
'x-foo-bar' => 'baz',
]);
self::assertEquals([
'isBase64Encoded' => false,
'statusCode' => 200,
'headers' => ['X-Foo-Bar' => 'baz'],
'body' => '',
], $response->toApiGatewayFormat());
self::assertEquals([
'cookies' => [],
'isBase64Encoded' => false,
'statusCode' => 200,
'headers' => ['X-Foo-Bar' => 'baz'],
'body' => '',
], $response->toApiGatewayFormatV2());
}
public function test multi value headers()
{
$response = new HttpResponse('', [
'foo' => ['bar', 'baz'],
]);
self::assertEquals([
'isBase64Encoded' => false,
'statusCode' => 200,
// The last value is kept (when multiheaders are not enabled)
'headers' => ['Foo' => 'baz'],
'body' => '',
], $response->toApiGatewayFormat());
self::assertEquals([
'cookies' => [],
'isBase64Encoded' => false,
'statusCode' => 200,
// Headers are joined with a comma
// See https://datatracker.ietf.org/doc/html/rfc7230#section-3.2.2
// API Gateway v2 does not support multi-value headers
'headers' => ['Foo' => 'bar, baz'],
'body' => '',
], $response->toApiGatewayFormatV2());
}
public function test empty headers are considered objects()
{
$response = new HttpResponse('');
// Make sure that the headers are `"headers":{}` (object) and not `"headers":[]` (array)
self::assertEquals('{"isBase64Encoded":false,"statusCode":200,"headers":{},"body":""}', json_encode($response->toApiGatewayFormat()));
self::assertEquals('{"cookies":[],"isBase64Encoded":false,"statusCode":200,"headers":{},"body":""}', json_encode($response->toApiGatewayFormatV2()));
}
/**
* @see https://github.com/brefphp/bref/issues/534
*/
public function test header values are forced as arrays for multiheaders()
{
$response = new HttpResponse('', [
'foo' => 'bar',
]);
self::assertEquals([
'isBase64Encoded' => false,
'statusCode' => 200,
'multiValueHeaders' => [
'Foo' => ['bar'],
],
'body' => '',
], $response->toApiGatewayFormat(true));
}
public function test response with single cookie()
{
$response = new HttpResponse('', [
'set-cookie' => 'foo',
]);
self::assertEquals([
'isBase64Encoded' => false,
'statusCode' => 200,
'headers' => [
'Set-Cookie' => 'foo',
],
'body' => '',
], $response->toApiGatewayFormat());
self::assertEquals([
'isBase64Encoded' => false,
'statusCode' => 200,
'multiValueHeaders' => [
'Set-Cookie' => ['foo'],
],
'body' => '',
], $response->toApiGatewayFormat(true));
self::assertEquals([
'cookies' => ['foo'],
'isBase64Encoded' => false,
'statusCode' => 200,
'headers' => new stdClass,
'body' => '',
], $response->toApiGatewayFormatV2());
}
public function test response with multiple cookies()
{
$response = new HttpResponse('', [
'set-cookie' => ['foo', 'bar'],
]);
self::assertEquals([
'isBase64Encoded' => false,
'statusCode' => 200,
'headers' => [
// Keep only the last value in v1 without multi-headers
'Set-Cookie' => 'bar',
],
'body' => '',
], $response->toApiGatewayFormat());
self::assertEquals([
'isBase64Encoded' => false,
'statusCode' => 200,
'multiValueHeaders' => [
'Set-Cookie' => ['foo', 'bar'],
],
'body' => '',
], $response->toApiGatewayFormat(true));
self::assertEquals([
'cookies' => ['foo', 'bar'],
'isBase64Encoded' => false,
'statusCode' => 200,
'headers' => new stdClass,
'body' => '',
], $response->toApiGatewayFormatV2());
}
}