Skip to content

Commit b746f7b

Browse files
committed
Update more tests to new pattern
1 parent e94a3a2 commit b746f7b

File tree

12 files changed

+1012
-1051
lines changed

12 files changed

+1012
-1051
lines changed

src/Servers/IIS/IIS/test/Common.FunctionalTests/AspNetCorePortTests.cs

Lines changed: 47 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System;
5-
using System.Collections.Generic;
64
using System.Globalization;
7-
using System.Linq;
85
using System.Net;
96
using System.Net.Sockets;
10-
using System.Threading.Tasks;
11-
using Xunit;
127
using Microsoft.AspNetCore.Server.IIS.FunctionalTests.Utilities;
138
using Microsoft.AspNetCore.Server.IntegrationTesting;
149
using Microsoft.AspNetCore.Testing;
@@ -58,11 +53,11 @@ public async Task EnvVarInWebConfig_Valid(TestVariant variant)
5853
var port = GetUnusedRandomPort();
5954
deploymentParameters.WebConfigBasedEnvironmentVariables["ASPNETCORE_PORT"] = port.ToString(CultureInfo.InvariantCulture);
6055

61-
var deploymentResult = await DeployAsync(deploymentParameters);
62-
63-
var responseText = await deploymentResult.HttpClient.GetStringAsync("/ServerAddresses");
64-
65-
Assert.Equal(port, new Uri(responseText).Port);
56+
await RunTest(deploymentParameters, async deploymentResult =>
57+
{
58+
var responseText = await deploymentResult.HttpClient.GetStringAsync("/ServerAddresses");
59+
Assert.Equal(port, new Uri(responseText).Port);
60+
});
6661
}
6762

6863
[ConditionalTheory]
@@ -73,12 +68,13 @@ public async Task EnvVarInWebConfig_Empty(TestVariant variant)
7368
var deploymentParameters = Fixture.GetBaseDeploymentParameters(variant);
7469
deploymentParameters.WebConfigBasedEnvironmentVariables["ASPNETCORE_PORT"] = string.Empty;
7570

76-
var deploymentResult = await DeployAsync(deploymentParameters);
77-
78-
var responseText = await deploymentResult.HttpClient.GetStringAsync("/ServerAddresses");
71+
await RunTest(deploymentParameters, async deploymentResult =>
72+
{
73+
var responseText = await deploymentResult.HttpClient.GetStringAsync("/ServerAddresses");
7974

80-
// If env var is empty, ANCM should assign a random port (same as no env var)
81-
Assert.InRange(new Uri(responseText).Port, _minPort, _maxPort);
75+
// If env var is empty, ANCM should assign a random port (same as no env var)
76+
Assert.InRange(new Uri(responseText).Port, _minPort, _maxPort);
77+
});
8278
}
8379

8480
[ConditionalTheory]
@@ -89,11 +85,11 @@ public async Task EnvVarInWebConfig_Invalid(TestVariant variant, string port)
8985
var deploymentParameters = Fixture.GetBaseDeploymentParameters(variant);
9086
deploymentParameters.WebConfigBasedEnvironmentVariables["ASPNETCORE_PORT"] = port;
9187

92-
var deploymentResult = await DeployAsync(deploymentParameters);
93-
94-
var response = await deploymentResult.HttpClient.GetAsync("/ServerAddresses");
95-
96-
Assert.Equal(HttpStatusCode.BadGateway, response.StatusCode);
88+
await RunTest(deploymentParameters, async deploymentResult =>
89+
{
90+
var response = await deploymentResult.HttpClient.GetAsync("/ServerAddresses");
91+
Assert.Equal(HttpStatusCode.BadGateway, response.StatusCode);
92+
});
9793
}
9894

9995
[ConditionalTheory]
@@ -104,45 +100,46 @@ public async Task ShutdownMultipleTimesWorks(TestVariant variant)
104100
// Must publish to set env vars in web.config
105101
var deploymentParameters = Fixture.GetBaseDeploymentParameters(variant);
106102

