@@ -13,10 +13,17 @@ namespace Microsoft.AspNetCore.Analyzers.Kestrel;
1313
1414public  class  ListenOnIPv6AnyAnalyzerAndFixerTests 
1515{ 
16-     [ Fact ]  // do we need any other scenarios except the direct usage one? 
16+     [ Fact ] 
17+     public  async  Task  ReportsDiagnostic_IPAddressAsLocalVariable_OuterScope ( ) 
18+     { 
19+         var  source  =  GetKestrelSetupSource ( "myIp" ,  extraOuterCode :  "var myIp = IPAddress.Any;" ) ; 
20+         await  VerifyCS . VerifyAnalyzerAsync ( source ,  codeSampleDiagnosticResult ) ; 
21+     } 
22+ 
23+     [ Fact ] 
1724    public  async  Task  ReportsDiagnostic_IPAddressAsLocalVariable ( ) 
1825    { 
19-         var  source  =  GetKestrelSetupSource ( "myIp" ,  "var myIp = IPAddress.Any;" ) ; 
26+         var  source  =  GetKestrelSetupSource ( "myIp" ,  extraInlineCode :   "var myIp = IPAddress.Any;" ) ; 
2027        await  VerifyCS . VerifyAnalyzerAsync ( source ,  codeSampleDiagnosticResult ) ; 
2128    } 
2229
@@ -31,19 +38,35 @@ public async Task ReportsDiagnostic_ExplicitUsage()
3138    public  async  Task  CodeFix_ExplicitUsage ( ) 
3239    { 
3340        var  source  =  GetKestrelSetupSource ( "IPAddress.Any" ) ; 
34-         var  fixedSource  =  GetKestrelSetupSource ( "IPAddress.IPv6Any" ) ; 
41+         var  fixedSource  =  GetCorrectedKestrelSetup ( ) ; 
42+         await  VerifyCS . VerifyCodeFixAsync ( source ,  codeSampleDiagnosticResult ,  fixedSource ) ; 
43+     } 
44+ 
45+     [ Fact ] 
46+     public  async  Task  CodeFix_IPAddressAsLocalVariable ( ) 
47+     { 
48+         var  source  =  GetKestrelSetupSource ( "IPAddress.Any" ,  extraInlineCode :  "var myIp = IPAddress.Any;" ) ; 
49+         var  fixedSource  =  GetCorrectedKestrelSetup ( extraInlineCode :  "var myIp = IPAddress.Any;" ) ; 
3550        await  VerifyCS . VerifyCodeFixAsync ( source ,  codeSampleDiagnosticResult ,  fixedSource ) ; 
3651    } 
3752
3853    private  static   DiagnosticResult  codeSampleDiagnosticResult 
3954        =  new  DiagnosticResult ( DiagnosticDescriptors . KestrelShouldListenOnIPv6AnyInsteadOfIpAny ) . WithLocation ( 0 ) ; 
4055
41-     static   string  GetKestrelSetupSource ( string  ipAddressArgument ,  string  extraInlineCode  =  null )  =>  $$ """ 
56+     static   string  GetKestrelSetupSource ( string  ipAddressArgument ,  string  extraInlineCode  =  null ,  string  extraOuterCode  =  null ) 
57+         =>  GetCodeSample ( $$ """ Listen({|#0:{{ ipAddressArgument }} |}, """ ,  extraInlineCode ,  extraOuterCode ) ; 
58+ 
59+     static   string  GetCorrectedKestrelSetup ( string  extraInlineCode  =  null ,  string  extraOuterCode  =  null ) 
60+         =>  GetCodeSample ( "ListenAnyIP(" ,  extraInlineCode ,  extraOuterCode ) ; 
61+ 
62+     static   string  GetCodeSample ( string  invocation ,  string  extraInlineCode  =  null ,  string  extraOuterCode  =  null )  =>  $$ """ 
4263        using Microsoft.Extensions.Hosting; 
4364        using Microsoft.AspNetCore.Hosting; 
4465        using Microsoft.AspNetCore.Server.Kestrel.Core; 
4566        using System.Net; 
4667     
68+         {{ extraOuterCode }}  
69+      
4770        var hostBuilder = new HostBuilder() 
4871            .ConfigureWebHost(webHost => 
4972            { 
@@ -53,7 +76,7 @@ static string GetKestrelSetupSource(string ipAddressArgument, string extraInline
5376                     
5477                    options.ListenLocalhost(5000); 
5578                    options.ListenAnyIP(5000); 
56-                     options.Listen({|#0: {{ ipAddressArgument }} |},  5000, listenOptions => 
79+                     options.{{ invocation }} 5000, listenOptions => 
5780                    { 
5881                        listenOptions.UseHttps(); 
5982                        listenOptions.Protocols = HttpProtocols.Http1AndHttp2AndHttp3; 
0 commit comments