Skip to content

Commit

Permalink
Merge pull request #7 from superbrave/DV-8390
Browse files Browse the repository at this point in the history
[DV-8391] Added ResponseStream to VerboseErrorHttpClient::stream
  • Loading branch information
kkevindev authored Apr 22, 2024
2 parents c9e64f9 + 7537987 commit c399665
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 13 deletions.
17 changes: 17 additions & 0 deletions src/HttpClient/Response/VerboseErrorResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@

namespace Superbrave\VerboseErrorHttpClientBundle\HttpClient\Response;

use Generator;
use Superbrave\VerboseErrorHttpClientBundle\HttpClient\Exception\ClientException;
use Superbrave\VerboseErrorHttpClientBundle\HttpClient\Exception\RedirectionException;
use Superbrave\VerboseErrorHttpClientBundle\HttpClient\Exception\ServerException;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
use Symfony\Contracts\HttpClient\ResponseStreamInterface;

/**
* Wraps a response to be able to decorate the thrown exceptions.
Expand Down Expand Up @@ -76,4 +79,18 @@ public function cancel(): void
{
$this->response->cancel();
}

/**
* @internal
*
* @param iterable<VerboseErrorResponse> $responses
*
* @return Generator<VerboseErrorResponse, ResponseStreamInterface>
*/
public static function stream(HttpClientInterface $client, iterable $responses, ?float $timeout): Generator
{
foreach ($responses as $response) {
yield $response => $client->stream($response->response, $timeout);
}
}
}
12 changes: 10 additions & 2 deletions src/HttpClient/VerboseErrorHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Superbrave\VerboseErrorHttpClientBundle\HttpClient;

use Superbrave\VerboseErrorHttpClientBundle\HttpClient\Response\VerboseErrorResponse;
use Symfony\Component\HttpClient\Response\ResponseStream;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
use Symfony\Contracts\HttpClient\ResponseStreamInterface;
Expand All @@ -27,9 +28,16 @@ public function request(string $method, string $url, array $options = []): Respo
return new VerboseErrorResponse($response);
}

public function stream($responses, ?float $timeout = null): ResponseStreamInterface
/**
* @param VerboseErrorResponse|iterable $responses
*/
public function stream(ResponseInterface|iterable $responses, ?float $timeout = null): ResponseStreamInterface
{
return $this->client->stream($responses, $timeout);
if ($responses instanceof VerboseErrorResponse) {
$responses = [$responses];
}

return new ResponseStream(VerboseErrorResponse::stream($this->client, $responses, $timeout));
}

public function withOptions(array $options): static
Expand Down
32 changes: 21 additions & 11 deletions tests/VerboseErrorHttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Superbrave\VerboseErrorHttpClientBundle\HttpClient\Exception\ClientException;
use Superbrave\VerboseErrorHttpClientBundle\HttpClient\Exception\RedirectionException;
use Superbrave\VerboseErrorHttpClientBundle\HttpClient\Exception\ServerException;
use Superbrave\VerboseErrorHttpClientBundle\HttpClient\Response\VerboseErrorResponse;
use Superbrave\VerboseErrorHttpClientBundle\HttpClient\VerboseErrorHttpClient;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
Expand Down Expand Up @@ -82,26 +83,35 @@ public function testRequestThrowsRedirectionException(
}

/**
* Tests if VerboseErrorHttpClient::stream only calls the stream method on
* the underlying/decorated HTTP client.
* Tests if {@see VerboseErrorHttpClient::stream()} only calls the stream method on
* the underlying/decorated HTTP client with the underlying response and that it returns a {@see ResponseStream}.
*/
public function testStream(): void
{
// Arrange
$mockResponse = new MockResponse('');

$verboseErrorResponse = new VerboseErrorResponse($mockResponse);

$expectedResponseStream = new ResponseStream(MockResponse::stream([$mockResponse], null));

$httpClientMock = $this->getMockBuilder(HttpClientInterface::class)
->getMock();
$httpClientMock->expects($this->once())
->method('stream')
->with($mockResponse, null)
->willReturn($expectedResponseStream);
$httpClientMock = $this->createMock(HttpClientInterface::class);

$httpClient = new VerboseErrorHttpClient($httpClientMock);
$verboseErrorHttpClient = new VerboseErrorHttpClient($httpClientMock);

$responseStream = $httpClient->stream($mockResponse);
// Act
$responseStream = $verboseErrorHttpClient->stream($verboseErrorResponse);

// Assert
$this->assertEquals($expectedResponseStream, $responseStream);

// Assert (2)
$httpClientMock->expects($this->once())
->method('stream')
->with($mockResponse, null);

$this->assertSame($expectedResponseStream, $responseStream);
// Act (2)
$responseStream->next();
}

public function provideServerExceptionResponses(): array
Expand Down

0 comments on commit c399665

Please sign in to comment.