|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of CodeIgniter 4 framework. |
| 7 | + * |
| 8 | + * (c) CodeIgniter Foundation <admin@codeigniter.com> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view |
| 11 | + * the LICENSE file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CodeIgniter\Filters; |
| 15 | + |
| 16 | +use CodeIgniter\Test\CIUnitTestCase; |
| 17 | +use PHPUnit\Framework\Attributes\Group; |
| 18 | + |
| 19 | +/** |
| 20 | + * @internal |
| 21 | + */ |
| 22 | +#[Group('Others')] |
| 23 | +final class RequestIdFilter extends CIUnitTestCase |
| 24 | +{ |
| 25 | + protected function setUp(): void |
| 26 | + { |
| 27 | + parent::setUp(); |
| 28 | + |
| 29 | + context()->clearAll(); |
| 30 | + } |
| 31 | + |
| 32 | + public function testBefore(): void |
| 33 | + { |
| 34 | + $filter = new RequestId(); |
| 35 | + $request = service('request', null, false); |
| 36 | + |
| 37 | + $filter->before($request); |
| 38 | + |
| 39 | + $requestId = context()->get('request_id'); |
| 40 | + |
| 41 | + $this->assertNotEmpty($requestId); |
| 42 | + $this->assertSame(32, strlen($requestId)); |
| 43 | + $this->assertMatchesRegularExpression('/^[A-Za-z0-9._:-]+$/', $requestId); |
| 44 | + } |
| 45 | + |
| 46 | + public function testBeforeWithExistingRequestId(): void |
| 47 | + { |
| 48 | + $filter = new RequestId(); |
| 49 | + $request = service('request', null, false); |
| 50 | + |
| 51 | + $existingRequestId = 'test-request-id-123'; |
| 52 | + $request->setHeader('X-Request-ID', $existingRequestId); |
| 53 | + |
| 54 | + $filter->before($request); |
| 55 | + |
| 56 | + $requestId = context()->get('request_id'); |
| 57 | + |
| 58 | + $this->assertSame($existingRequestId, $requestId); |
| 59 | + $this->assertMatchesRegularExpression('/^[A-Za-z0-9._:-]+$/', $requestId); |
| 60 | + } |
| 61 | + |
| 62 | + public function testBeforeWithExistingInvalidRequestId(): void |
| 63 | + { |
| 64 | + $filter = new RequestId(); |
| 65 | + $request = service('request', null, false); |
| 66 | + |
| 67 | + $existingRequestId = 'Abc@!#$'; |
| 68 | + $request->setHeader('X-Request-ID', $existingRequestId); |
| 69 | + |
| 70 | + $filter->before($request); |
| 71 | + |
| 72 | + $requestId = context()->get('request_id'); |
| 73 | + |
| 74 | + $this->assertNotSame($existingRequestId, $requestId); |
| 75 | + $this->assertSame(32, strlen($requestId)); |
| 76 | + $this->assertMatchesRegularExpression('/^[A-Za-z0-9._:-]+$/', $requestId); |
| 77 | + } |
| 78 | +} |
0 commit comments