@@ -476,5 +476,65 @@ void main() {
476476 // server received and processed our DELETE request
477477 expect (true , isTrue);
478478 });
479+
480+ test ('handles CRLF line endings in SSE events' , () async {
481+ transport = StreamableHttpClientTransport (serverUrl);
482+
483+ final messageCompleter = Completer <JsonRpcMessage >();
484+ transport.onmessage = (message) {
485+ print ('Transport received message: ${jsonEncode (message .toJson ())}' );
486+ messageCompleter.complete (message);
487+ };
488+
489+ transport.onerror = (error) {
490+ print ('Transport error: $error ' );
491+ };
492+
493+ await transport.start ();
494+
495+ final notification = JsonRpcInitializedNotification ();
496+ await transport.send (notification);
497+
498+ await Future .delayed (Duration (milliseconds: 1000 ));
499+
500+ if (currentSseConnections.isEmpty) {
501+ fail ('No SSE connections established' );
502+ }
503+
504+ print (
505+ 'About to send SSE event, active connections: ${currentSseConnections .length }' );
506+
507+ for (final connection in List <HttpResponse >.from (currentSseConnections)) {
508+ try {
509+ final message = {
510+ 'jsonrpc' : '2.0' ,
511+ 'method' : 'notifications/initialized' ,
512+ };
513+
514+ final data = jsonEncode (message);
515+ print ('Sending SSE event with data: $data ' );
516+
517+ connection.write ('event: message\r\n\r\n ' );
518+ connection.write ('data: $data \r\n\r\n ' );
519+ await connection.flush ();
520+ print ('Sent SSE event' );
521+ } catch (e) {
522+ print ('Error sending SSE event: $e ' );
523+ fail ('Failed to send SSE event: $e ' );
524+ }
525+ }
526+
527+ final message = await messageCompleter.future.timeout (
528+ Duration (seconds: 5 ),
529+ onTimeout: () {
530+ print ('*** TIMEOUT: No message received via SSE after 5 seconds' );
531+ throw TimeoutException ('No message received via SSE' );
532+ },
533+ );
534+
535+ expect (message, isA <JsonRpcNotification >());
536+ expect ((message as JsonRpcNotification ).method,
537+ equals ('notifications/initialized' ));
538+ }, timeout: Timeout (Duration (seconds: 10 )));
479539 });
480540}
0 commit comments