Skip to content

Commit

Permalink
[2.x] Fixes Undefined variable $response while using bypass cookie (#…
Browse files Browse the repository at this point in the history
…114)

* Fixes `Undefined variable $response` while using bypass cookie

* move method

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
nunomaduro and taylorotwell authored Dec 16, 2021
1 parent 553d210 commit 7ea7c00
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 25 deletions.
49 changes: 37 additions & 12 deletions src/Runtime/Octane/Octane.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ public static function handle($request)
$_ENV['VAPOR_MAINTENANCE_MODE_SECRET'] == $request->path()) {
$response = HttpKernel::bypassResponse($_ENV['VAPOR_MAINTENANCE_MODE_SECRET']);
} elseif (isset($_ENV['VAPOR_MAINTENANCE_MODE_SECRET']) &&
HttpKernel::hasValidBypassCookie($request, $_ENV['VAPOR_MAINTENANCE_MODE_SECRET'])) {
static::hasValidBypassCookie($request, $_ENV['VAPOR_MAINTENANCE_MODE_SECRET'])) {
$response = static::sendRequest($request, $context);
} elseif ($request->wantsJson() && file_exists($_ENV['LAMBDA_TASK_ROOT'].'/503.json')) {
$response = JsonResponse::fromJsonString(
file_get_contents($_ENV['LAMBDA_TASK_ROOT'].'/503.json'), 503
Expand All @@ -154,17 +155,7 @@ public static function handle($request)
);
}
} else {
$response = (new Pipeline)->send($request)
->through([
new EnsureOnNakedDomain,
new RedirectStaticAssets,
new EnsureVanityUrlIsNotIndexed,
new EnsureBinaryEncoding(),
])->then(function ($request) use ($context) {
static::$worker->handle($request, $context);

return static::$response->response;
});
$response = static::sendRequest($request, $context);
}

$content = $response instanceof BinaryFileResponse
Expand All @@ -180,6 +171,40 @@ public static function handle($request)
});
}

/**
* Send the request to the worker.
*
* @param \Illuminate\Http\Request $request
* @param \Laravel\Octane\RequestContext $context
* @return \Laravel\Octane\OctaneResponse
*/
protected static function sendRequest($request, $context)
{
return (new Pipeline)->send($request)
->through([
new EnsureOnNakedDomain,
new RedirectStaticAssets,
new EnsureVanityUrlIsNotIndexed,
new EnsureBinaryEncoding(),
])->then(function ($request) use ($context) {
static::$worker->handle($request, $context);

return static::$response->response;
});
}

/**
* Determine if the incoming request has a maintenance mode bypass cookie.
*
* @param \Illuminate\Http\Request $request
* @param string $secret
* @return bool
*/
public static function hasValidBypassCookie($request, $secret)
{
return HttpKernel::hasValidBypassCookie($request, $secret);
}

