-
Notifications
You must be signed in to change notification settings - Fork 305
/
Copy pathSwooleClientTest.php
300 lines (231 loc) · 10.9 KB
/
SwooleClientTest.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
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
<?php
namespace Laravel\Octane\Tests;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Laravel\Octane\OctaneResponse;
use Laravel\Octane\RequestContext;
use Laravel\Octane\Swoole\SwooleClient;
use Mockery;
use Symfony\Component\HttpFoundation\StreamedResponse;
class SwooleClientTest extends TestCase
{
public function test_marshal_request_method_marshals_proper_illuminate_request()
{
$client = new SwooleClient;
$swooleRequest = new class
{
public $get = [
'name' => 'Taylor',
];
public $cookie = [
'color' => 'blue',
];
public $server = [
'HTTP_HOST' => 'localhost',
'PATH_INFO' => '/foo/bar',
'REMOTE_ADDR' => '127.0.0.1',
'REQUEST_METHOD' => 'POST',
'REQUEST_URI' => '/foo/bar',
'QUERY_STRING' => 'name=Taylor',
];
public function rawContent()
{
return 'Hello World';
}
};
[$request, $context] = $client->marshalRequest($givenContext = new RequestContext([
'swooleRequest' => $swooleRequest,
]));
$this->assertInstanceOf(Request::class, $request);
$this->assertEquals('POST', $request->getMethod());
$this->assertEquals('Hello World', $request->getContent());
$this->assertEquals('127.0.0.1', $request->ip());
$this->assertEquals('foo/bar', $request->path());
$this->assertEquals('/foo/bar?name=Taylor', $request->getRequestUri());
$this->assertEquals('Taylor', $request->query('name'));
$this->assertEquals('blue', $request->cookies->get('color'));
$this->assertSame($givenContext, $context);
}
public function test_can_serve_static_files_if_configured_to_and_file_is_within_public_directory()
{
$client = new SwooleClient;
$request = Request::create('/foo.txt', 'GET');
$context = new RequestContext([
'publicPath' => __DIR__.'/public',
'octaneConfig' => [],
]);
$this->assertTrue($client->canServeRequestAsStaticFile($request, $context));
}
public function test_cant_serve_static_files_if_file_is_outside_public_directory()
{
$client = new SwooleClient;
$request = Request::create('/../foo.txt', 'GET');
$context = new RequestContext([
'publicPath' => __DIR__.'/public/files',
'octaneConfig' => [],
]);
$this->assertFalse($client->canServeRequestAsStaticFile($request, $context));
}
public function test_cant_serve_static_files_if_file_has_forbidden_extension()
{
$client = new SwooleClient;
$request = Request::create('/foo.php', 'GET');
$context = new RequestContext([
'publicPath' => __DIR__.'/public/files',
'octaneConfig' => [],
]);
$this->assertFalse($client->canServeRequestAsStaticFile($request, $context));
}
/** @doesNotPerformAssertions @test */
public function test_static_file_can_be_served()
{
$client = new SwooleClient;
$request = Request::create('/foo.txt', 'GET');
$context = new RequestContext([
'swooleResponse' => $swooleResponse = Mockery::mock('stdClass'),
'publicPath' => __DIR__.'/public',
'octaneConfig' => [],
]);
$swooleResponse->shouldReceive('status')->once()->with(200);
$swooleResponse->shouldReceive('header')->once()->with('Content-Type', 'text/plain');
$swooleResponse->shouldReceive('sendfile')->once()->with(realpath(__DIR__.'/public/foo.txt'));
$client->serveStaticFile($request, $context);
}
public function test_static_file_headers_can_be_sent()
{
$client = new SwooleClient;
$request = Request::create('/foo.txt', 'GET');
$context = new RequestContext([
'swooleResponse' => $swooleResponse = Mockery::mock('stdClass'),
'publicPath' => __DIR__.'/public',
'octaneConfig' => [
'static_file_headers' => [
'foo.txt' => [
'X-Test-Header' => 'Valid',
],
],
],
]);
$swooleResponse->shouldReceive('status')->once()->with(200);
$swooleResponse->shouldReceive('header')->once()->with('X-Test-Header', 'Valid');
$swooleResponse->shouldReceive('header')->once()->with('Content-Type', 'text/plain');
$swooleResponse->shouldReceive('sendfile')->once()->with(realpath(__DIR__.'/public/foo.txt'));
$client->serveStaticFile($request, $context);
}
public function test_can_serve_static_files_through_symlink()
{
$client = new SwooleClient;
$request = Request::create('/symlink/foo.txt', 'GET');
$context = new RequestContext([
'publicPath' => __DIR__.'/public/files',
'octaneConfig' => [],
]);
$this->assertTrue($client->canServeRequestAsStaticFile($request, $context));
}
public function test_cant_serve_static_files_through_symlink_using_directory_traversal()
{
$client = new SwooleClient;
$request = Request::create('/symlink/../foo.txt', 'GET');
$context = new RequestContext([
'publicPath' => __DIR__.'/public/files',
'octaneConfig' => [],
]);
$this->assertFalse($client->canServeRequestAsStaticFile($request, $context));
}
/** @doesNotPerformAssertions @test */
public function test_respond_method_sends_response_to_swoole()
{
$client = new SwooleClient;
if (extension_loaded('openswoole')) {
$this->markTestSkipped('This test is not compatible with Open Swoole');
}
$swooleResponse = Mockery::mock('Swoole\Http\Response');
$swooleResponse->shouldReceive('status')->once()->with(200);
$swooleResponse->shouldReceive('header')->once()->with('Cache-Control', 'no-cache, private');
$swooleResponse->shouldReceive('header')->once()->with('Content-Type', 'text/html');
$swooleResponse->shouldReceive('header')->once()->with('Date', Mockery::type('string'));
$swooleResponse->shouldReceive('cookie')->once()->with('new', 'value', 0, '/', '', false, true, 'lax');
$swooleResponse->shouldReceive('cookie')->once()->with('cleared', 'deleted', Mockery::type('int'), '/', '', false, true, 'lax');
$swooleResponse->shouldReceive('write')->with('Hello World');
$swooleResponse->shouldReceive('end')->once();
$response = new Response('Hello World', 200, ['Content-Type' => 'text/html']);
$response->cookie('new', 'value');
$response->withoutCookie('cleared');
$client->respond(new RequestContext([
'swooleResponse' => $swooleResponse,
]), new OctaneResponse($response));
}
/** @doesNotPerformAssertions @test */
public function test_respond_method_send_streamed_response_to_swoole()
{
$client = new SwooleClient;
if (extension_loaded('openswoole')) {
$this->markTestSkipped('This test is not compatible with Open Swoole');
}
$swooleResponse = Mockery::mock('Swoole\Http\Response');
$swooleResponse->shouldReceive('status')->once()->with(200);
$swooleResponse->shouldReceive('header')->once()->with('Cache-Control', 'no-cache, private');
$swooleResponse->shouldReceive('header')->once()->with('Content-Type', 'text/html');
$swooleResponse->shouldReceive('header')->once()->with('Date', Mockery::type('string'));
$swooleResponse->shouldReceive('write')->once()->with('Hello World');
$swooleResponse->shouldReceive('end')->once();
$client->respond(new RequestContext([
'swooleResponse' => $swooleResponse,
]), new OctaneResponse(new StreamedResponse(function () {
echo 'Hello World';
}, 200, ['Content-Type' => 'text/html'])));
}
/** @doesNotPerformAssertions @test */
public function test_respond_method_with_laravel_specific_status_code_sends_response_to_swoole()
{
$client = new SwooleClient;
if (extension_loaded('openswoole')) {
$this->markTestSkipped('This test is not compatible with Open Swoole');
}
$swooleResponse = Mockery::mock('Swoole\Http\Response');
$swooleResponse->shouldReceive('status')->once()->with(419, 'Page Expired');
$swooleResponse->shouldReceive('header')->once()->with('Cache-Control', 'no-cache, private');
$swooleResponse->shouldReceive('header')->once()->with('Content-Type', 'text/html');
$swooleResponse->shouldReceive('header')->once()->with('Date', Mockery::type('string'));
$swooleResponse->shouldReceive('write')->with('Hello World');
$swooleResponse->shouldReceive('end')->once();
$client->respond(new RequestContext([
'swooleResponse' => $swooleResponse,
]), new OctaneResponse(new Response('Hello World', 419, ['Content-Type' => 'text/html'])));
}
/** @doesNotPerformAssertions @test */
public function test_error_method_sends_error_response_to_swoole()
{
$client = new SwooleClient;
if (extension_loaded('openswoole')) {
$this->markTestSkipped('This test is not compatible with Open Swoole');
}
$swooleResponse = Mockery::spy('Swoole\Http\Response');
$app = $this->createApplication();
$app['config']['app.debug'] = false;
$request = Request::create('/', 'GET');
$context = new RequestContext(['swooleResponse' => $swooleResponse]);
$client->error(new Exception('Something went wrong...'), $app, $request, $context);
$swooleResponse->shouldHaveReceived('header')->with('Status', '500 Internal Server Error');
$swooleResponse->shouldHaveReceived('header')->with('Content-Type', 'text/plain');
$swooleResponse->shouldHaveReceived('end')->with('Internal server error.');
}
/** @doesNotPerformAssertions @test */
public function test_error_method_sends_detailed_error_response_to_swoole_in_debug_mode()
{
$client = new SwooleClient;
if (extension_loaded('openswoole')) {
$this->markTestSkipped('This test is not compatible with Open Swoole');
}
$swooleResponse = Mockery::spy('Swoole\Http\Response');
$app = $this->createApplication();
$app['config']['app.debug'] = true;
$request = Request::create('/', 'GET');
$context = new RequestContext(['swooleResponse' => $swooleResponse]);
$client->error($e = new Exception('Something went wrong...'), $app, $request, $context);
$swooleResponse->shouldHaveReceived('header')->with('Status', '500 Internal Server Error');
$swooleResponse->shouldHaveReceived('header')->with('Content-Type', 'text/plain');
$swooleResponse->shouldHaveReceived('end')->with((string) $e);
}
}