33
44using System . IO . Pipelines ;
55using System . Runtime . CompilerServices ;
6+ using System . Text ;
67using Microsoft . AspNetCore . Http ;
78using Microsoft . AspNetCore . Http . Features ;
89using Microsoft . Extensions . Logging . Abstractions ;
@@ -555,8 +556,30 @@ public async Task InvokeAsync_DoesNotAttachHeaders_WhenAlreadyAttached()
555556 Assert . Equal ( "true" , context . Response . Headers [ "ASPNETCORE-BROWSER-TOOLS" ] ) ;
556557 }
557558
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 )
560583 {
561584 // Arrange
562585 var stream = new MemoryStream ( ) ;
@@ -575,24 +598,32 @@ public async Task InvokeAsync_AddsScriptToThePage()
575598
576599 var middleware = new BrowserRefreshMiddleware ( async ( context ) =>
577600 {
601+ context . Response . StatusCode = statusCode ;
602+ context . Response . ContentType = contentType ;
578603
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+ }
588618 } , NullLogger < BrowserRefreshMiddleware > . Instance ) ;
589619
590620 // Act
591621 await middleware . InvokeAsync ( context ) ;
592622
593- // Assert
623+ // Return response content and verify status code
594624 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 ;
596627 }
597628
598629 private class TestHttpResponseFeature : IHttpResponseFeature , IHttpResponseBodyFeature
0 commit comments