Skip to content

Commit 1119156

Browse files
fix: convert 302 to 303
1 parent 29e6e07 commit 1119156

File tree

4 files changed

+67
-25
lines changed

4 files changed

+67
-25
lines changed

src/Http/Middleware.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,7 @@ public function __invoke(Request $request, HttpMiddlewareCallable $next): Respon
4949
$response->status === Status::FOUND &&
5050
in_array($request->method, [Method::POST, Method::PUT, Method::PATCH], strict: true)
5151
) {
52-
// TODO: set status to 303
53-
// return new GenericResponse(
54-
// status: Status::SEE_OTHER,
55-
// headers: [
56-
// 'Location' => $response->getHeader('Location'),
57-
// ]
58-
// );
52+
$response->setStatus(Status::SEE_OTHER);
5953
}
6054

6155
return $response;

tests/Fixtures/TestController.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,17 @@
1010
use NeoIsRecursive\Inertia\Props\AlwaysProp;
1111
use Tempest\Http\Responses\Ok;
1212
use Tempest\Http\Responses\Redirect;
13-
use Tempest\Http\Session\Session;
1413
use Tempest\Router\Get;
14+
use Tempest\Router\Patch;
15+
use Tempest\Router\Post;
16+
use Tempest\Router\Put;
1517

1618
use function NeoIsRecursive\Inertia\inertia;
1719
use function Tempest\uri;
1820

1921
final readonly class TestController
2022
{
23+
#[Get(uri: '/')]
2124
public function index(): InertiaResponse
2225
{
2326
return inertia(component: 'Index');
@@ -84,4 +87,22 @@ public function testRedirectWithClearHistory(Inertia $inertia): Redirect
8487
$inertia->clearHistory();
8588
return new Redirect(to: uri([self::class, 'testCanSharePropsFromAnyWhere']));
8689
}
90+
91+
#[Post(uri: '/test-post-with-redirect')]
92+
public function testPostWithRedirect(): Redirect
93+
{
94+
return new Redirect(to: uri([static::class, 'index']));
95+
}
96+
97+
#[Patch(uri: '/test-patch-with-redirect')]
98+
public function testPatchWithRedirect(): Redirect
99+
{
100+
return new Redirect(to: uri([static::class, 'index']));
101+
}
102+
103+
#[Put(uri: '/test-put-with-redirect')]
104+
public function testPutWithRedirect(): Redirect
105+
{
106+
return new Redirect(to: uri([static::class, 'index']));
107+
}
87108
}

tests/Integration/HistoryTest.php

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,6 @@ public function test_the_history_can_be_encrypted_via_middleware(): void
6666
// ]);
6767
}
6868

69-
public function test_the_history_can_be_encrypted_via_middleware_alias(): void
70-
{
71-
static::markTestIncomplete(message: 'This test is incomplete and needs to be fixed.');
72-
73-
// Route::middleware([StartSession::class, ExampleMiddleware::class, 'inertia.encrypt'])->get('/', function () {
74-
// return Inertia::render('User/Edit');
75-
// });
76-
// $response = $this->withoutExceptionHandling()->get('/', [
77-
// 'X-Inertia' => 'true',
78-
// ]);
79-
// $response->assertSuccessful();
80-
// $response->assertJson([
81-
// 'component' => 'User/Edit',
82-
// 'encryptHistory' => true,
83-
// ]);
84-
}
85-
8669
public function test_the_history_can_be_encrypted_globally(): void
8770
{
8871
static::markTestIncomplete(message: 'This test is incomplete and needs to be fixed.');
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace NeoIsRecursive\Inertia\Tests\Integration;
6+
7+
use NeoIsRecursive\Inertia\Tests\Fixtures\TestController;
8+
use NeoIsRecursive\Inertia\Tests\TestCase;
9+
use PHPUnit\Framework\Attributes\TestWith;
10+
use Tempest\Http\Status;
11+
12+
use function Tempest\uri;
13+
14+
final class MiddlewareTest extends TestCase
15+
{
16+
public function test_middleware_is_registered(): void
17+
{
18+
$middleware = $this->container->get(\NeoIsRecursive\Inertia\Http\Middleware::class);
19+
20+
static::assertInstanceOf(\NeoIsRecursive\Inertia\Http\Middleware::class, $middleware);
21+
}
22+
23+
#[TestWith([
24+
'testPostWithRedirect',
25+
'post',
26+
], name: 'post')]
27+
#[TestWith([
28+
'testPutWithRedirect',
29+
'put',
30+
], name: 'put')]
31+
#[TestWith([
32+
'testPatchWithRedirect',
33+
'patch',
34+
], name: 'patch')]
35+
public function test_middleware_converts_post_put_or_patch_302_to_303(string $action, string $method): void
36+
{
37+
$response = $this->http->{$method}(uri([TestController::class, $action]), headers: [
38+
'X-Inertia' => 'true',
39+
'X-Version' => '1.0',
40+
]);
41+
42+
$response->assertStatus(Status::SEE_OTHER);
43+
}
44+
}

0 commit comments

Comments
 (0)