|
17 | 17 | use Symfony\Component\HttpClient\Chunk\FirstChunk;
|
18 | 18 | use Symfony\Component\HttpClient\Chunk\ServerSentEvent;
|
19 | 19 | use Symfony\Component\HttpClient\EventSourceHttpClient;
|
| 20 | +use Symfony\Component\HttpClient\Exception\EventSourceException; |
20 | 21 | use Symfony\Component\HttpClient\Response\MockResponse;
|
21 | 22 | use Symfony\Component\HttpClient\Response\ResponseStream;
|
22 | 23 | use Symfony\Contracts\HttpClient\HttpClientInterface;
|
@@ -108,4 +109,61 @@ public function testGetServerSentEvents()
|
108 | 109 | }
|
109 | 110 | }
|
110 | 111 | }
|
| 112 | + |
| 113 | + /** |
| 114 | + * @dataProvider contentTypeProvider |
| 115 | + */ |
| 116 | + public function testContentType($contentType, $expected) |
| 117 | + { |
| 118 | + $chunk = new DataChunk(0, ''); |
| 119 | + $response = new MockResponse('', ['canceled' => false, 'http_method' => 'GET', 'url' => 'http://localhost:8080/events', 'response_headers' => ['content-type: '.$contentType]]); |
| 120 | + $responseStream = new ResponseStream((function () use ($response, $chunk) { |
| 121 | + yield $response => new FirstChunk(); |
| 122 | + yield $response => $chunk; |
| 123 | + yield $response => new ErrorChunk(0, 'timeout'); |
| 124 | + })()); |
| 125 | + |
| 126 | + $hasCorrectHeaders = function ($options) { |
| 127 | + $this->assertSame(['Accept: text/event-stream', 'Cache-Control: no-cache'], $options['headers']); |
| 128 | + |
| 129 | + return true; |
| 130 | + }; |
| 131 | + |
| 132 | + $httpClient = $this->createMock(HttpClientInterface::class); |
| 133 | + $httpClient->method('request')->with('GET', 'http://localhost:8080/events', $this->callback($hasCorrectHeaders))->willReturn($response); |
| 134 | + |
| 135 | + $httpClient->method('stream')->willReturn($responseStream); |
| 136 | + |
| 137 | + $es = new EventSourceHttpClient($httpClient); |
| 138 | + $res = $es->connect('http://localhost:8080/events'); |
| 139 | + |
| 140 | + if ($expected instanceof EventSourceException) { |
| 141 | + $this->expectExceptionMessage($expected->getMessage()); |
| 142 | + } |
| 143 | + |
| 144 | + foreach ($es->stream($res) as $chunk) { |
| 145 | + if ($chunk->isTimeout()) { |
| 146 | + continue; |
| 147 | + } |
| 148 | + |
| 149 | + if ($chunk->isLast()) { |
| 150 | + return; |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + public function contentTypeProvider() |
| 156 | + { |
| 157 | + return [ |
| 158 | + ['text/event-stream', true], |
| 159 | + ['text/event-stream;charset=utf-8', true], |
| 160 | + ['text/event-stream;charset=UTF-8', true], |
| 161 | + ['Text/EVENT-STREAM;Charset="utf-8"', true], |
| 162 | + ['text/event-stream; charset="utf-8"', true], |
| 163 | + ['text/event-stream; charset=iso-8859-15', true], |
| 164 | + ['text/html', new EventSourceException('Response content-type is "text/html" while "text/event-stream" was expected for "http://localhost:8080/events".')], |
| 165 | + ['text/html; charset="utf-8"', new EventSourceException('Response content-type is "text/html; charset="utf-8"" while "text/event-stream" was expected for "http://localhost:8080/events".')], |
| 166 | + ['text/event-streambla', new EventSourceException('Response content-type is "text/event-streambla" while "text/event-stream" was expected for "http://localhost:8080/events".')], |
| 167 | + ]; |
| 168 | + } |
111 | 169 | }
|
0 commit comments