-
-
Notifications
You must be signed in to change notification settings - Fork 466
Expand file tree
/
Copy pathRequestIntegrationTest.php
More file actions
506 lines (471 loc) · 18 KB
/
RequestIntegrationTest.php
File metadata and controls
506 lines (471 loc) · 18 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
<?php
declare(strict_types=1);
namespace Sentry\Tests\Integration;
use GuzzleHttp\Psr7\ServerRequest;
use GuzzleHttp\Psr7\UploadedFile;
use GuzzleHttp\Psr7\Utils;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ServerRequestInterface;
use Sentry\ClientInterface;
use Sentry\Event;
use Sentry\Integration\RequestFetcherInterface;
use Sentry\Integration\RequestIntegration;
use Sentry\Options;
use Sentry\SentrySdk;
use Sentry\State\Scope;
use Sentry\UserDataBag;
use function Sentry\withScope;
final class RequestIntegrationTest extends TestCase
{
/**
* @dataProvider invokeDataProvider
*/
public function testInvoke(array $options, ServerRequestInterface $request, array $expectedRequestContextData, ?UserDataBag $initialUser, ?UserDataBag $expectedUser): void
{
$event = Event::createEvent();
$event->setUser($initialUser);
$integration = new RequestIntegration($this->createRequestFetcher($request));
$integration->setupOnce();
/** @var ClientInterface&MockObject $client */
$client = $this->createMock(ClientInterface::class);
$client->expects($this->once())
->method('getIntegration')
->willReturn($integration);
$client->expects($this->once())
->method('getOptions')
->willReturn(new Options($options));
SentrySdk::getCurrentHub()->bindClient($client);
withScope(function (Scope $scope) use ($event, $expectedRequestContextData, $expectedUser): void {
$event = $scope->applyToEvent($event);
$this->assertNotNull($event);
$this->assertSame($expectedRequestContextData, $event->getRequest());
$user = $event->getUser();
if ($expectedUser !== null) {
$this->assertNotNull($user);
$this->assertEquals($expectedUser, $user);
} else {
$this->assertNull($user);
}
});
}
public static function invokeDataProvider(): iterable
{
yield [
[
'send_default_pii' => true,
],
(new ServerRequest('GET', 'http://www.example.com/foo'))
->withCookieParams(['foo' => 'bar']),
[
'url' => 'http://www.example.com/foo',
'method' => 'GET',
'cookies' => [
'foo' => 'bar',
],
'headers' => [
'Host' => ['www.example.com'],
],
],
UserDataBag::createFromUserIdentifier('unique_id'),
UserDataBag::createFromUserIdentifier('unique_id'),
];
yield [
[
'send_default_pii' => false,
],
(new ServerRequest('GET', 'http://www.example.com/foo'))
->withCookieParams(['foo' => 'bar']),
[
'url' => 'http://www.example.com/foo',
'method' => 'GET',
'headers' => [
'Host' => ['www.example.com'],
],
],
null,
null,
];
yield [
[
'send_default_pii' => true,
],
new ServerRequest('GET', 'http://www.example.com:1234/foo'),
[
'url' => 'http://www.example.com:1234/foo',
'method' => 'GET',
'cookies' => [],
'headers' => [
'Host' => ['www.example.com:1234'],
],
],
null,
null,
];
yield [
[
'send_default_pii' => false,
],
new ServerRequest('GET', 'http://www.example.com:1234/foo'),
[
'url' => 'http://www.example.com:1234/foo',
'method' => 'GET',
'headers' => [
'Host' => ['www.example.com:1234'],
],
],
null,
null,
];
yield [
[
'send_default_pii' => true,
],
(new ServerRequest('GET', 'http://www.example.com/foo?foo=bar&bar=baz', [], null, '1.1', ['REMOTE_ADDR' => '127.0.0.1']))
->withHeader('Host', 'www.example.com')
->withHeader('Authorization', 'foo')
->withHeader('Cookie', 'bar')
->withHeader('Set-Cookie', 'baz'),
[
'url' => 'http://www.example.com/foo?foo=bar&bar=baz',
'method' => 'GET',
'query_string' => 'foo=bar&bar=baz',
'env' => [
'REMOTE_ADDR' => '127.0.0.1',
],
'cookies' => [],
'headers' => [
'Host' => ['www.example.com'],
'Authorization' => ['foo'],
'Cookie' => ['bar'],
'Set-Cookie' => ['baz'],
],
],
null,
UserDataBag::createFromUserIpAddress('127.0.0.1'),
];
yield [
[
'send_default_pii' => true,
],
(new ServerRequest('GET', 'http://www.example.com', [], null, '1.1', ['REMOTE_ADDR' => '']))
->withHeader('Host', 'www.example.com'),
[
'url' => 'http://www.example.com',
'method' => 'GET',
'cookies' => [],
'headers' => [
'Host' => ['www.example.com'],
],
],
null,
null,
];
yield [
[
'send_default_pii' => false,
],
(new ServerRequest('GET', 'http://www.example.com/foo?foo=bar&bar=baz', [], null, '1.1', ['REMOTE_ADDR' => '127.0.0.1']))
->withHeader('Host', 'www.example.com')
->withHeader('Authorization', 'foo')
->withHeader('Cookie', 'bar')
->withHeader('Set-Cookie', 'baz'),
[
'url' => 'http://www.example.com/foo?foo=bar&bar=baz',
'method' => 'GET',
'query_string' => 'foo=bar&bar=baz',
'headers' => [
'Host' => ['www.example.com'],
'Authorization' => ['[Filtered]'],
'Cookie' => ['[Filtered]'],
'Set-Cookie' => ['[Filtered]'],
],
],
null,
null,
];
yield [
[
'send_default_pii' => false,
'integrations' => [
new RequestIntegration(null, ['pii_sanitize_headers' => ['aUthOrIzAtIoN']]),
],
],
(new ServerRequest('GET', 'http://www.example.com', [], null, '1.1', ['REMOTE_ADDR' => '127.0.0.1']))
->withHeader('Authorization', 'foo'),
[
'url' => 'http://www.example.com',
'method' => 'GET',
'headers' => [
'Host' => ['www.example.com'],
'Authorization' => ['[Filtered]'],
],
],
null,
null,
];
yield [
[
'max_request_body_size' => 'never',
],
(new ServerRequest('POST', 'http://www.example.com/foo'))
->withHeader('Content-Length', '3')
->withBody(Utils::streamFor('foo')),
[
'url' => 'http://www.example.com/foo',
'method' => 'POST',
'headers' => [
'Host' => ['www.example.com'],
'Content-Length' => ['3'],
],
],
null,
null,
];
yield [
[
'max_request_body_size' => 'small',
],
(new ServerRequest('POST', 'http://www.example.com/foo'))
->withHeader('Content-Length', 10 ** 3)
->withBody(Utils::streamFor('Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus at placerat est. Donec maximus odio augue, vitae bibendum nisi euismod nec. Nunc vel velit ligula. Ut non ultricies magna, non condimentum turpis. Donec pellentesque id nunc at facilisis. Sed fermentum ultricies nunc, id posuere ex ullamcorper quis. Sed varius tincidunt nulla, id varius nulla interdum sit amet. Pellentesque molestie sapien at mi tristique consequat. Nullam id eleifend arcu. Vivamus sed placerat neque. Ut sapien magna, elementum in euismod pretium, rhoncus vitae augue. Nam ullamcorper dui et tortor semper, eu feugiat elit faucibus. Curabitur vel auctor odio. Phasellus vestibulum ullamcorper dictum. Suspendisse fringilla, ipsum bibendum venenatis vulputate, nunc orci facilisis leo, commodo finibus mi arcu in turpis. Mauris ut ultrices est. Nam quis purus ut nulla interdum ornare. Proin in tellus egestas, commodo magna porta, consequat justo. Vivamus in convallis odio. Pellentesque porttitor, urna non gravida.')),
[
'url' => 'http://www.example.com/foo',
'method' => 'POST',
'headers' => [
'Host' => ['www.example.com'],
'Content-Length' => ['1000'],
],
'data' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus at placerat est. Donec maximus odio augue, vitae bibendum nisi euismod nec. Nunc vel velit ligula. Ut non ultricies magna, non condimentum turpis. Donec pellentesque id nunc at facilisis. Sed fermentum ultricies nunc, id posuere ex ullamcorper quis. Sed varius tincidunt nulla, id varius nulla interdum sit amet. Pellentesque molestie sapien at mi tristique consequat. Nullam id eleifend arcu. Vivamus sed placerat neque. Ut sapien magna, elementum in euismod pretium, rhoncus vitae augue. Nam ullamcorper dui et tortor semper, eu feugiat elit faucibus. Curabitur vel auctor odio. Phasellus vestibulum ullamcorper dictum. Suspendisse fringilla, ipsum bibendum venenatis vulputate, nunc orci facilisis leo, commodo finibus mi arcu in turpis. Mauris ut ultrices est. Nam quis purus ut nulla interdum ornare. Proin in tellus egestas, commodo magna porta, consequat justo. Vivamus in convallis odio. Pellentesque porttitor, urna non gravid',
],
null,
null,
];
yield [
[
'max_request_body_size' => 'small',
],
(new ServerRequest('POST', 'http://www.example.com/foo'))
->withHeader('Content-Length', (string) (10 ** 3))
->withParsedBody([
'foo' => 'foo value',
'bar' => 'bar value',
]),
[
'url' => 'http://www.example.com/foo',
'method' => 'POST',
'headers' => [
'Host' => ['www.example.com'],
'Content-Length' => ['1000'],
],
'data' => [
'foo' => 'foo value',
'bar' => 'bar value',
],
],
null,
null,
];
yield [
[
'max_request_body_size' => 'small',
],
(new ServerRequest('POST', 'http://www.example.com/foo'))
->withHeader('Content-Length', (string) (10 ** 3 + 1))
->withParsedBody([
'foo' => 'foo value',
'bar' => 'bar value',
]),
[
'url' => 'http://www.example.com/foo',
'method' => 'POST',
'headers' => [
'Host' => ['www.example.com'],
'Content-Length' => ['1001'],
],
],
null,
null,
];
yield [
[
'max_request_body_size' => 'medium',
],
(new ServerRequest('POST', 'http://www.example.com/foo'))
->withHeader('Content-Length', (string) (10 ** 4))
->withParsedBody([
'foo' => 'foo value',
'bar' => 'bar value',
]),
[
'url' => 'http://www.example.com/foo',
'method' => 'POST',
'headers' => [
'Host' => ['www.example.com'],
'Content-Length' => ['10000'],
],
'data' => [
'foo' => 'foo value',
'bar' => 'bar value',
],
],
null,
null,
];
yield [
[
'max_request_body_size' => 'medium',
],
(new ServerRequest('POST', 'http://www.example.com/foo'))
->withHeader('Content-Length', (string) (10 ** 4 + 1))
->withParsedBody([
'foo' => 'foo value',
'bar' => 'bar value',
]),
[
'url' => 'http://www.example.com/foo',
'method' => 'POST',
'headers' => [
'Host' => ['www.example.com'],
'Content-Length' => ['10001'],
],
],
null,
null,
];
yield [
[
'max_request_body_size' => 'always',
],
(new ServerRequest('POST', 'http://www.example.com/foo'))
->withHeader('Content-Length', '444')
->withUploadedFiles([
'foo' => [
new UploadedFile('foo content', 123, \UPLOAD_ERR_OK, 'foo.ext', 'application/text'),
new UploadedFile('bar content', 321, \UPLOAD_ERR_OK, 'bar.ext', 'application/octet-stream'),
],
]),
[
'url' => 'http://www.example.com/foo',
'method' => 'POST',
'headers' => [
'Host' => ['www.example.com'],
'Content-Length' => ['444'],
],
'data' => [
'foo' => [
[
'client_filename' => 'foo.ext',
'client_media_type' => 'application/text',
'size' => 123,
],
[
'client_filename' => 'bar.ext',
'client_media_type' => 'application/octet-stream',
'size' => 321,
],
],
],
],
null,
null,
];
yield [
[
'max_request_body_size' => 'always',
],
(new ServerRequest('POST', 'http://www.example.com/foo'))
->withHeader('Content-Type', 'application/json')
->withHeader('Content-Length', '23')
->withBody(Utils::streamFor('{"1":"foo","bar":"baz"}')),
[
'url' => 'http://www.example.com/foo',
'method' => 'POST',
'headers' => [
'Host' => ['www.example.com'],
'Content-Type' => ['application/json'],
'Content-Length' => ['23'],
],
'data' => [
'1' => 'foo',
'bar' => 'baz',
],
],
null,
null,
];
yield [
[
'max_request_body_size' => 'always',
],
(new ServerRequest('POST', 'http://www.example.com/foo'))
->withHeader('Content-Type', 'application/json')
->withHeader('Content-Length', '13')
->withBody(Utils::streamFor('{"foo":"bar"}')),
[
'url' => 'http://www.example.com/foo',
'method' => 'POST',
'headers' => [
'Host' => ['www.example.com'],
'Content-Type' => ['application/json'],
'Content-Length' => ['13'],
],
'data' => [
'foo' => 'bar',
],
],
null,
null,
];
yield [
[
'max_request_body_size' => 'always',
],
(new ServerRequest('POST', 'http://www.example.com/foo'))
->withHeader('Content-Type', 'application/json')
->withBody(Utils::streamFor('{"foo":"bar"}')),
[
'url' => 'http://www.example.com/foo',
'method' => 'POST',
'headers' => [
'Host' => ['www.example.com'],
'Content-Type' => ['application/json'],
],
],
null,
null,
];
yield [
[],
(new ServerRequest('GET', 'http://www.example.com/foo'))
->withHeader('123', 'test'),
[
'url' => 'http://www.example.com/foo',
'method' => 'GET',
'headers' => [
'Host' => ['www.example.com'],
'123' => ['test'],
],
],
null,
null,
];
}
private function createRequestFetcher(ServerRequestInterface $request): RequestFetcherInterface
{
return new class($request) implements RequestFetcherInterface {
/**
* @var ServerRequestInterface
*/
private $request;
public function __construct(ServerRequestInterface $request)
{
$this->request = $request;
}
public function fetchRequest(): ServerRequestInterface
{
return $this->request;
}
};
}
}