Skip to content

Commit 8a3fa28

Browse files
committed
Separated part of the logic concerning registering the HTTP context (#358)
1 parent 5d6f8ff commit 8a3fa28

File tree

6 files changed

+14
-63
lines changed

6 files changed

+14
-63
lines changed

src/MyTested.AspNetCore.Mvc.Abstractions/Internal/Application/ApplicationBuilderMock.cs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Collections.Generic;
55
using System.Linq;
66
using System.Threading.Tasks;
7-
using Http;
87
using Microsoft.AspNetCore.Builder;
98
using Microsoft.AspNetCore.Http;
109
using Microsoft.AspNetCore.Http.Features;
@@ -81,6 +80,9 @@ public IServiceProvider ApplicationServices
8180
/// <returns>The same <see cref="IApplicationBuilder"/>.</returns>
8281
public IApplicationBuilder Use(Func<RequestDelegate, RequestDelegate> middleware)
8382
{
83+
this.ExtractEndpointRoutes(middleware);
84+
this.ExtractLegacyRoutes(middleware);
85+
8486
this.components.Add(middleware);
8587

8688
return this;
@@ -112,17 +114,6 @@ public RequestDelegate Build()
112114
return app;
113115
}
114116

115-
public void ExtractRouteConfiguration()
116-
{
117-
this.Routes = new RouteCollection();
118-
119-
this.components.ForEach(component =>
120-
{
121-
this.ExtractEndpointRoutes(component);
122-
this.ExtractLegacyRoutes(component);
123-
});
124-
}
125-
126117
private T GetProperty<T>(string key)
127118
=> this.Properties.TryGetValue(key, out var value) ? (T)value : default;
128119

src/MyTested.AspNetCore.Mvc.Abstractions/Internal/Application/TestApplicationRouter.cs

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,17 @@
33
using System;
44
using System.Collections.Generic;
55
using System.Linq;
6-
using Http;
76
using Routing;
87
using Microsoft.AspNetCore.Builder;
98
using Microsoft.AspNetCore.Hosting;
109
using Microsoft.AspNetCore.Http;
11-
using Microsoft.AspNetCore.Http.Features;
1210
using Microsoft.AspNetCore.Routing;
1311
using Microsoft.Extensions.DependencyInjection;
1412
using Utilities.Extensions;
1513

1614
public static partial class TestApplication
1715
{
1816
private static volatile IRouter router;
19-
private static volatile RequestDelegate pipeline;
2017

2118
public static IRouter Router
2219
{
@@ -27,15 +24,6 @@ public static IRouter Router
2724
}
2825
}
2926

30-
public static RequestDelegate Pipeline
31-
{
32-
get
33-
{
34-
TryLockedInitialization();
35-
return pipeline;
36-
}
37-
}
38-
3927
internal static Action<IRouteBuilder> AdditionalRouting { get; set; }
4028

4129
private static void PrepareApplicationAndRouting()
@@ -57,38 +45,11 @@ private static void PrepareApplicationAndRouting()
5745

5846
AdditionalApplicationConfiguration?.Invoke(applicationBuilder);
5947

60-
PreparePipelineAndFeatures(applicationBuilder);
6148
PrepareRouter(applicationBuilder);
6249
}
6350

