Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/App/Traits/WebAppTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,21 @@ private function handleCors(Response $response): void
*/
private function sendResponse(Response $response): void
{
$this->handleCors($response);
$response->send();
try {
$this->handleCors($response);
$response->send();
} finally {
$this->cleanupRequestContext();
}
}

/**
* Clears request-scoped state after the response has been sent.
*/
private function cleanupRequestContext(): void
{
request()->setMatchedRoute(null);
request()->flush();
response()->flush();
}
}
2 changes: 1 addition & 1 deletion src/Module/Templates/DemoApi/src/Middlewares/Auth.php.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Auth extends BaseMiddleware
public function apply(Request $request, Closure $next): Response
{
if (!auth()->check()) {
$this->respondWithError($request,
return $this->respondWithError($request,
t('validation.unauthorizedRequest'),
StatusCode::UNAUTHORIZED
);
Expand Down
2 changes: 1 addition & 1 deletion src/Module/Templates/DemoWeb/src/Middlewares/Auth.php.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Auth extends Middleware
public function apply(Request $request, Closure $next): Response
{
if (!auth()->check()) {
redirect(base_url(true) . '/' . current_lang() . '/signin');
return redirect(base_url(true) . '/' . current_lang() . '/signin');
}

return $next($request);
Expand Down
50 changes: 50 additions & 0 deletions tests/Unit/App/Adapters/WebAppAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace Quantum\Tests\Unit\App\Adapters;

use Quantum\Http\Exceptions\HttpException;
use Quantum\App\Adapters\WebAppAdapter;
use Quantum\Tests\Unit\AppTestCase;
use Quantum\Router\Route;
use Throwable;

class WebAppAdapterTest extends AppTestCase
{
Expand All @@ -29,6 +32,11 @@ public function testWebAppAdapterStartSuccessfully(): void
ob_end_clean();

$this->assertEquals(0, $result);
$this->assertNull(request()->getMatchedRoute());
$this->assertNull(request()->getUri());
$this->assertSame([], response()->all());
$this->assertSame([], response()->allHeaders());
$this->assertSame(200, response()->getStatusCode());
}

public function testWebAppAdapterStartFails(): void
Expand All @@ -40,6 +48,11 @@ public function testWebAppAdapterStartFails(): void
ob_end_clean();

$this->assertSame(0, $result);
$this->assertNull(request()->getMatchedRoute());
$this->assertNull(request()->getUri());
$this->assertSame([], response()->all());
$this->assertSame([], response()->allHeaders());
$this->assertSame(200, response()->getStatusCode());
}

public function testWebAppAdapterHandlesPageNotFoundGracefully(): void
Expand All @@ -51,5 +64,42 @@ public function testWebAppAdapterHandlesPageNotFoundGracefully(): void
ob_end_clean();

$this->assertSame(0, $result);
$this->assertNull(request()->getMatchedRoute());
$this->assertNull(request()->getUri());
$this->assertSame([], response()->all());
$this->assertSame([], response()->allHeaders());
$this->assertSame(200, response()->getStatusCode());
}

public function testWebAppAdapterCleansUpOnException(): void
{
request()->create('GET', '/test/am/tests');
request()->setMatchedRoute(null);
request()->setMatchedRoute(new \Quantum\Router\MatchedRoute(
new Route(['GET'], '/test/am/tests', 'TestController', 'tests'),
[]
));
response()->setHeader('X-Test', '1');
response()->json(['foo' => 'bar']);

$throwingResponse = new class () extends \Quantum\Http\Response {
public function send(): void
{
throw new HttpException('boom');
}
};

try {
$this->invokePrivateMethod($this->webAppAdapter, 'sendResponse', [$throwingResponse]);
$this->fail('Expected response sending to fail.');
} catch (Throwable $exception) {
$this->assertInstanceOf(HttpException::class, $exception);
}

$this->assertNull(request()->getMatchedRoute());
$this->assertNull(request()->getUri());
$this->assertSame([], response()->all());
$this->assertSame([], response()->allHeaders());
$this->assertSame(200, response()->getStatusCode());
}
}
Loading