Skip to content

Commit 7c833ee

Browse files
feature symfony#51558 [HttpClient] Enable using EventSourceHttpClient::connect() for both GET and POST (wivaku)
This PR was squashed before being merged into the 6.4 branch. Discussion ---------- [HttpClient] Enable using EventSourceHttpClient::connect() for both GET and POST | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | License | MIT Fix so connect() can be used for SSE connections that require POST, e.g. GraphQL SSE subscriptions. This so we don't have to manually create the request() and include all of the options that are used for connect(). Commits ------- 35edcf8 [HttpClient] Enable using EventSourceHttpClient::connect() for both GET and POST
2 parents 972c442 + 35edcf8 commit 7c833ee

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

src/Symfony/Component/HttpClient/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CHANGELOG
77
* Add `HarFileResponseFactory` testing utility, allow to replay responses from `.har` files
88
* Add `max_retries` option to `RetryableHttpClient` to adjust the retry logic on a per request level
99
* Add `PingWehookMessage` and `PingWebhookMessageHandler`
10+
* Enable using EventSourceHttpClient::connect() for both GET and POST
1011

1112
6.3
1213
---

src/Symfony/Component/HttpClient/EventSourceHttpClient.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ public function __construct(HttpClientInterface $client = null, float $reconnect
3939
$this->reconnectionTime = $reconnectionTime;
4040
}
4141

42-
public function connect(string $url, array $options = []): ResponseInterface
42+
public function connect(string $url, array $options = [], string $method = 'GET'): ResponseInterface
4343
{
44-
return $this->request('GET', $url, self::mergeDefaultOptions($options, [
44+
return $this->request($method, $url, self::mergeDefaultOptions($options, [
4545
'buffer' => false,
4646
'headers' => [
4747
'Accept' => 'text/event-stream',

src/Symfony/Component/HttpClient/Tests/EventSourceHttpClientTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,33 @@ public function testGetServerSentEvents()
110110
}
111111
}
112112

113+
public function testPostServerSentEvents()
114+
{
115+
$chunk = new DataChunk(0, '');
116+
$response = new MockResponse('', ['canceled' => false, 'http_method' => 'POST', 'url' => 'http://localhost:8080/events', 'response_headers' => ['content-type: text/event-stream']]);
117+
$responseStream = new ResponseStream((function () use ($response, $chunk) {
118+
yield $response => new FirstChunk();
119+
yield $response => $chunk;
120+
yield $response => new ErrorChunk(0, 'timeout');
121+
})());
122+
123+
$hasCorrectHeaders = function ($options) {
124+
$this->assertSame(['Accept: text/event-stream', 'Cache-Control: no-cache'], $options['headers']);
125+
$this->assertSame('mybody', $options['body']);
126+
127+
return true;
128+
};
129+
130+
$httpClient = $this->createMock(HttpClientInterface::class);
131+
132+
$httpClient->method('request')->with('POST', 'http://localhost:8080/events', $this->callback($hasCorrectHeaders))->willReturn($response);
133+
134+
$httpClient->method('stream')->willReturn($responseStream);
135+
136+
$es = new EventSourceHttpClient($httpClient);
137+
$res = $es->connect('http://localhost:8080/events', ['body' => 'mybody'], 'POST');
138+
}
139+
113140
/**
114141
* @dataProvider contentTypeProvider
115142
*/

0 commit comments

Comments
 (0)