22// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. 
33
44using  System ; 
5+ using  System . Globalization ; 
56using  System . Threading . Tasks ; 
67using  Microsoft . AspNetCore . Builder ; 
78using  Microsoft . AspNetCore . Hosting ; 
@@ -128,7 +129,7 @@ public async Task CheckRedirectPathWithQueryString()
128129        [ InlineData ( StatusCodes . Status302Found ) ] 
129130        [ InlineData ( StatusCodes . Status307TemporaryRedirect ) ] 
130131        [ InlineData ( StatusCodes . Status308PermanentRedirect ) ] 
131-         public  async  Task  CheckRedirectToHttps ( int  statusCode ) 
132+         public  async  Task  CheckRedirectToHttpsStatus ( int  statusCode ) 
132133        { 
133134            var  options  =  new  RewriteOptions ( ) . AddRedirectToHttps ( statusCode :  statusCode ) ; 
134135            using  var  host  =  new  HostBuilder ( ) 
@@ -152,6 +153,78 @@ public async Task CheckRedirectToHttps(int statusCode)
152153            Assert . Equal ( statusCode ,  ( int ) response . StatusCode ) ; 
153154        } 
154155
156+         [ Theory ] 
157+         [ InlineData ( null ) ] 
158+         [ InlineData ( 123 ) ] 
159+         public  async  Task  CheckRedirectToHttpsSslPort ( int ?  sslPort ) 
160+         { 
161+             var  options  =  new  RewriteOptions ( ) . AddRedirectToHttps ( statusCode :  StatusCodes . Status302Found ,  sslPort :  sslPort ) ; 
162+             using  var  host  =  new  HostBuilder ( ) 
163+                 . ConfigureWebHost ( webHostBuilder => 
164+                 { 
165+                     webHostBuilder 
166+                     . UseTestServer ( ) 
167+                     . Configure ( app => 
168+                     { 
169+                         app . UseRewriter ( options ) ; 
170+                     } ) ; 
171+                 } ) . Build ( ) ; 
172+ 
173+             await  host . StartAsync ( ) ; 
174+ 
175+             var  server  =  host . GetTestServer ( ) ; 
176+ 
177+             var  response  =  await  server . CreateClient ( ) . GetAsync ( new  Uri ( "http://example.com" ) ) ; 
178+ 
179+             if  ( sslPort . HasValue ) 
180+             { 
181+                 Assert . Equal ( $ "https://example.com:{ sslPort . GetValueOrDefault ( ) . ToString ( CultureInfo . InvariantCulture ) } /",  response . Headers . Location . OriginalString ) ; 
182+             } 
183+             else 
184+             { 
185+                 Assert . Equal ( "https://example.com/" ,  response . Headers . Location . OriginalString ) ; 
186+             } 
187+         } 
188+ 
189+         [ Theory ] 
190+         [ InlineData ( null ,  "example.com" ,  "example.com/" ) ] 
191+         [ InlineData ( null ,  "example.com/path" ,  "example.com/path" ) ] 
192+         [ InlineData ( null ,  "example.com/path?name=value" ,  "example.com/path?name=value" ) ] 
193+         [ InlineData ( null ,  "hoψst.com" ,  "xn--host-cpd.com/" ) ] 
194+         [ InlineData ( null ,  "hoψst.com/path" ,  "xn--host-cpd.com/path" ) ] 
195+         [ InlineData ( null ,  "hoψst.com/path?name=value" ,  "xn--host-cpd.com/path?name=value" ) ] 
196+         [ InlineData ( null ,  "example.com/pãth" ,  "example.com/p%C3%A3th" ) ] 
197+         [ InlineData ( null ,  "example.com/path?näme=valüe" ,  "example.com/path?n%C3%A4me=val%C3%BCe" ) ] 
198+         [ InlineData ( "example.com/pathBase" ,  "example.com/pathBase/path" ,  "example.com/pathBase/path" ) ] 
199+         [ InlineData ( "example.com/pathBase" ,  "example.com/pathBase" ,  "example.com/pathBase" ) ] 
200+         [ InlineData ( "example.com/pâthBase" ,  "example.com/pâthBase/path" ,  "example.com/p%C3%A2thBase/path" ) ] 
201+         public  async  Task  CheckRedirectToHttpsUrl ( string  baseAddress ,  string  hostPathAndQuery ,  string  expectedHostPathAndQuery ) 
202+         { 
203+             var  options  =  new  RewriteOptions ( ) . AddRedirectToHttps ( ) ; 
204+             using  var  host  =  new  HostBuilder ( ) 
205+                 . ConfigureWebHost ( webHostBuilder => 
206+                 { 
207+                     webHostBuilder 
208+                     . UseTestServer ( ) 
209+                     . Configure ( app => 
210+                     { 
211+                         app . UseRewriter ( options ) ; 
212+                     } ) ; 
213+                 } ) . Build ( ) ; 
214+ 
215+             await  host . StartAsync ( ) ; 
216+ 
217+             var  server  =  host . GetTestServer ( ) ; 
218+             if  ( ! string . IsNullOrEmpty ( baseAddress ) ) 
219+             { 
220+                 server . BaseAddress  =  new  Uri ( "http://"  +  baseAddress ) ; 
221+             } 
222+ 
223+             var  response  =  await  server . CreateClient ( ) . GetAsync ( new  Uri ( "http://"  +  hostPathAndQuery ) ) ; 
224+ 
225+             Assert . Equal ( "https://"  +  expectedHostPathAndQuery ,  response . Headers . Location . OriginalString ) ; 
226+         } 
227+ 
155228        [ Fact ] 
156229        public  async  Task  CheckPermanentRedirectToHttps ( ) 
157230        { 
0 commit comments