AspireCloud/app/Http/Controllers/PassThroughController.php
Chuck Adams c7bc95191a
Improve test coverage (#217)
* fix(test): use pg_trgm if not exists

* test: add test for /core/importers

* test: add test for gp-icon and theme screenshot download

* test: inline most test assertion functions into assertExactJson

* test: add hot tags tests

* test: add plugin updates tests

* test: 100% coverage of download routes

* test: 100% coverage of plugin update controller

* test: 100% coverage of all api controllers

* fix: make PassThroughController drop unexpected requests

* refactor: rm special case for / in PassThroughController

* test: auto-fake facades

* test: test PassThroughController for 100% coverage on Controllers

* test: drop auto-fakes because facades are made of hate

* zap: rm unused build-container-images action

* test: add test for AssetCacheHit event

* deps: composer upgrade and bump
2025-03-31 12:53:25 -06:00

57 lines
2 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Client\Response as ClientResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;
use function Safe\json_encode;
class PassThroughController extends Controller
{
public static bool $expectsHit = false; // hack for tests
public function __invoke(Request $request): Response
{
if (app()->environment('testing') && !self::$expectsHit) {
throw new \RuntimeException('Unexpected request to pass-through controller in testing environment');
}
$requestData = $request->all();
$ua = $request->header('User-Agent');
$path = $request->path();
$queryParams = $request->query();
$response = Http::withHeaders(['User-Agent' => $ua, 'Accept' => '*/*'])
->asForm()
->send(
$request->getMethod(),
"https://api.wordpress.org/$path",
['query' => $queryParams, 'form_params' => $requestData],
);
$content = $response->body();
$this->logRequestAndResponse($request, $response, $content);
return response($content, $response->status(), ['Content-Type', $response->header('Content-Type')]);
}
private function logRequestAndResponse(Request $request, ClientResponse $response, string $content): void
{
DB::table('request_data')->insert([
'id' => Str::uuid()->toString(),
'request_path' => $request->path(),
'request_query_params' => json_encode($request->query()),
'request_body' => json_encode($request->all()),
'request_headers' => json_encode($request->headers->all()),
'response_code' => $response->status(),
'response_body' => $content,
'response_headers' => json_encode($response->headers()),
'created_at' => Carbon::now(),
]);
}
}