File tree Expand file tree Collapse file tree 2 files changed +48
-1
lines changed
test/System.Web.Mvc.Test/Test Expand file tree Collapse file tree 2 files changed +48
-1
lines changed Original file line number Diff line number Diff line change @@ -9,6 +9,25 @@ namespace System.Web.Mvc
99 [ AttributeUsage ( AttributeTargets . Class | AttributeTargets . Method , Inherited = true , AllowMultiple = false ) ]
1010 public class RequireHttpsAttribute : FilterAttribute , IAuthorizationFilter
1111 {
12+ public RequireHttpsAttribute ( )
13+ : this ( permanent : false )
14+ {
15+ }
16+
17+ /// <summary>
18+ /// Initializes a new instance of the <see cref="RequireHttpsAttribute"/> class.
19+ /// </summary>
20+ /// <param name="permanent">Whether the redirect to HTTPS should be a permanent redirect.</param>
21+ public RequireHttpsAttribute ( bool permanent )
22+ {
23+ this . Permanent = permanent ;
24+ }
25+
26+ /// <summary>
27+ /// Gets a value indicating whether the redirect to HTTPS should be a permanent redirect.
28+ /// </summary>
29+ public bool Permanent { get ; private set ; }
30+
1231 public virtual void OnAuthorization ( AuthorizationContext filterContext )
1332 {
1433 if ( filterContext == null )
@@ -34,7 +53,7 @@ protected virtual void HandleNonHttpsRequest(AuthorizationContext filterContext)
3453
3554 // redirect to HTTPS version of page
3655 string url = "https://" + filterContext . HttpContext . Request . Url . Host + filterContext . HttpContext . Request . RawUrl ;
37- filterContext . Result = new RedirectResult ( url ) ;
56+ filterContext . Result = new RedirectResult ( url , this . Permanent ) ;
3857 }
3958 }
4059}
Original file line number Diff line number Diff line change @@ -64,8 +64,36 @@ public void OnAuthorizationRedirectsIfRequestIsNotSecureAndMethodIsGet()
6464 RedirectResult result = authContext . Result as RedirectResult ;
6565
6666 // Assert
67+ Assert . IsFalse ( attr . Permanent ) ;
6768 Assert . NotNull ( result ) ;
6869 Assert . Equal ( "https://www.example.com/alpha/bravo/charlie?q=quux" , result . Url ) ;
70+ Assert . IsFalse ( result . Permanent ) ;
71+ }
72+
73+ [ Theory ]
74+ [ InlineData ( false ) ]
75+ [ InlineData ( true ) ]
76+ public void OnAuthorizationRedirectsIfPermanentConstructorParameterIsAndRequestIsNotSecureAndMethodIsGet ( bool permanent )
77+ {
78+ // Arrange
79+ Mock < AuthorizationContext > mockAuthContext = new Mock < AuthorizationContext > ( ) ;
80+ mockAuthContext . Setup ( c => c . HttpContext . Request . HttpMethod ) . Returns ( "get" ) ;
81+ mockAuthContext . Setup ( c => c . HttpContext . Request . IsSecureConnection ) . Returns ( false ) ;
82+ mockAuthContext . Setup ( c => c . HttpContext . Request . RawUrl ) . Returns ( "/alpha/bravo/charlie?q=quux" ) ;
83+ mockAuthContext . Setup ( c => c . HttpContext . Request . Url ) . Returns ( new Uri ( "http://www.example.com:8080/foo/bar/baz" ) ) ;
84+ AuthorizationContext authContext = mockAuthContext . Object ;
85+
86+ RequireHttpsAttribute attr = new RequireHttpsAttribute ( permanent ) ;
87+
88+ // Act
89+ attr . OnAuthorization ( authContext ) ;
90+ RedirectResult result = authContext . Result as RedirectResult ;
91+
92+ // Assert
93+ Assert . Equal ( permanent , attr . Permanent ) ;
94+ Assert . NotNull ( result ) ;
95+ Assert . Equal ( "https://www.example.com/alpha/bravo/charlie?q=quux" , result . Url ) ;
96+ Assert . Equal ( permanent , result . Permanent ) ;
6997 }
7098
7199 [ Fact ]
You can’t perform that action at this time.
0 commit comments