15
15
use Symfony \Component \HttpClient \Chunk \DataChunk ;
16
16
use Symfony \Component \HttpClient \Chunk \ErrorChunk ;
17
17
use Symfony \Component \HttpClient \Chunk \FirstChunk ;
18
+ use Symfony \Component \HttpClient \Chunk \LastChunk ;
18
19
use Symfony \Component \HttpClient \Chunk \ServerSentEvent ;
19
20
use Symfony \Component \HttpClient \EventSourceHttpClient ;
20
21
use Symfony \Component \HttpClient \Exception \EventSourceException ;
22
+ use Symfony \Component \HttpClient \MockHttpClient ;
21
23
use Symfony \Component \HttpClient \Response \MockResponse ;
22
24
use Symfony \Component \HttpClient \Response \ResponseStream ;
23
25
use Symfony \Contracts \HttpClient \HttpClientInterface ;
@@ -34,7 +36,11 @@ class EventSourceHttpClientTest extends TestCase
34
36
*/
35
37
public function testGetServerSentEvents (string $ sep )
36
38
{
37
- $ rawData = <<<TXT
39
+ $ es = new EventSourceHttpClient (new MockHttpClient (function (string $ method , string $ url , array $ options ) use ($ sep ): MockResponse {
40
+ $ this ->assertSame (['Accept: text/event-stream ' , 'Cache-Control: no-cache ' ], $ options ['headers ' ]);
41
+
42
+ return new MockResponse ([
43
+ str_replace ("\n" , $ sep , <<<TXT
38
44
event: builderror
39
45
id: 46
40
46
data: {"foo": "bar"}
@@ -43,7 +49,18 @@ public function testGetServerSentEvents(string $sep)
43
49
id: 47
44
50
data: {}
45
51
52
+ : this is a oneline comment
53
+
54
+ : this is a
55
+ : multiline comment
56
+
57
+ : comments are ignored
46
58
event: reload
59
+
60
+ TXT
61
+ ),
62
+ str_replace ("\n" , $ sep , <<<TXT
63
+ : anywhere
47
64
id: 48
48
65
data: {}
49
66
@@ -62,58 +79,33 @@ public function testGetServerSentEvents(string $sep)
62
79
63
80
id: 60
64
81
data
65
- TXT ;
66
- $ data = str_replace ("\n" , $ sep , $ rawData );
67
-
68
- $ chunk = new DataChunk (0 , $ data );
69
- $ response = new MockResponse ('' , ['canceled ' => false , 'http_method ' => 'GET ' , 'url ' => 'http://localhost:8080/events ' , 'response_headers ' => ['content-type: text/event-stream ' ]]);
70
- $ responseStream = new ResponseStream ((function () use ($ response , $ chunk ) {
71
- yield $ response => new FirstChunk ();
72
- yield $ response => $ chunk ;
73
- yield $ response => new ErrorChunk (0 , 'timeout ' );
74
- })());
75
-
76
- $ hasCorrectHeaders = function ($ options ) {
77
- $ this ->assertSame (['Accept: text/event-stream ' , 'Cache-Control: no-cache ' ], $ options ['headers ' ]);
78
-
79
- return true ;
80
- };
81
-
82
- $ httpClient = $ this ->createMock (HttpClientInterface::class);
83
- $ httpClient ->method ('request ' )->with ('GET ' , 'http://localhost:8080/events ' , $ this ->callback ($ hasCorrectHeaders ))->willReturn ($ response );
84
-
85
- $ httpClient ->method ('stream ' )->willReturn ($ responseStream );
86
-
87
- $ es = new EventSourceHttpClient ($ httpClient );
82
+ TXT
83
+ ),
84
+ ], [
85
+ 'canceled ' => false ,
86
+ 'http_method ' => 'GET ' ,
87
+ 'url ' => 'http://localhost:8080/events ' ,
88
+ 'response_headers ' => ['content-type: text/event-stream ' ],
89
+ ]);
90
+ }));
88
91
$ res = $ es ->connect ('http://localhost:8080/events ' );
89
92
90
93
$ expected = [
91
94
new FirstChunk (),
92
95
new ServerSentEvent (str_replace ("\n" , $ sep , "event: builderror \nid: 46 \ndata: { \"foo \": \"bar \"} \n\n" )),
93
96
new ServerSentEvent (str_replace ("\n" , $ sep , "event: reload \nid: 47 \ndata: {} \n\n" )),
94
- new ServerSentEvent (str_replace ("\n" , $ sep , "event: reload \nid: 48 \ndata: {} \n\n" )),
97
+ new DataChunk (-1 , str_replace ("\n" , $ sep , ": this is a oneline comment \n\n" )),
98
+ new DataChunk (-1 , str_replace ("\n" , $ sep , ": this is a \n: multiline comment \n\n" )),
99
+ new ServerSentEvent (str_replace ("\n" , $ sep , ": comments are ignored \nevent: reload \n: anywhere \nid: 48 \ndata: {} \n\n" )),
95
100
new ServerSentEvent (str_replace ("\n" , $ sep , "data: test \ndata:test \nid: 49 \nevent: testEvent \n\n\n" )),
96
101
new ServerSentEvent (str_replace ("\n" , $ sep , "id: 50 \ndata: <tag> \ndata \ndata: <foo /> \ndata \ndata: </tag> \n\n" )),
102
+ new DataChunk (-1 , str_replace ("\n" , $ sep , "id: 60 \ndata " )),
103
+ new LastChunk ("\r\n" === $ sep ? 355 : 322 ),
97
104
];
98
- $ i = 0 ;
99
-
100
- $ this ->expectExceptionMessage ('Response has been canceled ' );
101
- while ($ res ) {
102
- if ($ i > 0 ) {
103
- $ res ->cancel ();
104
- }
105
- foreach ($ es ->stream ($ res ) as $ chunk ) {
106
- if ($ chunk ->isTimeout ()) {
107
- continue ;
108
- }
109
-
110
- if ($ chunk ->isLast ()) {
111
- continue ;
112
- }
113
-
114
- $ this ->assertEquals ($ expected [$ i ++], $ chunk );
115
- }
105
+ foreach ($ es ->stream ($ res ) as $ chunk ) {
106
+ $ this ->assertEquals (array_shift ($ expected ), $ chunk );
116
107
}
108
+ $ this ->assertSame ([], $ expected );
117
109
}
118
110
119
111
/**
0 commit comments