Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[HttpClient] add DecoratorTrait for response #53332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions 1 src/Symfony/Component/HttpClient/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CHANGELOG
* Add `HttpOptions::setHeader()` to add or replace a single header
* Allow mocking `start_time` info in `MockResponse`
* Add `MockResponse::fromFile()` and `JsonMockResponse::fromFile()` methods to help using fixtures files
* Add `DecoratorTrait` for responses

7.0
---
Expand Down
98 changes: 98 additions & 0 deletions 98 src/Symfony/Component/HttpClient/Response/DecoratorTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpClient\Response;

use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;

/**
* @author Cyril Vermande <https://github.com/cyve>
*/
trait DecoratorTrait
{
private ResponseInterface $response;

public function __construct(
ResponseInterface $response,
) {
$this->response = $response;
}

public function getStatusCode(): int
{
return $this->response->getStatusCode();
}

public function getHeaders(bool $throw = true): array
{
return $this->response->getHeaders($throw);
}

public function getContent(bool $throw = true): string
{
return $this->response->getContent($throw);
}

public function toArray(bool $throw = true): array
{
return $this->response->toArray($throw);
}

public function cancel(): void
{
$this->response->cancel();
}

public function getInfo(string $type = null): mixed
{
return $this->response->getInfo($type);
}

/**
* @return resource
*/
public function toStream(bool $throw = true)
{
if ($throw) {
// Ensure headers arrived
$this->response->getHeaders();
}

if ($this->response instanceof StreamableInterface) {
return $this->response->toStream($throw);
}

return StreamWrapper::createResource($this->response);
}

/**
* @internal
*/
public static function stream(HttpClientInterface $client, ResponseInterface|iterable $responses, float $timeout = null): \Generator
{
if ($responses instanceof ResponseInterface) {
$responses = [$responses];
}

$wrappedResponses = [];
$responseMap = new \SplObjectStorage();

foreach ($responses as $response) {
$responseMap[$response->response] = $response;
$wrappedResponses[] = $response->response;
}

foreach ($client->stream($wrappedResponses, $timeout) as $wrappedResponse => $chunk) {
yield $responseMap[$wrappedResponse] => $chunk;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpClient\Tests\Response;

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\Response\DecoratorTrait;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Component\HttpClient\Response\ResponseStream;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
* @author Cyril Vermande <https://github.com/cyve>
*/
class DecoratorTraitTest extends TestCase
{
private $subject;

protected function setUp(): void
{
$decorated = MockResponse::fromRequest(
'GET',
'https://symfony.com',
[],
new MockResponse(
'{"foo":"bar"}',
[
'http_code' => 200,
'response_headers' => ['Content-Type' => 'application/json'],
],
)
);
$this->subject = $this->getObjectForTrait(DecoratorTrait::class, [$decorated]);
}

public function testShouldReturnDecoratedStatusCode()
{
$this->assertEquals(200, $this->subject->getStatusCode());
}

public function testShouldReturnDecoratedHeaders()
{
$this->assertEquals(['content-type' => ['application/json']], $this->subject->getHeaders());
}

public function testShouldReturnDecoratedContent()
{
$this->assertEquals('{"foo":"bar"}', $this->subject->getContent());
}

public function testShouldReturnDecoratedContentInArray()
{
$this->assertEquals(['foo' => 'bar'], $this->subject->toArray());
}

public function testShouldReturnDecoratedInfo()
{
$info = $this->subject->getInfo();
$this->assertArrayHasKey('http_code', $info);
$this->assertArrayHasKey('response_headers', $info);
$this->assertArrayHasKey('http_method', $info);
$this->assertArrayHasKey('url', $info);
}

public function testShouldReturnDecoratedInfoByType()
{
$this->assertEquals('https://symfony.com', $this->subject->getInfo('url'));
}

public function testShouldReturnDecoratedResponseAsStream()
{
$stream = $this->subject->toStream();
$this->assertTrue(\is_resource($stream));
$this->assertEquals('{"foo":"bar"}', stream_get_contents($stream));
}

public function testShouldStreamTheDecoratedResponses()
{
$httpClient = $this->createMock(HttpClientInterface::class);
$httpClient->method('stream')->willReturnCallback(fn ($responses, $timeout) => new ResponseStream(MockResponse::stream($responses, $timeout)));

foreach ($this->subject::stream($httpClient, [$this->subject]) as $response => $chunk) {
// Assert that the "stream()" method yield chunks from the decorated response, but with the wrapped response as key.
$this->assertSame($response, $this->subject);
break;
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.