Skip to content

Commit

Permalink
Return from maintenance middleware early if URL is excluded (#48218)
Browse files Browse the repository at this point in the history
  • Loading branch information
axlon authored Aug 28, 2023
1 parent 40b6059 commit 12b20c3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,18 @@ public function __construct(Application $app)
*/
public function handle($request, Closure $next)
{
if ($this->inExceptArray($request)) {
return $next($request);
}

if ($this->app->maintenanceMode()->active()) {
$data = $this->app->maintenanceMode()->data();

if (isset($data['secret']) && $request->path() === $data['secret']) {
return $this->bypassResponse($data['secret']);
}

if ($this->hasValidBypassCookie($request, $data) ||
$this->inExceptArray($request)) {
if ($this->hasValidBypassCookie($request, $data)) {
return $next($request);
}

Expand Down
27 changes: 27 additions & 0 deletions tests/Integration/Foundation/MaintenanceModeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,19 @@
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Route;
use Orchestra\Testbench\Http\Middleware\PreventRequestsDuringMaintenance as TestbenchPreventRequestsDuringMaintenance;
use Orchestra\Testbench\TestCase;
use Symfony\Component\HttpFoundation\Cookie;

class MaintenanceModeTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();

$this->withoutMiddleware(TestbenchPreventRequestsDuringMaintenance::class);
}

protected function tearDown(): void
{
@unlink(storage_path('framework/down'));
Expand Down Expand Up @@ -113,6 +121,25 @@ public function testMaintenanceModeCanBeBypassedWithValidCookie()
$this->assertSame('Hello World', $response->original);
}

public function testMaintenanceModeCanBeBypassedOnExcludedUrls()
{
$this->app->instance(PreventRequestsDuringMaintenance::class, new class($this->app) extends PreventRequestsDuringMaintenance
{
protected $except = ['/test'];
});

file_put_contents(storage_path('framework/down'), json_encode([
'retry' => 60,
]));

Route::get('/test', fn () => 'Hello World')->middleware(PreventRequestsDuringMaintenance::class);

$response = $this->get('/test');

$response->assertStatus(200);
$this->assertSame('Hello World', $response->original);
}

public function testMaintenanceModeCantBeBypassedWithInvalidCookie()
{
file_put_contents(storage_path('framework/down'), json_encode([
Expand Down

0 comments on commit 12b20c3

Please sign in to comment.