107-
var deploymentResult = await DeployAsync(deploymentParameters);
108-
109-
// Shutdown once
110-
var response = await deploymentResult.HttpClient.GetAsync("/Shutdown");
111-
112-
// Wait for server to start again.
113-
int i;
114-
for (i = 0; i < 10; i++)
103+
await RunTest(deploymentParameters, async deploymentResult =>
115104
{
116-
// ANCM should eventually recover from being shutdown multiple times.
117-
response = await deploymentResult.HttpClient.GetAsync("/HelloWorld");
118-
if (response.IsSuccessStatusCode)
105+
// Shutdown once
106+
var response = await deploymentResult.HttpClient.GetAsync("/Shutdown");
107+
108+
// Wait for server to start again.
109+
int i;
110+
for (i = 0; i < 10; i++)
119111
{
120-
break;
112+
// ANCM should eventually recover from being shutdown multiple times.
113+
response = await deploymentResult.HttpClient.GetAsync("/HelloWorld");
114+
if (response.IsSuccessStatusCode)
115+
{
116+
break;
117+
}
121118
}
122-
}
123119

124-
if (i == 10)
125-
{
126-
// Didn't restart after 10 retries
127-
Assert.False(true);
128-
}
120+
if (i == 10)
121+
{
122+
// Didn't restart after 10 retries
123+
Assert.False(true);
124+
}
129125

130-
// Shutdown again
131-
response = await deploymentResult.HttpClient.GetAsync("/Shutdown");
126+
// Shutdown again
127+
response = await deploymentResult.HttpClient.GetAsync("/Shutdown");
132128

133-
// return if server starts again.
134-
for (i = 0; i < 10; i++)
135-
{
136-
// ANCM should eventually recover from being shutdown multiple times.
137-
response = await deploymentResult.HttpClient.GetAsync("/HelloWorld");
138-
if (response.IsSuccessStatusCode)
129+
// return if server starts again.
130+
for (i = 0; i < 10; i++)
139131
{
140-
return;
132+
// ANCM should eventually recover from being shutdown multiple times.
133+
response = await deploymentResult.HttpClient.GetAsync("/HelloWorld");
134+
if (response.IsSuccessStatusCode)
135+
{
136+
return;
137+
}
141138
}
142-
}
143139

144-
// Test failure if this happens.
145-
Assert.False(true);
140+
// Test failure if this happens.
141+
Assert.False(true);
142+
});
146143
}
147144

148145
private static int GetUnusedRandomPort()

src/Servers/IIS/IIS/test/Common.FunctionalTests/Infrastructure/IISFunctionalTestBase.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ public async Task<IISDeploymentResult> DeployApp(HostingModel hostingModel = Hos
2929
return await DeployAsync(deploymentParameters);
3030
}
3131

32+
public Task RunTest(HostingModel hostingModel, Action<IISDeploymentResult> testCode)
33+
=> RunTest(Fixture.GetBaseDeploymentParameters(hostingModel: hostingModel), testCode);
34+
3235
public void AddAppOffline(string appPath, string content = "The app is offline.")
3336
{
3437
File.WriteAllText(Path.Combine(appPath, "app_offline.htm"), content);

src/Servers/IIS/IIS/test/Common.FunctionalTests/Inprocess/MaxRequestBodySizeTests.cs

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@
33

44
using System.Net;
55
using System.Net.Http;
6-
using System.Threading.Tasks;
76
using Microsoft.AspNetCore.Server.IIS.FunctionalTests.Utilities;
87
using Microsoft.AspNetCore.Server.IntegrationTesting;
98
using Microsoft.AspNetCore.Server.IntegrationTesting.IIS;
109
using Microsoft.AspNetCore.Testing;
11-
using Xunit;
1210

1311
#if !IIS_FUNCTIONALS
1412
using Microsoft.AspNetCore.Server.IIS.FunctionalTests;
@@ -59,13 +57,14 @@ public async Task SetIISLimitMaxRequestBodySizeE2EWorks()
5957
.GetOrAdd("requestFiltering")
6058
.GetOrAdd("requestLimits", "maxAllowedContentLength", "1");
6159
});
62-
var deploymentResult = await DeployAsync(deploymentParameters);
63-
64-
var result = await deploymentResult.HttpClient.PostAsync("/ReadRequestBody", new StringContent("test"));
60+
await RunTest(deploymentParameters, async deploymentResult =>
61+
{
62+
var result = await deploymentResult.HttpClient.PostAsync("/ReadRequestBody", new StringContent("test"));
6563

66-
// IIS either returns a 404 or a 413 based on versions of IIS.
67-
// Check for both as we don't know which specific patch version.
68-
Assert.True(result.StatusCode == HttpStatusCode.NotFound || result.StatusCode == HttpStatusCode.RequestEntityTooLarge);
64+
// IIS either returns a 404 or a 413 based on versions of IIS.
65+
// Check for both as we don't know which specific patch version.
66+
Assert.True(result.StatusCode == HttpStatusCode.NotFound || result.StatusCode == HttpStatusCode.RequestEntityTooLarge);
67+
});
6968
}
7069

