@@ -34,11 +34,15 @@ private ServerSentEventsMiddleware<ServerSentEventsService> PrepareServerSentEve
3434 ) ;
3535 }
3636
37- private HttpContext PrepareHttpContext ( )
37+ private HttpContext PrepareHttpContext ( string acceptHeaderValue )
3838 {
3939 HttpContext context = new DefaultHttpContext ( ) ;
4040
41- context . Request . Headers . Append ( ACCEPT_HTTP_HEADER , SSE_CONTENT_TYPE ) ;
41+ if ( acceptHeaderValue != null )
42+ {
43+ context . Request . Headers . Append ( ACCEPT_HTTP_HEADER , acceptHeaderValue ) ;
44+ }
45+
4246 context . RequestAborted = new CancellationToken ( true ) ;
4347
4448 return context ;
@@ -47,16 +51,38 @@ private HttpContext PrepareHttpContext()
4751
4852 #region Tests
4953 [ Fact ]
50- public async Task Invoke_SseRequest_Accepts ( )
54+ public async Task Invoke_SseRequestWithoutAcceptHeader_Accepts ( )
5155 {
5256 ServerSentEventsMiddleware < ServerSentEventsService > serverSentEventsMiddleware = PrepareServerSentEventsMiddleware ( ) ;
53- HttpContext context = PrepareHttpContext ( ) ;
57+ HttpContext context = PrepareHttpContext ( null ) ;
5458
5559 await serverSentEventsMiddleware . Invoke ( context , null ) ;
5660
5761 Assert . Equal ( SSE_CONTENT_TYPE , context . Response . ContentType ) ;
5862 }
5963
64+ [ Fact ]
65+ public async Task Invoke_SseRequestWithEventStreamAcceptHeader_Accepts ( )
66+ {
67+ ServerSentEventsMiddleware < ServerSentEventsService > serverSentEventsMiddleware = PrepareServerSentEventsMiddleware ( ) ;
68+ HttpContext context = PrepareHttpContext ( SSE_CONTENT_TYPE ) ;
69+
70+ await serverSentEventsMiddleware . Invoke ( context , null ) ;
71+
72+ Assert . Equal ( SSE_CONTENT_TYPE , context . Response . ContentType ) ;
73+ }
74+
75+ [ Fact ]
76+ public async Task Invoke_SseRequestWithNotEventStreamAcceptHeader_Accepts ( )
77+ {
78+ ServerSentEventsMiddleware < ServerSentEventsService > serverSentEventsMiddleware = PrepareServerSentEventsMiddleware ( ) ;
79+ HttpContext context = PrepareHttpContext ( "text/plain" ) ;
80+
81+ await serverSentEventsMiddleware . Invoke ( context , null ) ;
82+
83+ Assert . Null ( context . Response . ContentType ) ;
84+ }
85+
6086 [ Fact ]
6187 public async Task Invoke_SseRequest_CallsOnPrepareAccept ( )
6288 {
@@ -66,7 +92,7 @@ public async Task Invoke_SseRequest_CallsOnPrepareAccept()
6692 OnPrepareAccept = onPrepareAcceptMock . Object
6793 } ) ;
6894
69- await serverSentEventsMiddleware . Invoke ( PrepareHttpContext ( ) , null ) ;
95+ await serverSentEventsMiddleware . Invoke ( PrepareHttpContext ( null ) , null ) ;
7096
7197 onPrepareAcceptMock . Verify ( m => m ( It . IsAny < HttpResponse > ( ) ) , Times . Once ) ;
7298 }
0 commit comments