3
3
4
4
using System . IO . Pipelines ;
5
5
using System . Runtime . CompilerServices ;
6
+ using System . Text ;
6
7
using Microsoft . AspNetCore . Http ;
7
8
using Microsoft . AspNetCore . Http . Features ;
8
9
using Microsoft . Extensions . Logging . Abstractions ;
@@ -555,8 +556,30 @@ public async Task InvokeAsync_DoesNotAttachHeaders_WhenAlreadyAttached()
555
556
Assert . Equal ( "true" , context . Response . Headers [ "ASPNETCORE-BROWSER-TOOLS" ] ) ;
556
557
}
557
558
558
- [ Fact ]
559
- public async Task InvokeAsync_AddsScriptToThePage ( )
559
+ [ Theory ]
560
+ [ InlineData ( 500 , "text/html" ) ]
561
+ [ InlineData ( 404 , "text/html" ) ]
562
+ [ InlineData ( 200 , "text/html" ) ]
563
+ public async Task InvokeAsync_AddsScriptToThePage_ForSupportedStatusCodes ( int statusCode , string contentType )
564
+ {
565
+ // Act & Assert
566
+ var responseContent = await TestBrowserRefreshMiddleware ( statusCode , contentType , "Test Content" ) ;
567
+ Assert . Contains ( "<script src=\" /_framework/aspnetcore-browser-refresh.js\" ></script>" , responseContent ) ;
568
+ }
569
+
570
+ [ Theory ]
571
+ [ InlineData ( 400 , "text/html" ) ] // Bad Request
572
+ [ InlineData ( 401 , "text/html" ) ] // Unauthorized
573
+ [ InlineData ( 404 , "application/json" ) ] // 404 with wrong content type
574
+ [ InlineData ( 200 , "application/json" ) ] // 200 with wrong content type
575
+ public async Task InvokeAsync_DoesNotAddScript_ForUnsupportedStatusCodesOrContentTypes ( int statusCode , string contentType )
576
+ {
577
+ // Act & Assert
578
+ var responseContent = await TestBrowserRefreshMiddleware ( statusCode , contentType , "Test Content" , includeHtmlWrapper : false ) ;
579
+ Assert . DoesNotContain ( "<script src=\" /_framework/aspnetcore-browser-refresh.js\" ></script>" , responseContent ) ;
580
+ }
581
+
582
+ private async Task < string > TestBrowserRefreshMiddleware ( int statusCode , string contentType , string content , bool includeHtmlWrapper = true )
560
583
{
561
584
// Arrange
562
585
var stream = new MemoryStream ( ) ;
@@ -575,24 +598,32 @@ public async Task InvokeAsync_AddsScriptToThePage()
575
598
576
599
var middleware = new BrowserRefreshMiddleware ( async ( context ) =>
577
600
{
601
+ context . Response . StatusCode = statusCode ;
602
+ context . Response . ContentType = contentType ;
578
603
579
- context . Response . ContentType = "text/html" ;
580
-
581
- await context . Response . WriteAsync ( "<html>" ) ;
582
- await context . Response . WriteAsync ( "<body>" ) ;
583
- await context . Response . WriteAsync ( "<h1>" ) ;
584
- await context . Response . WriteAsync ( "Hello world" ) ;
585
- await context . Response . WriteAsync ( "</h1>" ) ;
586
- await context . Response . WriteAsync ( "</body>" ) ;
587
- await context . Response . WriteAsync ( "</html>" ) ;
604
+ if ( includeHtmlWrapper )
605
+ {
606
+ await context . Response . WriteAsync ( "<html>" ) ;
607
+ await context . Response . WriteAsync ( "<body>" ) ;
608
+ await context . Response . WriteAsync ( "<h1>" ) ;
609
+ await context . Response . WriteAsync ( content ) ;
610
+ await context . Response . WriteAsync ( "</h1>" ) ;
611
+ await context . Response . WriteAsync ( "</body>" ) ;
612
+ await context . Response . WriteAsync ( "</html>" ) ;
613
+ }
614
+ else
615
+ {
616
+ await context . Response . WriteAsync ( content ) ;
617
+ }
588
618
} , NullLogger < BrowserRefreshMiddleware > . Instance ) ;
589
619
590
620
// Act
591
621
await middleware . InvokeAsync ( context ) ;
592
622
593
- // Assert
623
+ // Return response content and verify status code
594
624
var responseContent = Encoding . UTF8 . GetString ( stream . ToArray ( ) ) ;
595
- Assert . Equal ( "<html><body><h1>Hello world</h1><script src=\" /_framework/aspnetcore-browser-refresh.js\" ></script></body></html>" , responseContent ) ;
625
+ Assert . Equal ( statusCode , context . Response . StatusCode ) ;
626
+ return responseContent ;
596
627
}
597
628
598
629
private class TestHttpResponseFeature : IHttpResponseFeature , IHttpResponseBodyFeature
0 commit comments