7170
[ConditionalFact]
@@ -82,11 +81,11 @@ public async Task SetIISLimitMaxRequestBodySizeE2EWorksWithLargerLimit()
8281
.GetOrAdd("requestFiltering")
8382
.GetOrAdd("requestLimits", "maxAllowedContentLength", "100000000");
8483
});
85-
var deploymentResult = await DeployAsync(deploymentParameters);
86-
87-
var result = await deploymentResult.HttpClient.PostAsync("/ReadRequestBodyLarger", new StringContent(new string('a', 100000000)));
88-
89-
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
84+
await RunTest(deploymentParameters, async deploymentResult =>
85+
{
86+
var result = await deploymentResult.HttpClient.PostAsync("/ReadRequestBodyLarger", new StringContent(new string('a', 100000000)));
87+
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
88+
});
9089
}
9190

9291
[ConditionalFact]
@@ -103,31 +102,32 @@ public async Task SetIISLimitMaxRequestBodySizeE2EWorksWithIntMaxValue()
103102
.GetOrAdd("requestFiltering")
104103
.GetOrAdd("requestLimits", "maxAllowedContentLength", "4294967295");
105104
});
106-
var deploymentResult = await DeployAsync(deploymentParameters);
107-
108-
var result = await deploymentResult.HttpClient.PostAsync("/ReadRequestBodyLarger", new StringContent(new string('a', 10000)));
109-
110-
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
105+
await RunTest(deploymentParameters, async deploymentResult =>
106+
{
107+
var result = await deploymentResult.HttpClient.PostAsync("/ReadRequestBodyLarger", new StringContent(new string('a', 10000)));
108+
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
109+
});
111110
}
112111

113112
[ConditionalFact]
114113
[RequiresNewHandler]
115114
public async Task IISRejectsContentLengthTooLargeByDefault()
116115
{
117116
var deploymentParameters = Fixture.GetBaseDeploymentParameters();
118-
var deploymentResult = await DeployAsync(deploymentParameters);
119-
120-
using (var connection = new TestConnection(deploymentResult.HttpClient.BaseAddress.Port))
117+
await RunTest(deploymentParameters, async deploymentResult =>
121118
{
122-
await connection.Send(
123-
"POST /HelloWorld HTTP/1.1",
124-
$"Content-Length: 30000001",
125-
"Host: localhost",
126-
"",
127-
"A");
128-
var requestLine = await connection.ReadLineAsync();
129-
Assert.True(requestLine.Contains("404") || requestLine.Contains("413"));
130-
}
119+
using (var connection = new TestConnection(deploymentResult.HttpClient.BaseAddress.Port))
120+
{
121+
await connection.Send(
122+
"POST /HelloWorld HTTP/1.1",
123+
$"Content-Length: 30000001",
124+
"Host: localhost",
125+
"",
126+
"A");
127+
var requestLine = await connection.ReadLineAsync();
128+
Assert.True(requestLine.Contains("404") || requestLine.Contains("413"));
129+
}
130+
});
131131
}
132132

133133
[ConditionalFact]
@@ -149,18 +149,19 @@ public async Task SetIISLimitMaxRequestBodyLogsWarning()
149149
.GetOrAdd("requestFiltering")
150150
.GetOrAdd("requestLimits", "maxAllowedContentLength", "1");
151151
});
152-
var deploymentResult = await DeployAsync(deploymentParameters);
153-
154-
var result = await deploymentResult.HttpClient.PostAsync("/IncreaseRequestLimit", new StringContent("1"));
155-
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
152+
await RunTest(deploymentParameters, async deploymentResult =>
153+
{
154+
var result = await deploymentResult.HttpClient.PostAsync("/IncreaseRequestLimit", new StringContent("1"));
155+
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
156156

157-
StopServer();
157+
StopServer();
158158

159-
if (deploymentParameters.ServerType == ServerType.IISExpress)
160-
{
161-
Assert.Single(TestSink.Writes, w => w.Message.Contains("Increasing the MaxRequestBodySize conflicts with the max value for IIS limit maxAllowedContentLength." +
162-
" HTTP requests that have a content length greater than maxAllowedContentLength will still be rejected by IIS." +
163-
" You can disable the limit by either removing or setting the maxAllowedContentLength value to a higher limit."));
164-
}
159+
if (deploymentParameters.ServerType == ServerType.IISExpress)
160+
{
161+
Assert.Single(TestSink.Writes, w => w.Message.Contains("Increasing the MaxRequestBodySize conflicts with the max value for IIS limit maxAllowedContentLength." +
162+
" HTTP requests that have a content length greater than maxAllowedContentLength will still be rejected by IIS." +
163+
" You can disable the limit by either removing or setting the maxAllowedContentLength value to a higher limit."));
164+
}
165+
});
165166
}
166167
}
Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System;
54
using System.Net;
65
using System.Net.Http;
7-
using System.Threading.Tasks;
86
using Microsoft.AspNetCore.Server.IIS.FunctionalTests.Utilities;
97
using Microsoft.AspNetCore.Server.IntegrationTesting;
108
using Microsoft.AspNetCore.Server.IntegrationTesting.IIS;
119
using Microsoft.AspNetCore.Testing;
12-
using Xunit;
1310