/**
* Terminates an Octane worker instance, if any.
*
Expand Down
75 changes: 69 additions & 6 deletions tests/Feature/LoadBalancedOctaneHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ public function test_response_file()
'path' => '/',
]);

self::assertEquals(['text/javascript'], $response->toApiGatewayFormat()['multiValueHeaders']['Content-Type']);
self::assertEquals("console.log();\n", $response->toApiGatewayFormat()['body']);
static::assertEquals(['text/javascript'], $response->toApiGatewayFormat()['multiValueHeaders']['Content-Type']);
static::assertEquals("console.log();\n", $response->toApiGatewayFormat()['body']);
}

public function test_response_download()
Expand All @@ -116,8 +116,8 @@ public function test_response_download()
'path' => '/',
]);

self::assertEquals(['attachment; filename=asset.js'], $response->toApiGatewayFormat()['multiValueHeaders']['Content-Disposition']);
self::assertEquals("console.log();\n", $response->toApiGatewayFormat()['body']);
static::assertEquals(['attachment; filename=asset.js'], $response->toApiGatewayFormat()['multiValueHeaders']['Content-Disposition']);
static::assertEquals("console.log();\n", $response->toApiGatewayFormat()['body']);
}

public function test_response_headers()
Expand Down Expand Up @@ -238,8 +238,8 @@ public function test_maintenance_mode()
],
]);

self::assertEquals(['application/json'], $response->toApiGatewayFormat()['multiValueHeaders']['Content-Type']);
self::assertEquals(['hello' => 'world'], json_decode($response->toApiGatewayFormat()['body'], true));
static::assertEquals(['application/json'], $response->toApiGatewayFormat()['multiValueHeaders']['Content-Type']);
static::assertEquals(['message' => 'We are currently down for maintenance.'], json_decode($response->toApiGatewayFormat()['body'], true));
}

public function test_request_body()
Expand Down Expand Up @@ -403,4 +403,67 @@ public function test_request_headers()

static::assertEquals(['my-header-value'], json_decode($body, true)['my-header']);
}

public function test_maintenance_mode_with_valid_bypass_cookie()
{
$octane = new class() extends Octane
{
public static function hasValidBypassCookie($request, $secret)
{
return true;
}
};

$handler = new class() extends LoadBalancedOctaneHandler
{
public function request($event)
{
return parent::request($event);
}

public function response($response)
{
return parent::response($response);
}
};

$_ENV['VAPOR_MAINTENANCE_MODE'] = 'true';
$_ENV['APP_VANITY_URL'] = 'production.com';
$_ENV['VAPOR_MAINTENANCE_MODE_SECRET'] = 'my-secret';

Route::get('/', function () {
return 'Hello World';
});

$response = $handler->response($octane::handle($handler->request([
'httpMethod' => 'GET',
'path' => '/',
])));

static::assertEquals('Hello World', $response->toApiGatewayFormat()['body']);
}

public function test_maintenance_mode_with_invalid_bypass_cookie()
{
$handler = new LoadBalancedOctaneHandler();

$_ENV['VAPOR_MAINTENANCE_MODE'] = 'true';
$_ENV['APP_VANITY_URL'] = 'production.com';
$_ENV['VAPOR_MAINTENANCE_MODE_SECRET'] = 'my-secret';

Route::get('/', function () {
return 'Hello World';
});

$response = $handler->handle([
'httpMethod' => 'GET',
'path' => '/',
'headers' => [
'Accept' => 'application/json',
],
]);

static::assertEquals(['application/json'], $response->toApiGatewayFormat()['multiValueHeaders']['Content-Type']);
static::assertEquals(['message' => 'We are currently down for maintenance.'], json_decode($response->toApiGatewayFormat()['body'], true));
}
}
75 changes: 69 additions & 6 deletions tests/Feature/OctaneHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ public function test_response_file()
'path' => '/',
]);

self::assertEquals('text/javascript', $response->toApiGatewayFormat()['headers']['Content-Type']);
self::assertEquals("console.log();\n", $response->toApiGatewayFormat()['body']);
static::assertEquals('text/javascript', $response->toApiGatewayFormat()['headers']['Content-Type']);
static::assertEquals("console.log();\n", $response->toApiGatewayFormat()['body']);
}

public function test_response_download()
Expand All @@ -96,8 +96,8 @@ public function test_response_download()
'path' => '/',
]);

self::assertEquals('attachment; filename=asset.js', $response->toApiGatewayFormat()['headers']['Content-Disposition']);
self::assertEquals("console.log();\n", $response->toApiGatewayFormat()['body']);
static::assertEquals('attachment; filename=asset.js', $response->toApiGatewayFormat()['headers']['Content-Disposition']);
static::assertEquals("console.log();\n", $response->toApiGatewayFormat()['body']);
}

public function test_response_fires_events()
Expand Down Expand Up @@ -240,8 +240,8 @@ public function test_maintenance_mode()
],
]);

self::assertEquals('application/json', $response->toApiGatewayFormat()['headers']['Content-Type']);
self::assertEquals(['hello' => 'world'], json_decode($response->toApiGatewayFormat()['body'], true));
static::assertEquals('application/json', $response->toApiGatewayFormat()['headers']['Content-Type']);
static::assertEquals(['message' => 'We are currently down for maintenance.'], json_decode($response->toApiGatewayFormat()['body'], true));
}

public function test_request_body()
Expand Down Expand Up @@ -405,4 +405,67 @@ public function test_request_headers()

static::assertEquals(['my-token'], json_decode($body, true)['x-xsrf-token']);
}

public function test_maintenance_mode_with_valid_bypass_cookie()
{
$octane = new class() extends Octane
{
public static function hasValidBypassCookie($request, $secret)
{
return true;
}
};

$handler = new class() extends OctaneHandler
{
public function request($event)
{
return parent::request($event);
}

public function response($response)
{
return parent::response($response);
}
};

$_ENV['VAPOR_MAINTENANCE_MODE'] = 'true';
$_ENV['APP_VANITY_URL'] = 'production.com';
$_ENV['VAPOR_MAINTENANCE_MODE_SECRET'] = 'my-secret';

Route::get('/', function () {
return 'Hello World';
});

$response = $handler->response($octane::handle($handler->request([
'httpMethod' => 'GET',
'path' => '/',
])));

static::assertEquals('Hello World', $response->toApiGatewayFormat()['body']);
}

public function test_maintenance_mode_with_invalid_bypass_cookie()
{
$handler = new OctaneHandler();

$_ENV['VAPOR_MAINTENANCE_MODE'] = 'true';
$_ENV['APP_VANITY_URL'] = 'production.com';
$_ENV['VAPOR_MAINTENANCE_MODE_SECRET'] = 'my-secret';

Route::get('/', function () {
return 'Hello World';
});

$response = $handler->handle([
'httpMethod' => 'GET',
'path' => '/',
'headers' => [
'Accept' => 'application/json',
],
]);

static::assertEquals('application/json', $response->toApiGatewayFormat()['headers']['Content-Type']);
static::assertEquals(['message' => 'We are currently down for maintenance.'], json_decode($response->toApiGatewayFormat()['body'], true));
}
}
2 changes: 1 addition & 1 deletion tests/Fixtures/503.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"hello": "world"
"message": "We are currently down for maintenance."
}

0 comments on commit 7ea7c00

Please sign in to comment.