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
9
9
[ AttributeUsage ( AttributeTargets . Class | AttributeTargets . Method , Inherited = true , AllowMultiple = false ) ]
10
10
public class RequireHttpsAttribute : FilterAttribute , IAuthorizationFilter
11
11
{
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
+
12
31
public virtual void OnAuthorization ( AuthorizationContext filterContext )
13
32
{
14
33
if ( filterContext == null )
@@ -34,7 +53,7 @@ protected virtual void HandleNonHttpsRequest(AuthorizationContext filterContext)
34
53
35
54
// redirect to HTTPS version of page
36
55
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 ) ;
38
57
}
39
58
}
40
59
}
Original file line number Diff line number Diff line change @@ -64,8 +64,36 @@ public void OnAuthorizationRedirectsIfRequestIsNotSecureAndMethodIsGet()
64
64
RedirectResult result = authContext . Result as RedirectResult ;
65
65
66
66
// Assert
67
+ Assert . IsFalse ( attr . Permanent ) ;
67
68
Assert . NotNull ( result ) ;
68
69
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 ) ;
69
97
}
70
98
71
99
[ Fact ]
You can’t perform that action at this time.
0 commit comments