This repository was archived by the owner on Nov 20, 2018. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +23
-2
lines changed
src/Microsoft.AspNet.Http
test/Microsoft.AspNet.Http.Tests Expand file tree Collapse file tree 2 files changed +23
-2
lines changed Original file line number Diff line number Diff line change @@ -9,6 +9,10 @@ public class HttpContextFactory : IHttpContextFactory
9
9
{
10
10
private IHttpContextAccessor _httpContextAccessor ;
11
11
12
+ public HttpContextFactory ( ) : this ( httpContextAccessor : null )
13
+ {
14
+ }
15
+
12
16
public HttpContextFactory ( IHttpContextAccessor httpContextAccessor )
13
17
{
14
18
_httpContextAccessor = httpContextAccessor ;
@@ -17,13 +21,19 @@ public HttpContextFactory(IHttpContextAccessor httpContextAccessor)
17
21
public HttpContext Create ( IFeatureCollection featureCollection )
18
22
{
19
23
var httpContext = new DefaultHttpContext ( featureCollection ) ;
20
- _httpContextAccessor . HttpContext = httpContext ;
24
+ if ( _httpContextAccessor != null )
25
+ {
26
+ _httpContextAccessor . HttpContext = httpContext ;
27
+ }
21
28
return httpContext ;
22
29
}
23
30
24
31
public void Dispose ( HttpContext httpContext )
25
32
{
26
- _httpContextAccessor . HttpContext = null ;
33
+ if ( _httpContextAccessor != null )
34
+ {
35
+ _httpContextAccessor . HttpContext = null ;
36
+ }
27
37
}
28
38
}
29
39
}
Original file line number Diff line number Diff line change @@ -21,5 +21,16 @@ public void CreateHttpContextSetsHttpContextAccessor()
21
21
// Assert
22
22
Assert . True ( ReferenceEquals ( context , accessor . HttpContext ) ) ;
23
23
}
24
+
25
+ [ Fact ]
26
+ public void AllowsCreatingContextWithoutSettingAccessor ( )
27
+ {
28
+ // Arrange
29
+ var contextFactory = new HttpContextFactory ( ) ;
30
+
31
+ // Act & Assert
32
+ var context = contextFactory . Create ( new FeatureCollection ( ) ) ;
33
+ contextFactory . Dispose ( context ) ;
34
+ }
24
35
}
25
36
}
You can’t perform that action at this time.
0 commit comments