1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
<?php
namespace Illuminate\Tests\Integration\Foundation;
use Illuminate\Foundation\Console\DownCommand;
use Illuminate\Foundation\Console\UpCommand;
use Illuminate\Foundation\Events\MaintenanceModeDisabled;
use Illuminate\Foundation\Events\MaintenanceModeEnabled;
use Illuminate\Foundation\Http\MaintenanceModeBypassCookie;
use Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Route;
use Orchestra\Testbench\TestCase;
use Symfony\Component\HttpFoundation\Cookie;
class MaintenanceModeTest extends TestCase
{
protected function tearDown(): void
{
@unlink(storage_path('framework/down'));
}
public function testBasicMaintenanceModeResponse()
{
file_put_contents(storage_path('framework/down'), json_encode([
'retry' => 60,
'refresh' => 60,
]));
Route::get('/foo', function () {
return 'Hello World';
})->middleware(PreventRequestsDuringMaintenance::class);
$response = $this->get('/foo');
$response->assertStatus(503);
$response->assertHeader('Retry-After', '60');
$response->assertHeader('Refresh', '60');
}
public function testMaintenanceModeCanHaveCustomStatus()
{
file_put_contents(storage_path('framework/down'), json_encode([
'retry' => 60,
'status' => 200,
]));
Route::get('/foo', function () {
return 'Hello World';
})->middleware(PreventRequestsDuringMaintenance::class);
$response = $this->get('/foo');
$response->assertStatus(200);
$response->assertHeader('Retry-After', '60');
}
public function testMaintenanceModeCanHaveCustomTemplate()
{
file_put_contents(storage_path('framework/down'), json_encode([
'retry' => 60,
'template' => 'Rendered Content',
]));
Route::get('/foo', function () {
return 'Hello World';
})->middleware(PreventRequestsDuringMaintenance::class);
$response = $this->get('/foo');
$response->assertStatus(503);
$response->assertHeader('Retry-After', '60');
$this->assertSame('Rendered Content', $response->original);
}
public function testMaintenanceModeCanRedirectWithBypassCookie()
{
file_put_contents(storage_path('framework/down'), json_encode([
'retry' => 60,
'secret' => 'foo',
'template' => 'Rendered Content',
]));
Route::get('/foo', function () {
return 'Hello World';
})->middleware(PreventRequestsDuringMaintenance::class);
$response = $this->get('/foo');
$response->assertStatus(302);
$response->assertCookie('laravel_maintenance');
}
public function testMaintenanceModeCanBeBypassedWithValidCookie()
{
file_put_contents(storage_path('framework/down'), json_encode([
'retry' => 60,
'secret' => 'foo',
]));
$cookie = MaintenanceModeBypassCookie::create('foo');
Route::get('/test', function () {
return 'Hello World';
})->middleware(PreventRequestsDuringMaintenance::class);
$response = $this->withUnencryptedCookies([
'laravel_maintenance' => $cookie->getValue(),
])->get('/test');
$response->assertStatus(200);
$this->assertSame('Hello World', $response->original);
}
public function testMaintenanceModeCantBeBypassedWithInvalidCookie()
{
file_put_contents(storage_path('framework/down'), json_encode([
'retry' => 60,
'secret' => 'foo',
]));
$cookie = MaintenanceModeBypassCookie::create('test-key');
Route::get('/test', function () {
return 'Hello World';
})->middleware(PreventRequestsDuringMaintenance::class);
$response = $this->withUnencryptedCookies([
'laravel_maintenance' => $cookie->getValue(),
])->get('/test');
$response->assertStatus(503);
}
public function testCanCreateBypassCookies()
{
$cookie = MaintenanceModeBypassCookie::create('test-key');
$this->assertInstanceOf(Cookie::class, $cookie);
$this->assertSame('laravel_maintenance', $cookie->getName());
$this->assertTrue(MaintenanceModeBypassCookie::isValid($cookie->getValue(), 'test-key'));
$this->assertFalse(MaintenanceModeBypassCookie::isValid($cookie->getValue(), 'wrong-key'));
Carbon::setTestNow(now()->addMonths(6));
$this->assertFalse(MaintenanceModeBypassCookie::isValid($cookie->getValue(), 'test-key'));
Carbon::setTestNow(null);
}
public function testDispatchEventWhenMaintenanceModeIsEnabled()
{
Event::fake();
Event::assertNotDispatched(MaintenanceModeEnabled::class);
$this->artisan(DownCommand::class);
Event::assertDispatched(MaintenanceModeEnabled::class);
}
public function testDispatchEventWhenMaintenanceModeIsDisabled()
{
file_put_contents(storage_path('framework/down'), json_encode([
'retry' => 60,
'refresh' => 60,
]));
Event::fake();
Event::assertNotDispatched(MaintenanceModeDisabled::class);
$this->artisan(UpCommand::class);
Event::assertDispatched(MaintenanceModeDisabled::class);
}
}
|