diff --git a/tests/Feature/Filament/RedirectResource/Pages/ManageRedirectsTest.php b/tests/Feature/Filament/RedirectResource/Pages/ManageRedirectsTest.php index 596fd68..f64a946 100644 --- a/tests/Feature/Filament/RedirectResource/Pages/ManageRedirectsTest.php +++ b/tests/Feature/Filament/RedirectResource/Pages/ManageRedirectsTest.php @@ -1,8 +1,10 @@ redirects = Redirect::factory()->createMany([ [ - 'from' => '/one', - 'to' => '/two', + 'from' => 'http://example.com/one', + 'to' => 'http://example.com/two', ], [ - 'from' => '/foo', - 'to' => '/bar', + 'from' => 'https://example.com/foo', + 'to' => 'https://example.com/bar', ], ]); @@ -66,8 +68,8 @@ $this->assertDatabaseCount(Redirect::class, 3); $this->assertDatabaseHas(Redirect::class, [ - 'from' => '/from', - 'to' => '/to', + 'from' => 'https://example.com/from', + 'to' => 'https://example.com/to', 'status' => 301, ]); }); @@ -85,16 +87,123 @@ livewire(ManageRedirects::class) ->assertActionExists('create') ->callAction('create', [ - 'from' => '/from', - 'to' => '/to', + 'from' => 'https://example.com/from', + 'to' => 'https://example.com/to', 'status' => 410, ]) ->assertHasNoActionErrors(); $this->assertDatabaseCount(Redirect::class, 3); $this->assertDatabaseHas(Redirect::class, [ - 'from' => '/from', - 'to' => '/to', + 'from' => 'https://example.com/from', + 'to' => 'https://example.com/to', 'status' => 410, ]); }); + +it('can create a redirect with validation errors for invalid URLs', function () { + livewire(ManageRedirects::class) + ->assertActionExists('create') + ->callAction('create', [ + 'from' => 'invalid-url', + 'to' => 'another-invalid-url', + ]) + ->assertHasActionErrors(['from' => 'url', 'to' => 'url']); +}); + +it('can create a redirect with different protocols', function () { + livewire(ManageRedirects::class) + ->assertActionExists('create') + ->callAction('create', [ + 'from' => 'http://example.com/old', + 'to' => 'https://example.com/new', + 'status' => 301, + ]) + ->assertHasNoActionErrors(); + + $this->assertDatabaseHas(Redirect::class, [ + 'from' => 'http://example.com/old', + 'to' => 'https://example.com/new', + 'status' => 301, + ]); +}); + +dataset('protocol-combinations', [ + 'http to http' => ['http', 'http'], + 'http to https' => ['http', 'https'], + 'https to http' => ['https', 'http'], + 'https to https' => ['https', 'https'], +]); + +it('redirects correctly regardless of protocol', function ($fromProtocol, $toProtocol) { + $redirect = Redirect::create([ + 'from' => "{$fromProtocol}://example.com/test", + 'to' => "{$toProtocol}://example.com/result", + 'status' => 301, + 'online' => true, + ]); + + $request = Request::create($redirect->from); + + $middleware = new Redirects; + $response = $middleware->handle($request, fn () => response('This is a secret place')); + + expect($response->getStatusCode()) + ->toBe(301) + ->and($response->headers->get('Location')) + ->toBe($redirect->to); + + $redirect->from = $fromProtocol === 'http' + ? 'https://example.com/test' + : 'http://example.com/test'; + + $request = Request::create($redirect->from); + + $response = $middleware->handle($request, fn () => response('This is a secret place')); + + expect($response->getStatusCode()) + ->toBe(301) + ->and($response->headers->get('Location')) + ->toBe($redirect->to); +}) + ->with('protocol-combinations'); + +it('redirects correctly with query parameters', function () { + $redirect = Redirect::create([ + 'from' => 'http://example.com/query', + 'to' => 'https://example.com/result', + 'status' => 301, + 'pass_query_string' => true, + 'online' => true, + ]); + + $query = '?param=value'; + $request = Request::create("{$redirect->from}{$query}", 'GET'); + + $middleware = new Redirects; + $response = $middleware->handle($request, fn () => response('This is a secret place')); + + expect($response->getStatusCode()) + ->toBe(301) + ->and($response->headers->get('Location')) + ->toBe("{$redirect->to}{$query}"); +}); + +it('handles wildcard redirects', function () { + $redirect = Redirect::create([ + 'from' => 'http://example.com/wildcard*', + 'to' => 'https://example.com/result', + 'status' => 301, + 'online' => true, + ]); + + $request = Request::create('http://example.com/wildcard-test', 'GET'); + + $middleware = new Redirects; + $response = $middleware->handle($request, fn () => response('This is a secret place')); + + expect($response->getStatusCode()) + ->toBe(301) + ->and($response->headers->get('Location')) + ->toBe($redirect->to); +}); diff --git a/tests/Fixtures/import_redirects.xlsx b/tests/Fixtures/import_redirects.xlsx index 56de90c..8067aa4 100644 Binary files a/tests/Fixtures/import_redirects.xlsx and b/tests/Fixtures/import_redirects.xlsx differ