-
-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathResponseTest.php
More file actions
247 lines (200 loc) · 9.5 KB
/
Copy pathResponseTest.php
File metadata and controls
247 lines (200 loc) · 9.5 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
<?php
namespace React\Tests\Http\Message;
use Psr\Http\Message\StreamInterface;
use React\Http\Io\HttpBodyStream;
use React\Http\Message\Response;
use React\Stream\ReadableStreamInterface;
use React\Stream\ThroughStream;
use React\Tests\Http\TestCase;
class ResponseTest extends TestCase
{
public function testConstructWithStringBodyWillReturnStreamInstance()
{
$response = new Response(200, [], 'hello');
$body = $response->getBody();
/** @var \Psr\Http\Message\StreamInterface $body */
$this->assertInstanceOf(StreamInterface::class, $body);
$this->assertEquals('hello', (string) $body);
}
public function testConstructWithStreamingBodyWillReturnReadableBodyStream()
{
$response = new Response(200, [], new ThroughStream());
$body = $response->getBody();
/** @var \Psr\Http\Message\StreamInterface $body */
$this->assertInstanceOf(StreamInterface::class, $body);
$this->assertInstanceof(ReadableStreamInterface::class, $body);
$this->assertInstanceOf(HttpBodyStream::class, $body);
$this->assertNull($body->getSize());
}
public function testConstructWithHttpBodyStreamReturnsBodyAsIs()
{
$response = new Response(
200,
[],
$body = new HttpBodyStream(new ThroughStream(), 100)
);
$this->assertSame($body, $response->getBody());
}
public function testFloatBodyWillThrow()
{
$this->expectException(\InvalidArgumentException::class);
new Response(200, [], 1.0);
}
public function testResourceBodyWillThrow()
{
$this->expectException(\InvalidArgumentException::class);
new Response(200, [], tmpfile());
}
public function testWithStatusReturnsNewInstanceWhenStatusIsChanged()
{
$response = new Response(200);
$new = $response->withStatus(404);
$this->assertNotSame($response, $new);
$this->assertEquals(404, $new->getStatusCode());
$this->assertEquals('Not Found', $new->getReasonPhrase());
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('OK', $response->getReasonPhrase());
}
public function testWithStatusReturnsSameInstanceWhenStatusIsUnchanged()
{
$response = new Response(200);
$new = $response->withStatus(200);
$this->assertSame($response, $new);
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('OK', $response->getReasonPhrase());
}
public function testWithStatusReturnsNewInstanceWhenStatusIsUnchangedButReasonIsChanged()
{
$response = new Response(200);
$new = $response->withStatus(200, 'Quite Ok');
$this->assertNotSame($response, $new);
$this->assertEquals(200, $new->getStatusCode());
$this->assertEquals('Quite Ok', $new->getReasonPhrase());
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('OK', $response->getReasonPhrase());
}
public function testHtmlMethodReturnsHtmlResponse()
{
$response = Response::html('<!doctype html><body>Hello wörld!</body>');
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('text/html; charset=utf-8', $response->getHeaderLine('Content-Type'));
$this->assertEquals('<!doctype html><body>Hello wörld!</body>', (string) $response->getBody());
}
public function testJsonMethodReturnsPrettyPrintedJsonResponse()
{
$response = Response::json(['text' => 'Hello wörld!']);
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('application/json', $response->getHeaderLine('Content-Type'));
$this->assertEquals("{\n \"text\": \"Hello wörld!\"\n}\n", (string) $response->getBody());
}
public function testJsonMethodReturnsZeroFractionsInJsonResponse()
{
$response = Response::json(1.0);
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('application/json', $response->getHeaderLine('Content-Type'));
$this->assertEquals("1.0\n", (string) $response->getBody());
}
public function testJsonMethodReturnsJsonTextForSimpleString()
{
$response = Response::json('Hello world!');
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('application/json', $response->getHeaderLine('Content-Type'));
$this->assertEquals("\"Hello world!\"\n", (string) $response->getBody());
}
public function testJsonMethodThrowsForInvalidString()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Unable to encode given data as JSON: Malformed UTF-8 characters, possibly incorrectly encoded');
Response::json("Hello w\xF6rld!");
}
public function testPlaintextMethodReturnsPlaintextResponse()
{
$response = Response::plaintext("Hello wörld!\n");
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('text/plain; charset=utf-8', $response->getHeaderLine('Content-Type'));
$this->assertEquals("Hello wörld!\n", (string) $response->getBody());
}
public function testXmlMethodReturnsXmlResponse()
{
$response = Response::xml('<?xml version="1.0" encoding="utf-8"?><body>Hello wörld!</body>');
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('application/xml', $response->getHeaderLine('Content-Type'));
$this->assertEquals('<?xml version="1.0" encoding="utf-8"?><body>Hello wörld!</body>', (string) $response->getBody());
}
public function testParseMessageWithMinimalOkResponse()
{
$response = Response::parseMessage("HTTP/1.1 200 OK\r\n");
$this->assertEquals('1.1', $response->getProtocolVersion());
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('OK', $response->getReasonPhrase());
$this->assertEquals([], $response->getHeaders());
}
public function testParseMessageWithSimpleOkResponse()
{
$response = Response::parseMessage("HTTP/1.1 200 OK\r\nServer: demo\r\n");
$this->assertEquals('1.1', $response->getProtocolVersion());
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('OK', $response->getReasonPhrase());
$this->assertEquals(['Server' => ['demo']], $response->getHeaders());
}
public function testParseMessageWithSimpleOkResponseWithCustomReasonPhrase()
{
$response = Response::parseMessage("HTTP/1.1 200 Mostly Okay\r\nServer: demo\r\n");
$this->assertEquals('1.1', $response->getProtocolVersion());
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('Mostly Okay', $response->getReasonPhrase());
$this->assertEquals(['Server' => ['demo']], $response->getHeaders());
}
public function testParseMessageWithSimpleOkResponseWithEmptyReasonPhraseAppliesDefault()
{
$response = Response::parseMessage("HTTP/1.1 200 \r\nServer: demo\r\n");
$this->assertEquals('1.1', $response->getProtocolVersion());
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('OK', $response->getReasonPhrase());
$this->assertEquals(['Server' => ['demo']], $response->getHeaders());
}
public function testParseMessageWithSimpleOkResponseWithoutReasonPhraseAndWhitespaceSeparatorAppliesDefault()
{
$response = Response::parseMessage("HTTP/1.1 200\r\nServer: demo\r\n");
$this->assertEquals('1.1', $response->getProtocolVersion());
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('OK', $response->getReasonPhrase());
$this->assertEquals(['Server' => ['demo']], $response->getHeaders());
}
public function testParseMessageWithHttp10SimpleOkResponse()
{
$response = Response::parseMessage("HTTP/1.0 200 OK\r\nServer: demo\r\n");
$this->assertEquals('1.0', $response->getProtocolVersion());
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('OK', $response->getReasonPhrase());
$this->assertEquals(['Server' => ['demo']], $response->getHeaders());
}
public function testParseMessageWithHttp10SimpleOkResponseWithLegacyNewlines()
{
$response = Response::parseMessage("HTTP/1.0 200 OK\nServer: demo\r\n");
$this->assertEquals('1.0', $response->getProtocolVersion());
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('OK', $response->getReasonPhrase());
$this->assertEquals(['Server' => ['demo']], $response->getHeaders());
}
public function testParseMessageWithInvalidHttpProtocolVersion12Throws()
{
$this->expectException(\InvalidArgumentException::class);
Response::parseMessage("HTTP/1.2 200 OK\r\n");
}
public function testParseMessageWithInvalidHttpProtocolVersion2Throws()
{
$this->expectException(\InvalidArgumentException::class);
Response::parseMessage("HTTP/2 200 OK\r\n");
}
public function testParseMessageWithInvalidStatusCodeUnderflowThrows()
{
$this->expectException(\InvalidArgumentException::class);
Response::parseMessage("HTTP/1.1 99 OK\r\n");
}
public function testParseMessageWithInvalidResponseHeaderFieldThrows()
{
$this->expectException(\InvalidArgumentException::class);
Response::parseMessage("HTTP/1.1 200 OK\r\nServer\r\n");
}
}