Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
10 / 10 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| InProcessHttpClient | |
100.00% |
10 / 10 |
|
100.00% |
2 / 2 |
4 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| sendRequest | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace LambdaTwelve\OneRecord\Testing; |
| 6 | |
| 7 | use Psr\Http\Client\ClientInterface; |
| 8 | use Psr\Http\Message\RequestInterface; |
| 9 | use Psr\Http\Message\ResponseInterface; |
| 10 | use Psr\Http\Message\ServerRequestFactoryInterface; |
| 11 | use Psr\Http\Message\StreamFactoryInterface; |
| 12 | use Psr\Http\Server\RequestHandlerInterface; |
| 13 | |
| 14 | /** |
| 15 | * A PSR-18 client that hands every request to a PSR-15 handler in the same |
| 16 | * process: the SDK client talking to a server without a socket. The bearer |
| 17 | * token is passed on as the header HeaderAuthenticator trusts, so tests use |
| 18 | * agent IRIs as tokens. |
| 19 | */ |
| 20 | final class InProcessHttpClient implements ClientInterface |
| 21 | { |
| 22 | /** @var list<RequestInterface> */ |
| 23 | public array $requests = []; |
| 24 | |
| 25 | public function __construct( |
| 26 | private readonly RequestHandlerInterface $handler, |
| 27 | private readonly ServerRequestFactoryInterface $serverRequests, |
| 28 | private readonly StreamFactoryInterface $streams, |
| 29 | ) {} |
| 30 | |
| 31 | public function sendRequest(RequestInterface $request): ResponseInterface |
| 32 | { |
| 33 | $this->requests[] = $request; |
| 34 | $serverRequest = $this->serverRequests->createServerRequest($request->getMethod(), $request->getUri()) |
| 35 | ->withProtocolVersion($request->getProtocolVersion()) |
| 36 | ->withBody($this->streams->createStream((string) $request->getBody())); |
| 37 | foreach ($request->getHeaders() as $name => $values) { |
| 38 | $serverRequest = $serverRequest->withHeader($name, $values); |
| 39 | } |
| 40 | if (preg_match('/^Bearer\s+(.+)$/', $request->getHeaderLine('Authorization'), $m) === 1) { |
| 41 | $serverRequest = $serverRequest->withHeader(HeaderAuthenticator::HEADER, $m[1]); |
| 42 | } |
| 43 | |
| 44 | return $this->handler->handle($serverRequest); |
| 45 | } |
| 46 | } |