forked from slimphp/Slim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResponseEmitterTest.php
185 lines (150 loc) · 5.7 KB
/
ResponseEmitterTest.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
181
182
183
184
185
<?php
/**
* Slim Framework (https://slimframework.com)
*
* @link https://github.com/slimphp/Slim
* @copyright Copyright (c) 2011-2018 Josh Lockhart
* @license https://github.com/slimphp/Slim/blob/4.x/LICENSE.md (MIT License)
*/
namespace Slim\Tests;
use Slim\ResponseEmitter;
use Slim\Tests\Assets\HeaderStack;
use Slim\Tests\Mocks\MockStream;
use Slim\Tests\Mocks\SmallChunksStream;
/**
* Class ResponseEmitterTest
* @package Slim\Tests
*/
class ResponseEmitterTest extends TestCase
{
public function setUp()
{
HeaderStack::reset();
}
public function tearDown()
{
HeaderStack::reset();
}
public function testRespond()
{
$response = $this->createResponse();
$response->getBody()->write('Hello');
$responseEmitter = new ResponseEmitter();
$responseEmitter->emit($response);
$this->expectOutputString('Hello');
}
public function testRespondNoContent()
{
$response = $this->createResponse();
$responseEmitter = new ResponseEmitter();
$responseEmitter->emit($response);
$this->assertEquals(false, HeaderStack::has('Content-Type'));
$this->assertEquals(false, HeaderStack::has('Content-Length'));
$this->expectOutputString('');
}
public function testRespondWithPaddedStreamFilterOutput()
{
$availableFilter = stream_get_filters();
$filterName = 'string.rot13';
$unfilterName = 'string.rot13';
$specificFilterName = 'string.rot13';
$specificUnfilterName = 'string.rot13';
if (in_array($filterName, $availableFilter) && in_array($unfilterName, $availableFilter)) {
$key = base64_decode('xxxxxxxxxxxxxxxx');
$iv = base64_decode('Z6wNDk9LogWI4HYlRu0mng==');
$data = 'Hello';
$length = strlen($data);
$stream = fopen('php://temp', 'r+');
$filter = stream_filter_append($stream, $specificFilterName, STREAM_FILTER_WRITE, [
'key' => $key,
'iv' => $iv
]);
fwrite($stream, $data);
rewind($stream);
stream_filter_remove($filter);
stream_filter_append($stream, $specificUnfilterName, STREAM_FILTER_READ, [
'key' => $key,
'iv' => $iv
]);
$body = $this->getStreamFactory()->createStreamFromResource($stream);
$response = $this
->createResponse()
->withHeader('Content-Length', $length)
->withBody($body);
$responseEmitter = new ResponseEmitter();
$responseEmitter->emit($response);
$this->expectOutputString('Hello');
} else {
$this->assertTrue(true);
}
}
public function testRespondIndeterminateLength()
{
$stream = fopen('php://temp', 'r+');
fwrite($stream, 'Hello');
rewind($stream);
$body = $this
->getMockBuilder(MockStream::class)
->setConstructorArgs([$stream])
->setMethods(['getSize'])
->getMock();
$body->method('getSize')->willReturn(null);
$response = $this->createResponse()->withBody($body);
$responseEmitter = new ResponseEmitter();
$responseEmitter->emit($response);
$this->expectOutputString('Hello');
}
public function testResponseWithStreamReadYieldingLessBytesThanAsked()
{
$body = new SmallChunksStream();
$response = $this->createResponse()->withBody($body);
$responseEmitter = new ResponseEmitter($body::CHUNK_SIZE * 2);
$responseEmitter->emit($response);
$this->expectOutputString(str_repeat('.', $body->getSize()));
}
public function testResponseReplacesPreviouslySetHeaders()
{
$response = $this
->createResponse(200, 'OK')
->withHeader('X-Foo', 'baz1')
->withAddedHeader('X-Foo', 'baz2');
$responseEmitter = new ResponseEmitter();
$responseEmitter->emit($response);
$expectedStack = [
['header' => 'X-Foo: baz1', 'replace' => true, 'status_code' => null],
['header' => 'X-Foo: baz2', 'replace' => false, 'status_code' => null],
['header' => 'HTTP/1.1 200 OK', 'replace' => true, 'status_code' => 200],
];
$this->assertSame($expectedStack, HeaderStack::stack());
}
public function testResponseDoesNotReplacePreviouslySetSetCookieHeaders()
{
$response = $this
->createResponse(200, 'OK')
->withHeader('Set-Cookie', 'foo=bar')
->withAddedHeader('Set-Cookie', 'bar=baz');
$responseEmitter = new ResponseEmitter();
$responseEmitter->emit($response);
$expectedStack = [
['header' => 'Set-Cookie: foo=bar', 'replace' => false, 'status_code' => null],
['header' => 'Set-Cookie: bar=baz', 'replace' => false, 'status_code' => null],
['header' => 'HTTP/1.1 200 OK', 'replace' => true, 'status_code' => 200],
];
$this->assertSame($expectedStack, HeaderStack::stack());
}
public function testIsResponseEmptyWithNonEmptyBodyAndTriggeringStatusCode()
{
$body = $this->createStream('Hello');
$response = $this
->createResponse(204)
->withBody($body);
$responseEmitter = new ResponseEmitter();
$this->assertTrue($responseEmitter->isResponseEmpty($response));
}
public function testIsResponseEmptyWithEmptyBody()
{
$response = $this->createResponse(200);
$responseEmitter = new ResponseEmitter();
$this->assertTrue($responseEmitter->isResponseEmpty($response));
}
}