|
1 | 1 | <?php |
2 | 2 |
|
3 | 3 | use Codedor\FilamentRedirects\Filament\RedirectResource\Pages\ManageRedirects; |
| 4 | +use Codedor\FilamentRedirects\Http\Middleware\Redirects; |
4 | 5 | use Codedor\FilamentRedirects\Models\Redirect; |
5 | 6 | use Filament\Notifications\Notification; |
| 7 | +use Illuminate\Http\Request; |
6 | 8 | use Illuminate\Support\Facades\Storage; |
7 | 9 |
|
8 | 10 | use function Pest\Livewire\livewire; |
9 | 11 |
|
10 | 12 | beforeEach(function () { |
11 | 13 | $this->redirects = Redirect::factory()->createMany([ |
12 | 14 | [ |
13 | | - 'from' => '/one', |
14 | | - 'to' => '/two', |
| 15 | + 'from' => 'http://example.com/one', |
| 16 | + 'to' => 'http://example.com/two', |
15 | 17 | ], |
16 | 18 | [ |
17 | | - 'from' => '/foo', |
18 | | - 'to' => '/bar', |
| 19 | + 'from' => 'https://example.com/foo', |
| 20 | + 'to' => 'https://example.com/bar', |
19 | 21 | ], |
20 | 22 | ]); |
21 | 23 |
|
|
66 | 68 |
|
67 | 69 | $this->assertDatabaseCount(Redirect::class, 3); |
68 | 70 | $this->assertDatabaseHas(Redirect::class, [ |
69 | | - 'from' => '/from', |
70 | | - 'to' => '/to', |
| 71 | + 'from' => 'https://example.com/from', |
| 72 | + 'to' => 'https://example.com/to', |
71 | 73 | 'status' => 301, |
72 | 74 | ]); |
73 | 75 | }); |
|
85 | 87 | livewire(ManageRedirects::class) |
86 | 88 | ->assertActionExists('create') |
87 | 89 | ->callAction('create', [ |
88 | | - 'from' => '/from', |
89 | | - 'to' => '/to', |
| 90 | + 'from' => 'https://example.com/from', |
| 91 | + 'to' => 'https://example.com/to', |
90 | 92 | 'status' => 410, |
91 | 93 | ]) |
92 | 94 | ->assertHasNoActionErrors(); |
93 | 95 |
|
94 | 96 | $this->assertDatabaseCount(Redirect::class, 3); |
95 | 97 | $this->assertDatabaseHas(Redirect::class, [ |
96 | | - 'from' => '/from', |
97 | | - 'to' => '/to', |
| 98 | + 'from' => 'https://example.com/from', |
| 99 | + 'to' => 'https://example.com/to', |
98 | 100 | 'status' => 410, |
99 | 101 | ]); |
100 | 102 | }); |
| 103 | + |
| 104 | +it('can create a redirect with validation errors for invalid URLs', function () { |
| 105 | + livewire(ManageRedirects::class) |
| 106 | + ->assertActionExists('create') |
| 107 | + ->callAction('create', [ |
| 108 | + 'from' => 'invalid-url', |
| 109 | + 'to' => 'another-invalid-url', |
| 110 | + ]) |
| 111 | + ->assertHasActionErrors(['from' => 'url', 'to' => 'url']); |
| 112 | +}); |
| 113 | + |
| 114 | +it('can create a redirect with different protocols', function () { |
| 115 | + livewire(ManageRedirects::class) |
| 116 | + ->assertActionExists('create') |
| 117 | + ->callAction('create', [ |
| 118 | + 'from' => 'http://example.com/old', |
| 119 | + 'to' => 'https://example.com/new', |
| 120 | + 'status' => 301, |
| 121 | + ]) |
| 122 | + ->assertHasNoActionErrors(); |
| 123 | + |
| 124 | + $this->assertDatabaseHas(Redirect::class, [ |
| 125 | + 'from' => 'http://example.com/old', |
| 126 | + 'to' => 'https://example.com/new', |
| 127 | + 'status' => 301, |
| 128 | + ]); |
| 129 | +}); |
| 130 | + |
| 131 | +it('redirects correctly regardless of protocol', function ($fromProtocol, $toProtocol) { |
| 132 | + $redirect = Redirect::create([ |
| 133 | + 'from' => "{$fromProtocol}://example.com/test", |
| 134 | + 'to' => "{$toProtocol}://example.com/result", |
| 135 | + 'status' => 301, |
| 136 | + 'online' => true, |
| 137 | + ]); |
| 138 | + |
| 139 | + $request = Request::create($redirect->from); |
| 140 | + |
| 141 | + $middleware = new Redirects; |
| 142 | + $response = $middleware->handle($request, fn () => response('This is a secret place')); |
| 143 | + |
| 144 | + expect($response->getStatusCode()) |
| 145 | + ->toBe(301) |
| 146 | + ->and($response->headers->get('Location')) |
| 147 | + ->toBe($redirect->to); |
| 148 | + |
| 149 | + $redirect->from = $fromProtocol === 'http' |
| 150 | + ? 'https://example.com/test' |
| 151 | + : 'http://example.com/test'; |
| 152 | + |
| 153 | + $request = Request::create($redirect->from); |
| 154 | + |
| 155 | + $response = $middleware->handle($request, fn () => response('This is a secret place')); |
| 156 | + |
| 157 | + expect($response->getStatusCode()) |
| 158 | + ->toBe(301) |
| 159 | + ->and($response->headers->get('Location')) |
| 160 | + ->toBe($redirect->to); |
| 161 | +}) |
| 162 | + ->with([ |
| 163 | + 'http to http' => ['http', 'http'], |
| 164 | + 'http to https' => ['http', 'https'], |
| 165 | + 'https to http' => ['https', 'http'], |
| 166 | + 'https to https' => ['https', 'https'], |
| 167 | + ]); |
| 168 | + |
| 169 | +it('redirects correctly with query parameters', function () { |
| 170 | + $redirect = Redirect::create([ |
| 171 | + 'from' => 'http://example.com/query', |
| 172 | + 'to' => 'https://example.com/result', |
| 173 | + 'status' => 301, |
| 174 | + 'pass_query_string' => true, |
| 175 | + 'online' => true, |
| 176 | + ]); |
| 177 | + |
| 178 | + $query = '?param=value'; |
| 179 | + $request = Request::create("{$redirect->from}{$query}", 'GET'); |
| 180 | + |
| 181 | + $middleware = new Redirects; |
| 182 | + $response = $middleware->handle($request, fn () => response('This is a secret place')); |
| 183 | + |
| 184 | + expect($response->getStatusCode()) |
| 185 | + ->toBe(301) |
| 186 | + ->and($response->headers->get('Location')) |
| 187 | + ->toBe("{$redirect->to}{$query}"); |
| 188 | +}); |
| 189 | + |
| 190 | +it('handles wildcard redirects', function () { |
| 191 | + $redirect = Redirect::create([ |
| 192 | + 'from' => 'http://example.com/wildcard*', |
| 193 | + 'to' => 'https://example.com/result', |
| 194 | + 'status' => 301, |
| 195 | + 'online' => true, |
| 196 | + ]); |
| 197 | + |
| 198 | + $request = Request::create('http://example.com/wildcard-test', 'GET'); |
| 199 | + |
| 200 | + $middleware = new Redirects; |
| 201 | + $response = $middleware->handle($request, fn () => response('This is a secret place')); |
| 202 | + |
| 203 | + expect($response->getStatusCode()) |
| 204 | + ->toBe(301) |
| 205 | + ->and($response->headers->get('Location')) |
| 206 | + ->toBe($redirect->to); |
| 207 | +}); |
0 commit comments