|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of CodeIgniter 4 framework. |
| 5 | + * |
| 6 | + * (c) CodeIgniter Foundation <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view |
| 9 | + * the LICENSE file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace CodeIgniter\HTTP; |
| 13 | + |
| 14 | +use CodeIgniter\HTTP\Exceptions\RedirectException; |
| 15 | +use CodeIgniter\Log\Logger; |
| 16 | +use CodeIgniter\Test\Mock\MockLogger as LoggerConfig; |
| 17 | +use Config\Services; |
| 18 | +use LogicException; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | +use Tests\Support\Log\Handlers\TestHandler; |
| 21 | + |
| 22 | +/** |
| 23 | + * @internal |
| 24 | + * |
| 25 | + * @group Others |
| 26 | + */ |
| 27 | +final class RedirectExceptionTest extends TestCase |
| 28 | +{ |
| 29 | + protected function setUp(): void |
| 30 | + { |
| 31 | + Services::reset(); |
| 32 | + Services::injectMock('logger', new Logger(new LoggerConfig())); |
| 33 | + } |
| 34 | + |
| 35 | + public function testResponse(): void |
| 36 | + { |
| 37 | + $response = (new RedirectException( |
| 38 | + Services::response() |
| 39 | + ->redirect('redirect') |
| 40 | + ->setCookie('cookie', 'value') |
| 41 | + ->setHeader('Redirect-Header', 'value') |
| 42 | + ))->getResponse(); |
| 43 | + |
| 44 | + $this->assertSame('redirect', $response->getHeaderLine('location')); |
| 45 | + $this->assertSame(302, $response->getStatusCode()); |
| 46 | + $this->assertSame('value', $response->getHeaderLine('Redirect-Header')); |
| 47 | + $this->assertSame('value', $response->getCookie('cookie')->getValue()); |
| 48 | + } |
| 49 | + |
| 50 | + public function testResponseWithoutLocation(): void |
| 51 | + { |
| 52 | + $this->expectException(LogicException::class); |
| 53 | + $this->expectExceptionMessage( |
| 54 | + 'The Response object passed to RedirectException does not contain a redirect address.' |
| 55 | + ); |
| 56 | + |
| 57 | + new RedirectException(Services::response()); |
| 58 | + } |
| 59 | + |
| 60 | + public function testResponseWithoutStatusCode(): void |
| 61 | + { |
| 62 | + $response = (new RedirectException(Services::response()->setHeader('Location', 'location')))->getResponse(); |
| 63 | + |
| 64 | + $this->assertSame('location', $response->getHeaderLine('location')); |
| 65 | + $this->assertSame(302, $response->getStatusCode()); |
| 66 | + } |
| 67 | + |
| 68 | + public function testLoggingLocationHeader(): void |
| 69 | + { |
| 70 | + $uri = 'http://location'; |
| 71 | + $expected = 'INFO - ' . date('Y-m-d') . ' --> REDIRECTED ROUTE at ' . $uri; |
| 72 | + $response = (new RedirectException(Services::response()->redirect($uri)))->getResponse(); |
| 73 | + |
| 74 | + $logs = TestHandler::getLogs(); |
| 75 | + |
| 76 | + $this->assertSame($uri, $response->getHeaderLine('Location')); |
| 77 | + $this->assertSame('', $response->getHeaderLine('Refresh')); |
| 78 | + $this->assertSame($expected, $logs[0]); |
| 79 | + } |
| 80 | + |
| 81 | + public function testLoggingRefreshHeader(): void |
| 82 | + { |
| 83 | + $uri = 'http://location'; |
| 84 | + $expected = 'INFO - ' . date('Y-m-d') . ' --> REDIRECTED ROUTE at ' . $uri; |
| 85 | + $response = (new RedirectException(Services::response()->redirect($uri, 'refresh')))->getResponse(); |
| 86 | + |
| 87 | + $logs = TestHandler::getLogs(); |
| 88 | + |
| 89 | + $this->assertSame($uri, substr($response->getHeaderLine('Refresh'), 6)); |
| 90 | + $this->assertSame('', $response->getHeaderLine('Location')); |
| 91 | + $this->assertSame($expected, $logs[0]); |
| 92 | + } |
| 93 | +} |
0 commit comments