|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 6 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | + */ |
| 8 | + |
| 9 | +namespace Test; |
| 10 | + |
| 11 | +use GuzzleHttp\Handler\CurlHandler; |
| 12 | +use GuzzleHttp\Promise\Create; |
| 13 | +use GuzzleHttp\Promise\PromiseInterface; |
| 14 | +use OC\Http\Client\ClientService; |
| 15 | +use OCP\Http\Client\IClient; |
| 16 | +use OCP\Http\Client\IClientService; |
| 17 | +use OCP\Server; |
| 18 | +use Psr\Http\Message\RequestInterface; |
| 19 | +use Psr\Http\Message\ResponseInterface; |
| 20 | + |
| 21 | +/** |
| 22 | + * Records the outgoing HTTP requests of a test run for tests/http-analyzer.php. |
| 23 | + * Enable by setting TEST_LOG_HTTP to the log file path. |
| 24 | + * |
| 25 | + * Covers IClientService only: a library building its own client or calling curl |
| 26 | + * directly stays invisible, as does any test that mocks IClientService. |
| 27 | + */ |
| 28 | +final class HttpRequestLogger implements IClientService { |
| 29 | + private const RUN_MARKER = 'TEST_LOG_HTTP_RUN'; |
| 30 | + |
| 31 | + private function __construct( |
| 32 | + private IClientService $inner, |
| 33 | + private string $logFile, |
| 34 | + ) { |
| 35 | + } |
| 36 | + |
| 37 | + public static function install(string $logFile): void { |
| 38 | + // Resolving ClientService rather than IClientService avoids recursing into |
| 39 | + // this decorator, and keeps the service lazy. |
| 40 | + /** @psalm-suppress InternalMethod */ |
| 41 | + \OC::$server->registerService(IClientService::class, static fn (): IClientService |
| 42 | + => new self(Server::get(ClientService::class), $logFile)); |
| 43 | + |
| 44 | + // A test running in a separate process re-runs the bootstrap, so only the |
| 45 | + // process that owns the run may truncate. Children inherit the marker. |
| 46 | + if (getenv(self::RUN_MARKER) === false) { |
| 47 | + putenv(self::RUN_MARKER . '=' . getmypid()); |
| 48 | + file_put_contents($logFile, ''); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + #[\Override] |
| 53 | + public function newClient(?callable $handler = null): IClient { |
| 54 | + $next = $handler ?? new CurlHandler(); |
| 55 | + |
| 56 | + return $this->inner->newClient( |
| 57 | + function (RequestInterface $request, array $options) use ($next): PromiseInterface { |
| 58 | + $start = microtime(true); |
| 59 | + |
| 60 | + return $next($request, $options)->then( |
| 61 | + function (ResponseInterface $response) use ($request, $start): ResponseInterface { |
| 62 | + $this->record($request, $start, (string)$response->getStatusCode()); |
| 63 | + return $response; |
| 64 | + }, |
| 65 | + function (mixed $reason) use ($request, $start): PromiseInterface { |
| 66 | + $this->record($request, $start, 'error'); |
| 67 | + return Create::rejectionFor($reason); |
| 68 | + }, |
| 69 | + ); |
| 70 | + }, |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + private function record(RequestInterface $request, float $start, string $outcome): void { |
| 75 | + $line = json_encode([ |
| 76 | + 'test' => self::currentTest(), |
| 77 | + 'method' => $request->getMethod(), |
| 78 | + 'uri' => (string)$request->getUri(), |
| 79 | + 'outcome' => $outcome, |
| 80 | + 'duration' => round(microtime(true) - $start, 6), |
| 81 | + ], JSON_INVALID_UTF8_SUBSTITUTE | JSON_UNESCAPED_SLASHES); |
| 82 | + |
| 83 | + if ($line !== false) { |
| 84 | + file_put_contents($this->logFile, $line . "\n", FILE_APPEND); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /** PHPUnit exposes no global for the running test, so walk the stack for it. */ |
| 89 | + private static function currentTest(): string { |
| 90 | + $test = '(unknown)'; |
| 91 | + |
| 92 | + foreach (debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS) as $frame) { |
| 93 | + $class = $frame['class'] ?? ''; |
| 94 | + $function = $frame['function'] ?? ''; |
| 95 | + |
| 96 | + if ($class !== '' |
| 97 | + && str_starts_with($function, 'test') |
| 98 | + && is_subclass_of($class, \PHPUnit\Framework\TestCase::class) |
| 99 | + ) { |
| 100 | + $test = $class . '::' . $function; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return $test; |
| 105 | + } |
| 106 | +} |
0 commit comments