diff --git a/src/Config.php b/src/Config.php index 4370d15..c1183eb 100644 --- a/src/Config.php +++ b/src/Config.php @@ -51,6 +51,18 @@ public static function getHandler($handler): string )); } + // When the application is running behind a proxy and the TrustedProxy middleware has not been set up yet, + // an error like [HttpRequest.url must start with 'https'] could be thrown. Since the handler URL must + // always be https, we will provide a little extra information on how to fix this. + if ($parse['scheme'] !== 'https') { + throw new Exception(sprintf( + 'Unable to push task to Cloud Tasks because the hander URL is not https. Google Cloud Tasks ' . + 'will only call safe (https) URLs. If you are running Laravel behind a proxy (e.g. Ngrok, Expose), make sure it is ' . + 'as a trusted proxy. To quickly fix this, add the following to the [app/Http/Middleware/TrustProxies] middleware: ' . + 'protected $proxies = \'*\';' + )); + } + // Versions 1.x and 2.x required the full path (e.g. my-app.com/handle-task). In 3.x and beyond // it is no longer necessary to also include the path and simply setting the handler // URL is enough. If someone upgrades and forgets we will warn them here. diff --git a/tests/QueueTest.php b/tests/QueueTest.php index 4a019b9..29a891d 100644 --- a/tests/QueueTest.php +++ b/tests/QueueTest.php @@ -42,7 +42,7 @@ public function a_http_request_with_the_handler_url_is_made() // Assert CloudTasksApi::assertTaskCreated(function (Task $task): bool { - return $task->getHttpRequest()->getUrl() === 'http://docker.for.mac.localhost:8080/handle-task'; + return $task->getHttpRequest()->getUrl() === 'https://docker.for.mac.localhost:8080/handle-task'; }); } @@ -69,7 +69,7 @@ public function it_posts_to_the_handler() public function it_posts_to_the_correct_handler_url() { // Arrange - $this->setConfigValue('handler', 'http://docker.for.mac.localhost:8081'); + $this->setConfigValue('handler', 'https://docker.for.mac.localhost:8081'); CloudTasksApi::fake(); // Act @@ -77,7 +77,7 @@ public function it_posts_to_the_correct_handler_url() // Assert CloudTasksApi::assertTaskCreated(function (Task $task): bool { - return $task->getHttpRequest()->getUrl() === 'http://docker.for.mac.localhost:8081/handle-task'; + return $task->getHttpRequest()->getUrl() === 'https://docker.for.mac.localhost:8081/handle-task'; }); } diff --git a/tests/TestCase.php b/tests/TestCase.php index 9110869..0d994be 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -101,7 +101,7 @@ protected function getEnvironmentSetUp($app) 'queue' => 'barbequeue', 'project' => 'my-test-project', 'location' => 'europe-west6', - 'handler' => env('CLOUD_TASKS_HANDLER', 'http://docker.for.mac.localhost:8080'), + 'handler' => env('CLOUD_TASKS_HANDLER', 'https://docker.for.mac.localhost:8080'), 'service_account_email' => 'info@stackkit.io', ]); $app['config']->set('queue.failed.driver', 'database-uuids'); @@ -216,7 +216,7 @@ protected function addIdTokenToHeader(?Closure $closure = null): void { $base = [ 'iss' => 'https://accounts.google.com', - 'aud' => 'http://docker.for.mac.localhost:8080', + 'aud' => 'https://docker.for.mac.localhost:8080', 'exp' => time() + 10, ];