1411
#if !IIS_FUNCTIONALS
1512
using Microsoft.AspNetCore.Server.IIS.FunctionalTests;
@@ -40,15 +37,16 @@ public async Task Latin1Works()
4037
var deploymentParameters = Fixture.GetBaseDeploymentParameters();
4138
deploymentParameters.TransformArguments((a, _) => $"{a} AddLatin1");
4239

43-
var deploymentResult = await DeployAsync(deploymentParameters);
44-
45-
var client = new HttpClient(new LoggingHandler(new WinHttpHandler() { SendTimeout = TimeSpan.FromMinutes(3) }, deploymentResult.Logger));
40+
await RunTest(deploymentParameters, async deploymentResult =>
41+
{
42+
var client = new HttpClient(new LoggingHandler(new WinHttpHandler() { SendTimeout = TimeSpan.FromMinutes(3) }, deploymentResult.Logger));
4643

47-
var requestMessage = new HttpRequestMessage(HttpMethod.Get, $"{deploymentResult.ApplicationBaseUri}Latin1");
48-
requestMessage.Headers.Add("foo", "£");
44+
var requestMessage = new HttpRequestMessage(HttpMethod.Get, $"{deploymentResult.ApplicationBaseUri}Latin1");
45+
requestMessage.Headers.Add("foo", "£");
4946

50-
var result = await client.SendAsync(requestMessage);
51-
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
47+
var result = await client.SendAsync(requestMessage);
48+
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
49+
});
5250
}
5351

5452
[ConditionalFact]
@@ -58,15 +56,16 @@ public async Task Latin1ReplacedWithoutAppContextSwitch()
5856
var deploymentParameters = Fixture.GetBaseDeploymentParameters();
5957
deploymentParameters.TransformArguments((a, _) => $"{a}");
6058

61-
var deploymentResult = await DeployAsync(deploymentParameters);
62-
63-
var client = new HttpClient(new LoggingHandler(new WinHttpHandler() { SendTimeout = TimeSpan.FromMinutes(3) }, deploymentResult.Logger));
59+
await RunTest(deploymentParameters, async deploymentResult =>
60+
{
61+
var client = new HttpClient(new LoggingHandler(new WinHttpHandler() { SendTimeout = TimeSpan.FromMinutes(3) }, deploymentResult.Logger));
6462

65-
var requestMessage = new HttpRequestMessage(HttpMethod.Get, $"{deploymentResult.ApplicationBaseUri}InvalidCharacter");
66-
requestMessage.Headers.Add("foo", "£");
63+
var requestMessage = new HttpRequestMessage(HttpMethod.Get, $"{deploymentResult.ApplicationBaseUri}InvalidCharacter");
64+
requestMessage.Headers.Add("foo", "£");
6765

68-
var result = await client.SendAsync(requestMessage);
69-
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
66+
var result = await client.SendAsync(requestMessage);
67+
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
68+
});
7069
}
7170

7271
[ConditionalFact]
@@ -75,20 +74,20 @@ public async Task Latin1InvalidCharacters_HttpSysRejects()
7574
{
7675
var deploymentParameters = Fixture.GetBaseDeploymentParameters();
7776
deploymentParameters.TransformArguments((a, _) => $"{a} AddLatin1");
78-
79-
var deploymentResult = await DeployAsync(deploymentParameters);
80-
81-
using (var connection = new TestConnection(deploymentResult.HttpClient.BaseAddress.Port))
77+
await RunTest(deploymentParameters, async deploymentResult =>
8278
{
83-
await connection.Send(
84-
"GET /ReadAndFlushEcho HTTP/1.1",
85-
"Host: localhost",
86-
"Connection: close",
87-
"foo: £\0a",
88-
"",
89-
"");
90-
91-
await connection.ReceiveStartsWith("HTTP/1.1 400 Bad Request");
92-
}
79+
using (var connection = new TestConnection(deploymentResult.HttpClient.BaseAddress.Port))
80+
{
81+
await connection.Send(
82+
"GET /ReadAndFlushEcho HTTP/1.1",
83+
"Host: localhost",
84+
"Connection: close",
85+
"foo: £\0a",
86+
"",
87+
"");
88+
89+
await connection.ReceiveStartsWith("HTTP/1.1 400 Bad Request");
90+
}
91+
});
9392
}
9493
}

0 commit comments

Comments
 (0)