Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Commit d03a851

Browse files
committed
Use MvcTextFixture as much as possible
- #3066 - reduce `TestServer` -> `PhysicalFileProvider` -> `FileSystemWatcher` count enough to run with Core CLR on Linux - remove use of `HttpClient.DefaultRequestHeaders`; any client change affects other tests - remove use of `RequestBuilder` class; creates a per-test `HttpClient` and requires the `TestServer` - updated a few expectations because `CommonTestEncoder` does JavaScript a bit differently - "JavaScriptEncode[[...]]" -> "JavaScriptStringEncode[[...]]" - side benefit: xUnit reports functional tests execute for only ~12.4s; was >30s before this change Infrastructure: Enhance `MvcTestFixture` - handle `ConfigureServices()` methods that are not `void` - handle `Configure(IApplicationBuilder, ILoggerFactory)` - ensure server is initialized with consistent `CurrentCulture` and `CurrentUICulture` - add `FilteredDefaultAssemblyProviderFixture<TStartup>` and `MvcEncodedTestFixture<TStartup>` - add `MvcTextFixture.AddAdditionalServices()` extension point supporting these - do not expose the `TestServer`; an anti-pattern for tests to manipulate the server - update class names to match containing files - use existing `TestApplicationEnvironment` - apply some `MvcTestFixture` improvements to the shared `TestApplicationEnvironment` class - remove unused methods from `TestHelper` nits: - touched-up some leftover `_app` &c declarations to be more explicit and minimize `using`s - moved statements into correct sections of methods in `RoutingTests` - removed `TestLoggerFactory` and related classes from `TagHelperSampleTest`
1 parent 6459fb0 commit d03a851

File tree

88 files changed

+2337
-4398
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+2337
-4398
lines changed

test/Microsoft.AspNet.Mvc.FunctionalTests/ActionResultTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212

