From 2e51fbff9d8472a0e98af39aff52d30f0b67706b Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Wed, 21 Nov 2018 12:17:28 +0100 Subject: Added / route with redirects --- config/routes.php | 4 +++ src/Http/Exceptions/HttpPermanentRedirect.php | 17 ++++++++++++ src/Http/Exceptions/HttpRedirect.php | 23 +++++++++++++++++ src/Http/Exceptions/HttpTemporaryRedirect.php | 17 ++++++++++++ src/Middleware/LegacyMiddleware.php | 3 --- .../Http/Exceptions/HttpPermanentRedirectTest.php | 20 +++++++++++++++ tests/Unit/Http/Exceptions/HttpRedirectTest.php | 26 +++++++++++++++++++ .../Http/Exceptions/HttpTemporaryRedirectTest.php | 20 +++++++++++++++ tests/Unit/Middleware/LegacyMiddlewareTest.php | 30 ++++++++-------------- 9 files changed, 137 insertions(+), 23 deletions(-) create mode 100644 src/Http/Exceptions/HttpPermanentRedirect.php create mode 100644 src/Http/Exceptions/HttpRedirect.php create mode 100644 src/Http/Exceptions/HttpTemporaryRedirect.php create mode 100644 tests/Unit/Http/Exceptions/HttpPermanentRedirectTest.php create mode 100644 tests/Unit/Http/Exceptions/HttpRedirectTest.php create mode 100644 tests/Unit/Http/Exceptions/HttpTemporaryRedirectTest.php diff --git a/config/routes.php b/config/routes.php index b9e3646a..6f61ec71 100644 --- a/config/routes.php +++ b/config/routes.php @@ -1,10 +1,14 @@ get('/', function () { + throw new HttpTemporaryRedirect(auth()->user() ? config('home_site') : 'login'); +}); $route->get('/credits', 'CreditsController@index'); // Stats diff --git a/src/Http/Exceptions/HttpPermanentRedirect.php b/src/Http/Exceptions/HttpPermanentRedirect.php new file mode 100644 index 00000000..a1aa986f --- /dev/null +++ b/src/Http/Exceptions/HttpPermanentRedirect.php @@ -0,0 +1,17 @@ + $url, + ], $headers); + + parent::__construct($statusCode, '', $headers); + } +} diff --git a/src/Http/Exceptions/HttpTemporaryRedirect.php b/src/Http/Exceptions/HttpTemporaryRedirect.php new file mode 100644 index 00000000..ece8d607 --- /dev/null +++ b/src/Http/Exceptions/HttpTemporaryRedirect.php @@ -0,0 +1,17 @@ +path(); $page = str_replace('-', '_', $page); } - if ($page == '/') { - $page = $this->auth->user() ? config('home_site') : 'login'; - } $title = $content = ''; if ( diff --git a/tests/Unit/Http/Exceptions/HttpPermanentRedirectTest.php b/tests/Unit/Http/Exceptions/HttpPermanentRedirectTest.php new file mode 100644 index 00000000..3f6a832c --- /dev/null +++ b/tests/Unit/Http/Exceptions/HttpPermanentRedirectTest.php @@ -0,0 +1,20 @@ +assertInstanceOf(HttpRedirect::class, $exception); + $this->assertEquals(301, $exception->getStatusCode()); + } +} diff --git a/tests/Unit/Http/Exceptions/HttpRedirectTest.php b/tests/Unit/Http/Exceptions/HttpRedirectTest.php new file mode 100644 index 00000000..04190f28 --- /dev/null +++ b/tests/Unit/Http/Exceptions/HttpRedirectTest.php @@ -0,0 +1,26 @@ +assertEquals(302, $exception->getStatusCode()); + $this->assertArraySubset(['Location' => 'https://lorem.ipsum/foo/bar'], $exception->getHeaders()); + + $exception = new HttpRedirect('/test', 301, ['lorem' => 'ipsum']); + $this->assertEquals(301, $exception->getStatusCode()); + $this->assertArraySubset(['lorem' => 'ipsum'], $exception->getHeaders()); + } +} diff --git a/tests/Unit/Http/Exceptions/HttpTemporaryRedirectTest.php b/tests/Unit/Http/Exceptions/HttpTemporaryRedirectTest.php new file mode 100644 index 00000000..6096830f --- /dev/null +++ b/tests/Unit/Http/Exceptions/HttpTemporaryRedirectTest.php @@ -0,0 +1,20 @@ +assertInstanceOf(HttpRedirect::class, $exception); + $this->assertEquals(302, $exception->getStatusCode()); + } +} diff --git a/tests/Unit/Middleware/LegacyMiddlewareTest.php b/tests/Unit/Middleware/LegacyMiddlewareTest.php index 0589340a..f14a38ed 100644 --- a/tests/Unit/Middleware/LegacyMiddlewareTest.php +++ b/tests/Unit/Middleware/LegacyMiddlewareTest.php @@ -46,36 +46,28 @@ class LegacyMiddlewareTest extends TestCase ->disableOriginalConstructor() ->getMock(); - $middleware->expects($this->exactly(2)) + $middleware->expects($this->once()) ->method('loadPage') - ->withConsecutive(['user_worklog'], ['login']) - ->willReturnOnConsecutiveCalls( - ['title', 'content'], - ['title2', 'content2'] - ); + ->with('user_worklog') + ->willReturn(['title', 'content']); - $middleware->expects($this->exactly(3)) + $middleware->expects($this->exactly(2)) ->method('renderPage') ->withConsecutive( ['user_worklog', 'title', 'content'], - ['404', 'Page not found', 'It\'s not available!'], - ['login', 'title2', 'content2'] + ['404', 'Page not found', 'It\'s not available!'] ) ->willReturn($response); - $container->expects($this->exactly(4)) + $container->expects($this->exactly(3)) ->method('get') - ->withConsecutive(['request'], ['request'], ['translator'], ['request']) + ->withConsecutive(['request'], ['request'], ['translator']) ->willReturnOnConsecutiveCalls( $defaultRequest, $defaultRequest, - $translator, - $defaultRequest + $translator ); - $auth->expects($this->atLeastOnce()) - ->method('user') - ->willReturn(false); $auth->expects($this->atLeastOnce()) ->method('can') ->willReturn(false); @@ -92,17 +84,15 @@ class LegacyMiddlewareTest extends TestCase ->method('path') ->willReturn('user-worklog'); - $parameters->expects($this->exactly(3)) + $parameters->expects($this->exactly(2)) ->method('get') ->with('p') ->willReturnOnConsecutiveCalls( null, - 'foo', - '/' + 'foo' ); $middleware->process($request, $handler); $middleware->process($request, $handler); - $middleware->process($request, $handler); } } -- cgit v1.2.3-54-g00ecf