8000 [HttpClient] Enable using EventSourceHttpClient::connect() for both GET and POST by wivaku · Pull Request #51558 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[HttpClient] Enable using EventSourceHttpClient::connect() for both GET and POST #51558

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

Merged
merged 1 commit into from
Sep 14, 2023
Merged
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
[HttpClient] Enable using EventSourceHttpClient::connect() for both G…
…ET and POST
  • Loading branch information
wivaku authored and nicolas-grekas committed Sep 14, 2023
commit 35edcf84efc57f7cbdbd6c864a40ddc5f67fea73
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpClient/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CHANGELOG
* Add `HarFileResponseFactory` testing utility, allow to replay responses from `.har` files
* Add `max_retries` option to `RetryableHttpClient` to adjust the retry logic on a per request level
* Add `PingWehookMessage` and `PingWebhookMessageHandler`
* Enable using EventSourceHttpClient::connect() for both GET and POST

6.3
---
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/HttpClient/EventSourceHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public function __construct(HttpClientInterface $client = null, float $reconnect
$this->reconnectionTime = $reconnectionTime;
}

public function connect(string $url, array $options = []): ResponseInterface
public function connect(string $url, array $options = [], string $method = 'GET'): ResponseInterface
{
return $this->request('GET', $url, self::mergeDefaultOptions($options, [
return $this->request($method, $url, self::mergeDefaultOptions($options, [
'buffer' => false,
'headers' => [
'Accept' => 'text/event-stream',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,33 @@ public function testGetServerSentEvents()
}
}

public function testPostServerSentEvents()
{
$chunk = new DataChunk(0, '');
$response = new MockResponse('', ['canceled' => false, 'http_method' => 'POST', 'url' => 'http://localhost:8080/events', 'response_headers' => ['content-type: text/event-stream']]);
$responseStream = new ResponseStream((function () use ($response, $chunk) {
yield $response => new FirstChunk();
yield $response => $chunk;
yield $response => new ErrorChunk(0, 'timeout');
})());

$hasCorrectHeaders = function ($options) {
$this->assertSame(['Accept: text/event-stream', 'Cache-Control: no-cache'], $options['headers']);
$this->assertSame('mybody', $options['body']);

return true;
};

$httpClient = $this->createMock(HttpClientInterface::class);

$httpClient->method('request')->with('POST', 'http://localhost:8080/events', $this->callback($hasCorrectHeaders))->willReturn($response);

$httpClient->method('stream')->willReturn($responseStream);

$es = new EventSourceHttpClient($httpClient);
$res = $es->connect('http://localhost:8080/events', ['body' => 'mybody'], 'POST');
}

/**
* @dataProvider contentTypeProvider
*/
Expand Down
0