1313
namespace Microsoft.AspNet.Mvc.FunctionalTests
1414
{
15-
public class ActionResultTests : IClassFixture<MvcFixture<ActionResultsWebSite.Startup>>
15+
public class ActionResultTests : IClassFixture<MvcTestFixture<ActionResultsWebSite.Startup>>
1616
{
17-
public ActionResultTests(MvcFixture<ActionResultsWebSite.Startup> fixture)
17+
public ActionResultTests(MvcTestFixture<ActionResultsWebSite.Startup> fixture)
1818
{
1919
Client = fixture.Client;
2020
}

test/Microsoft.AspNet.Mvc.FunctionalTests/ActivatorTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
namespace Microsoft.AspNet.Mvc.FunctionalTests
1010
{
11-
public class ActivatorTests : IClassFixture<MvcFixture<ActivatorWebSite.Startup>>
11+
public class ActivatorTests : IClassFixture<MvcTestFixture<ActivatorWebSite.Startup>>
1212
{
13-
public ActivatorTests(MvcFixture<ActivatorWebSite.Startup> fixture)
13+
public ActivatorTests(MvcTestFixture<ActivatorWebSite.Startup> fixture)
1414
{
1515
Client = fixture.Client;
1616
}

test/Microsoft.AspNet.Mvc.FunctionalTests/AntiforgeryTests.cs

Lines changed: 28 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,31 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
using System;
54
using System.Collections.Generic;
65
using System.Linq;
76
using System.Net;
87
using System.Net.Http;
98
using System.Threading.Tasks;
10-
using Microsoft.AspNet.Builder;
11-
using Microsoft.Framework.DependencyInjection;
129
using Xunit;
1310

1411
namespace Microsoft.AspNet.Mvc.FunctionalTests
1512
{
16-
public class AntiforgeryTests
13+
public class AntiforgeryTests : IClassFixture<MvcTestFixture<AntiforgeryTokenWebSite.Startup>>
1714
{
18-
private const string SiteName = nameof(AntiforgeryTokenWebSite);
19-
private readonly Action<IApplicationBuilder> _app = new AntiforgeryTokenWebSite.Startup().Configure;
20-
private readonly Action<IServiceCollection> _configureServices = new AntiforgeryTokenWebSite.Startup().ConfigureServices;
15+
public AntiforgeryTests(MvcTestFixture<AntiforgeryTokenWebSite.Startup> fixture)
16+
{
17+
Client = fixture.Client;
18+
}
19+
20+
public HttpClient Client { get; }
2121

2222
[Fact]
2323
public async Task MultipleAFTokensWithinTheSamePage_GeneratesASingleCookieToken()
2424
{
25-
// Arrange
26-
var server = TestHelper.CreateServer(_app, SiteName, _configureServices);
27-
var client = server.CreateClient();
25+
// Arrange & Act
26+
var response = await Client.GetAsync("http://localhost/Account/Login");
2827

29-
// Act
30-
var response = await client.GetAsync("http://localhost/Account/Login");
31-
32-
//Assert
28+
// Assert
3329
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
3430
var header = Assert.Single(response.Headers.GetValues("X-Frame-Options"));
3531
Assert.Equal("SAMEORIGIN", header);
@@ -45,11 +41,8 @@ public async Task MultipleAFTokensWithinTheSamePage_GeneratesASingleCookieToken(
4541
public async Task MultipleFormPostWithingASingleView_AreAllowed()
4642
{
4743
// Arrange
48-
var server = TestHelper.CreateServer(_app, SiteName, _configureServices);
49-
var client = server.CreateClient();
50-
51-
// do a get response.
52-
var getResponse = await client.GetAsync("http://localhost/Account/Login");
44+
// Do a get request.
45+
var getResponse = await Client.GetAsync("http://localhost/Account/Login");
5346
var responseBody = await getResponse.Content.ReadAsStringAsync();
5447

5548
// Get the AF token for the second login. If the cookies are generated twice(i.e are different),
@@ -69,7 +62,7 @@ public async Task MultipleFormPostWithingASingleView_AreAllowed()
6962
request.Content = new FormUrlEncodedContent(nameValueCollection);
7063

7164
// Act
72-
var response = await client.SendAsync(request);
65+
var response = await Client.SendAsync(request);
7366

7467
// Assert
7568
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
@@ -80,10 +73,7 @@ public async Task MultipleFormPostWithingASingleView_AreAllowed()
8073
public async Task InvalidCookieToken_Throws()
8174
{
8275
// Arrange
83-
var server = TestHelper.CreateServer(_app, SiteName, _configureServices);
84-
var client = server.CreateClient();
85-
86-
var getResponse = await client.GetAsync("http://localhost/Account/Login");
76+
var getResponse = await Client.GetAsync("http://localhost/Account/Login");
8777
var responseBody = await getResponse.Content.ReadAsStringAsync();
8878
var formToken = AntiforgeryTestHelper.RetrieveAntiforgeryToken(responseBody, "Account/Login");
8979

@@ -101,7 +91,7 @@ public async Task InvalidCookieToken_Throws()
10191
request.Content = new FormUrlEncodedContent(nameValueCollection);
10292

10393
// Act
104-
var response = await client.SendAsync(request);
94+
var response = await Client.SendAsync(request);
10595

10696
// Assert
10797
var exception = response.GetServerException();
@@ -112,10 +102,7 @@ public async Task InvalidCookieToken_Throws()
112102
public async Task InvalidFormToken_Throws()
113103
{
114104
// Arrange
115-
var server = TestHelper.CreateServer(_app, SiteName, _configureServices);
116-
var client = server.CreateClient();
117-
118-
var getResponse = await client.GetAsync("http://localhost/Account/Login");
105+
var getResponse = await Client.GetAsync("http://localhost/Account/Login");
119106
var responseBody = await getResponse.Content.ReadAsStringAsync();
120107
var cookieToken = AntiforgeryTestHelper.RetrieveAntiforgeryCookie(getResponse);
121108
var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/Account/Login");
@@ -131,7 +118,7 @@ public async Task InvalidFormToken_Throws()
131118
request.Content = new FormUrlEncodedContent(nameValueCollection);
132119

133120
// Act
134-
var response = await client.SendAsync(request);
121+
var response = await Client.SendAsync(request);
135122

136123
// Assert
137124
var exception = response.GetServerException();
@@ -142,16 +129,13 @@ public async Task InvalidFormToken_Throws()
142129
public async Task IncompatibleCookieToken_Throws()
143130
{
144131
// Arrange
145-
var server = TestHelper.CreateServer(_app, SiteName, _configureServices);
146-
var client = server.CreateClient();
147-
148132
// do a get response.
149133
// We do two requests to get two different sets of antiforgery cookie and token values.
150-
var getResponse1 = await client.GetAsync("http://localhost/Account/Login");
134+
var getResponse1 = await Client.GetAsync("http://localhost/Account/Login");
151135
var responseBody1 = await getResponse1.Content.ReadAsStringAsync();
152136
var formToken1 = AntiforgeryTestHelper.RetrieveAntiforgeryToken(responseBody1, "Account/Login");
153137

154-
var getResponse2 = await client.GetAsync("http://localhost/Account/Login");
138+
var getResponse2 = await Client.GetAsync("http://localhost/Account/Login");
155139
var responseBody2 = await getResponse2.Content.ReadAsStringAsync();
156140
var cookieToken2 = AntiforgeryTestHelper.RetrieveAntiforgeryCookie(getResponse2);
157141

@@ -169,7 +153,7 @@ public async Task IncompatibleCookieToken_Throws()
169153
request.Content = new FormUrlEncodedContent(nameValueCollection);
170154

171155
// Act
172-
var response = await client.SendAsync(request);
156+
var response = await Client.SendAsync(request);
173157

174158
// Assert
175159
var exception = response.GetServerException();
@@ -180,11 +164,8 @@ public async Task IncompatibleCookieToken_Throws()
180164
public async Task MissingCookieToken_Throws()
181165
{
182166
// Arrange
183-
var server = TestHelper.CreateServer(_app, SiteName, _configureServices);
184-
var client = server.CreateClient();
185-
186167
// do a get response.
187-
var getResponse = await client.GetAsync("http://localhost/Account/Login");
168+
var getResponse = await Client.GetAsync("http://localhost/Account/Login");
188169
var responseBody = await getResponse.Content.ReadAsStringAsync();
189170
var formToken = AntiforgeryTestHelper.RetrieveAntiforgeryToken(responseBody, "Account/Login");
190171
var cookieTokenKey = AntiforgeryTestHelper.RetrieveAntiforgeryCookie(getResponse).Key;
@@ -200,7 +181,7 @@ public async Task MissingCookieToken_Throws()
200181
request.Content = new FormUrlEncodedContent(nameValueCollection);
201182

202183
// Act
203-
var response = await client.SendAsync(request);
184+
var response = await Client.SendAsync(request);
204185

205186
// Assert
206187
var exception = response.GetServerException();
@@ -213,9 +194,7 @@ public async Task MissingCookieToken_Throws()
213194
public async Task MissingAFToken_Throws()
214195
{
215196
// Arrange
216-
var server = TestHelper.CreateServer(_app, SiteName, _configureServices);
217-
var client = server.CreateClient();
218-
var getResponse = await client.GetAsync("http://localhost/Account/Login");
197+
var getResponse = await Client.GetAsync("http://localhost/Account/Login");
219198
var responseBody = await getResponse.Content.ReadAsStringAsync();
220199
var cookieToken = AntiforgeryTestHelper.RetrieveAntiforgeryCookie(getResponse);
221200

@@ -230,7 +209,7 @@ public async Task MissingAFToken_Throws()
230209
request.Content = new FormUrlEncodedContent(nameValueCollection);
231210

232211
// Act
233-
var response = await client.SendAsync(request);
212+
var response = await Client.SendAsync(request);
234213

235214
// Assert
236215
var exception = response.GetServerException();
@@ -241,12 +220,8 @@ public async Task MissingAFToken_Throws()
241220
[Fact]
242221
public async Task SetCookieAndHeaderBeforeFlushAsync_GeneratesCookieTokenAndHeader()
243222
{
244-
// Arrange
245-
var server = TestHelper.CreateServer(_app, SiteName, _configureServices);
246-
var client = server.CreateClient();
247-
248-
// Act
249-
var response = await client.GetAsync("http://localhost/Account/FlushAsyncLogin");
223+
// Arrange & Act
224+
var response = await Client.GetAsync("http://localhost/Account/FlushAsyncLogin");
250225

251226
// Assert
252227
var header = Assert.Single(response.Headers.GetValues("X-Frame-Options"));
@@ -260,11 +235,8 @@ public async Task SetCookieAndHeaderBeforeFlushAsync_GeneratesCookieTokenAndHead
260235
public async Task SetCookieAndHeaderBeforeFlushAsync_PostToForm()
261236
{
262237
// Arrange
263-
var server = TestHelper.CreateServer(_app, SiteName, _configureServices);
264-
var client = server.CreateClient();
265-
266238
// do a get response.
267-
var getResponse = await client.GetAsync("http://localhost/Account/FlushAsyncLogin");
239+
var getResponse = await Client.GetAsync("http://localhost/Account/FlushAsyncLogin");
268240
var responseBody = await getResponse.Content.ReadAsStringAsync();
269241

270242
var formToken = AntiforgeryTestHelper.RetrieveAntiforgeryToken(responseBody, "Account/FlushAsyncLogin");
@@ -282,7 +254,7 @@ public async Task SetCookieAndHeaderBeforeFlushAsync_PostToForm()
282254
request.Content = new FormUrlEncodedContent(nameValueCollection);
283255

284256
// Act
285-
var response = await client.SendAsync(request);
257+
var response = await Client.SendAsync(request);
286258

287259
// Assert
288260
Assert.Equal(HttpStatusCode.OK, response.StatusCode);

0 commit comments

Comments
 (0)