64-
private static void PreparePipelineAndFeatures(IApplicationBuilder applicationBuilder)
65-
{
66-
pipeline = applicationBuilder.Build();
67-
68-
var httpContext = new DefaultHttpContext();
69-
70-
var featuresToRemove = new FeatureCollection(httpContext.Features);
71-
72-
pipeline(httpContext);
73-
74-
var defaultFeatures = new FeatureCollection();
75-
76-
httpContext.Features.ForEach(feature =>
77-
{
78-
var (type, value) = feature;
79-
if (featuresToRemove[type] == null)
80-
{
81-
defaultFeatures[type] = value;
82-
}
83-
});
84-
85-
TestHelper.DefaultHttpFeatures = defaultFeatures;
86-
}
87-
8851
private static void PrepareRouter(ApplicationBuilderMock applicationBuilder)
8952
{
90-
applicationBuilder.ExtractRouteConfiguration();
91-
9253
var routeBuilder = new RouteBuilder(applicationBuilder)
9354
{
9455
DefaultHandler = RouteHandlerMock.Null

src/MyTested.AspNetCore.Mvc.Abstractions/Internal/Http/HttpContextMock.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class HttpContextMock : HttpContext
2525
public HttpContextMock()
2626
: this(new FeatureCollection())
2727
{
28-
this.PrepareFeatures();
28+
this.PrepareDefaultFeatures();
2929
this.PrepareDefaultValues();
3030
}
3131

@@ -50,7 +50,7 @@ private HttpContextMock(HttpContext context)
5050
{
5151
CommonValidator.CheckForNullReference(context, nameof(HttpContext));
5252

53-
this.PrepareFeatures();
53+
this.PrepareDefaultFeatures();
5454
this.PrepareData(context);
5555
this.PrepareDefaultValues();
5656
}
@@ -119,7 +119,7 @@ public override ClaimsPrincipal User
119119
public static HttpContextMock From(HttpContext httpContext)
120120
=> new HttpContextMock(httpContext);
121121

122-
private void PrepareFeatures()
122+
private void PrepareDefaultFeatures()
123123
{
124124
if (this.Features.Get<IHttpRequestFeature>() == null)
125125
{

src/MyTested.AspNetCore.Mvc.Abstractions/Internal/TestHelper.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ public static class TestHelper
1616
{
1717
public static Action GlobalTestCleanup { get; set; }
1818

19-
public static IFeatureCollection DefaultHttpFeatures { get; set; } = new FeatureCollection();
20-
2119
public static ISet<IHttpFeatureRegistrationPlugin> HttpFeatureRegistrationPlugins { get; }
2220
= new HashSet<IHttpFeatureRegistrationPlugin>();
2321

@@ -28,8 +26,7 @@ public static HttpContextMock CreateHttpContextMock()
2826
{
2927
var httpContextFactory = TestServiceProvider.GetService<IHttpContextFactory>();
3028
var httpContext = httpContextFactory != null
31-
? HttpContextMock.From(httpContextFactory
32-
.Create(new FeatureCollection(DefaultHttpFeatures)))
29+
? HttpContextMock.From(httpContextFactory.Create(new FeatureCollection()))
3330
: new HttpContextMock();
3431

3532
SetHttpContextToAccessor(httpContext);

test/MyTested.AspNetCore.Mvc.Http.Test/ServicesTests.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
namespace MyTested.AspNetCore.Mvc.Test
1+
using Microsoft.AspNetCore.Hosting;
2+
using MyTested.AspNetCore.Mvc.Test.Setups.Http;
3+
using MyTested.AspNetCore.Mvc.Test.Setups.StartupFilters;
4+
5+
namespace MyTested.AspNetCore.Mvc.Test
26
{
37
using System.Collections.Generic;
48
using System.Linq;
@@ -41,7 +45,7 @@ public void CustomConfigureOptionsShouldNotOverrideTheDefaultTestOnes()
4145

4246

4347
[Fact]
44-
public void IHttpContextAccessorShouldWorkCorrectlySynchronously()
48+
public void HttpContextAccessorShouldWorkCorrectlySynchronously()
4549
{
4650
MyApplication
4751
.StartsFrom<DefaultStartup>()
@@ -80,7 +84,7 @@ public void IHttpContextAccessorShouldWorkCorrectlySynchronously()
8084
}
8185

8286
[Fact]
83-
public void IHttpContextAccessorShouldWorkCorrectlyAsynchronously()
87+
public void HttpContextAccessorShouldWorkCorrectlyAsynchronously()
8488
{
8589
MyApplication
8690
.StartsFrom<DefaultStartup>()

test/MyTested.AspNetCore.Mvc.Pipeline.Test/TestStartup.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
namespace MyTested.AspNetCore.Mvc.Test
22
{
3-
using Microsoft.AspNetCore.Builder;
4-
using Microsoft.Extensions.DependencyInjection;
53
using Setups;
64

75
public class TestStartup : DefaultStartup

0 commit comments

Comments
 (0)