diff --git a/src/Symfony/Component/HttpClient/CHANGELOG.md b/src/Symfony/Component/HttpClient/CHANGELOG.md index ff802cbb54cf5..5f8fbf322d46d 100644 --- a/src/Symfony/Component/HttpClient/CHANGELOG.md +++ b/src/Symfony/Component/HttpClient/CHANGELOG.md @@ -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 --- diff --git a/src/Symfony/Component/HttpClient/EventSourceHttpClient.php b/src/Symfony/Component/HttpClient/EventSourceHttpClient.php index 95baebaf6424c..853657c770eff 100644 --- a/src/Symfony/Component/HttpClient/EventSourceHttpClient.php +++ b/src/Symfony/Component/HttpClient/EventSourceHttpClient.php @@ -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', diff --git a/src/Symfony/Component/HttpClient/Tests/EventSourceHttpClientTest.php b/src/Symfony/Component/HttpClient/Tests/EventSourceHttpClientTest.php index 72eb74fb9f289..e4b1986d34e18 100644 --- a/src/Symfony/Component/HttpClient/Tests/EventSourceHttpClientTest.php +++ b/src/Symfony/Component/HttpClient/Tests/EventSourceHttpClientTest.php @@ -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 */
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: