Skip to content

Commit

Permalink
Update changeRedirectCode method in InertiaMiddleware
Browse files Browse the repository at this point in the history
Continue the improvement of the library based on Inertia Laravel Library and what the documentation states. A 409 with a X-Inertia-Location Header should produce an external redirect in the inertia.js library. But for this to happen we need to remove the X-Inertia header, if not, inertia.js won't handle the external redirect.

Add a test for this path inside changeRedirectCode method of the InertiaMiddleware
  • Loading branch information
aiglesiasn committed Jul 10, 2024
1 parent 292af3a commit 434b6b8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/Middleware/InertiaMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ private function changeRedirectCode(Request $request, Response $response): Respo
return $response->withStatus(303);
}

// For External redirects
// https://inertiajs.com/redirects#external-redirects
if (
409 === $response->getStatusCode()
&& $response->hasHeader('X-Inertia-Location')
) {
return $response->withoutHeader('X-Inertia');
}

return $response;
}
}
33 changes: 32 additions & 1 deletion test/Middleware/InertiaMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,35 @@ public function testItChangesResponseCodeTo303WhenRedirectHappensForPutPatchDele

$this->assertSame($response->reveal(), $middleware->process($request->reveal(), $handler->reveal()));
}
}

public function testItRemovesInertiaHeaderForExternalRedirects()
{
$factory = $this->prophesize(InertiaFactoryInterface::class);
$inertia = $this->prophesize(InertiaInterface::class);
$inertia->getVersion()->willReturn('12345');

$request = $this->prophesize(ServerRequestInterface::class);
$request->withAttribute(InertiaMiddleware::INERTIA_ATTRIBUTE, $inertia->reveal())->willReturn($request);
$request->hasHeader('X-Inertia')->willReturn(true);
$request->getHeader('X-Inertia-Version')->willReturn('12345');
$request->getMethod()->willReturn('POST');

$factory->fromRequest($request)->willReturn($inertia);

$response = $this->prophesize(ResponseInterface::class);

$response->withAddedHeader('Vary', 'Accept')->willReturn($response);
$response->withAddedHeader('X-Inertia', 'true')->willReturn($response);
$response->hasHeader('X-Inertia-Location')->willReturn(true);
$response->getStatusCode()->willReturn(409);
$response->withoutHeader('X-Inertia')->shouldBeCalled();
$response->withoutHeader('X-Inertia')->willReturn($response);

$handler = $this->prophesize(RequestHandlerInterface::class);
$handler->handle(Argument::that([$request, 'reveal']))->willReturn($response);

$middleware = new InertiaMiddleware($factory->reveal());

$this->assertSame($response->reveal(), $middleware->process($request->reveal(), $handler->reveal()));
}
}

0 comments on commit 434b6b8

Please sign in to comment.