Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit c732b84

Browse files
committed
PR feedback, add ssl checks
1 parent c9de86e commit c732b84

File tree

2 files changed

+127
-111
lines changed

2 files changed

+127
-111
lines changed

src/System.Net.Requests/tests/HttpWebRequestTest.cs

Lines changed: 123 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -223,23 +223,26 @@ public void AllowReadStreamBuffering_SetTrueThenGet_ExpectTrue(Uri remoteServer)
223223
Assert.True(request.AllowReadStreamBuffering);
224224
}
225225

226-
[Fact]
227-
public async Task ContentLength_Get_ExpectSameAsGetResponseStream()
226+
[Theory]
227+
[InlineData(true)]
228+
[InlineData(false)]
229+
public async Task ContentLength_Get_ExpectSameAsGetResponseStream(bool useSsl)
228230
{
229-
await LoopbackServer.CreateServerAsync(async (server, uri) =>
231+
var options = new LoopbackServer.Options { UseSsl = useSsl };
232+
233+
await LoopbackServer.CreateClientAndServerAsync(async uri =>
230234
{
231235
HttpWebRequest request = WebRequest.CreateHttp(uri);
232-
Task<WebResponse> getResponse = request.GetResponseAsync();
233-
await server.HandleRequestAsync();
234-
using (WebResponse response = await getResponse)
236+
request.ServerCertificateValidationCallback = (a, b, c, d) => true;
237+
using (WebResponse response = await request.GetResponseAsync())
235238
using (Stream myStream = response.GetResponseStream())
236239
using (var sr = new StreamReader(myStream))
237240
{
238241
string strContent = sr.ReadToEnd();
239242
long length = response.ContentLength;
240243
Assert.Equal(strContent.Length, length);
241244
}
242-
});
245+
}, server => server.HandleRequestAsync(), options);
243246
}
244247

245248
[Theory, MemberData(nameof(EchoServers))]
@@ -282,7 +285,7 @@ await LoopbackServer.CreateServerAsync(async (server, uri) =>
282285
{
283286
HttpWebRequest request = WebRequest.CreateHttp(uri);
284287
Task<WebResponse> getResponse = request.GetResponseAsync();
285-
await server.HandleRequestAsync();
288+
await server.AcceptConnectionSendResponseAndCloseAsync();
286289
using (WebResponse response = await getResponse)
287290
{
288291
Assert.Throws<InvalidOperationException>(() => request.AutomaticDecompression = DecompressionMethods.Deflate);
@@ -1116,7 +1119,7 @@ await LoopbackServer.CreateServerAsync((server, url) =>
11161119
request.BeginGetRequestStream(null, null);
11171120
});
11181121

1119-
return Task.FromResult<object>(null);
1122+
return Task.CompletedTask;
11201123
});
11211124
}
11221125

@@ -1174,39 +1177,41 @@ await LoopbackServer.CreateServerAsync(async (server, url) =>
11741177
[Fact]
11751178
public async Task GetResponseAsync_GetResponseStream_ExpectNotNull()
11761179
{
1177-
await LoopbackServer.CreateServerAsync(async (server, url) =>
1180+
await LoopbackServer.CreateClientAndServerAsync(async uri =>
11781181
{
1179-
HttpWebRequest request = WebRequest.CreateHttp(url);
1180-
Task<WebResponse> getResponse = request.GetResponseAsync();
1181-
await server.HandleRequestAsync();
1182-
using (WebResponse response = await getResponse)
1182+
HttpWebRequest request = WebRequest.CreateHttp(uri);
1183+
using (WebResponse response = await request.GetResponseAsync())
11831184
using (Stream myStream = response.GetResponseStream())
11841185
{
11851186
Assert.NotNull(myStream);
11861187
}
1187-
});
1188+
}, server => server.HandleRequestAsync());
11881189
}
11891190

1190-
[Fact]
1191-
public async Task GetResponseAsync_GetResponseStream_ContainsHost()
1191+
[Theory]
1192+
[InlineData(true)]
1193+
[InlineData(false)]
1194+
public async Task GetResponseAsync_GetResponseStream_ContainsHost(bool useSsl)
11921195
{
1193-
await LoopbackServer.CreateServerAsync(async (server, uri) =>
1196+
var options = new LoopbackServer.Options { UseSsl = useSsl };
1197+
1198+
await LoopbackServer.CreateClientAndServerAsync(async uri =>
11941199
{
11951200
HttpWebRequest request = WebRequest.CreateHttp(uri);
1196-
request.Method = HttpMethod.Get.Method;
1197-
1198-
string host = uri.Host + ":" + uri.Port;
1199-
Task<WebResponse> getResponse = request.GetResponseAsync();
1200-
HttpRequestData requestData = await server.HandleRequestAsync(headers: new HttpHeaderData[] { new HttpHeaderData("Host", host) });
1201-
string serverReceivedHost = requestData.GetSingleHeaderValue("Host");
1202-
Assert.Equal(host, serverReceivedHost);
1203-
using (WebResponse response = await getResponse)
1201+
request.ServerCertificateValidationCallback = (a, b, c, d) => true;
1202+
using (WebResponse response = await request.GetResponseAsync())
12041203
using (Stream myStream = response.GetResponseStream())
12051204
using (var sr = new StreamReader(myStream))
12061205
{
1207-
Assert.Equal(host, response.Headers["Host"]);
1206+
Assert.Equal(uri.Host + ":" + uri.Port, response.Headers["Host"]);
12081207
}
1209-
});
1208+
}, async server =>
1209+
{
1210+
string host = server.Uri.Host + ":" + server.Uri.Port;
1211+
HttpRequestData requestData = await server.HandleRequestAsync(headers: new HttpHeaderData[] { new HttpHeaderData("Host", host) });
1212+
string serverReceivedHost = requestData.GetSingleHeaderValue("Host");
1213+
Assert.Equal(host, serverReceivedHost);
1214+
}, options);
12101215
}
12111216

12121217
[OuterLoop]
@@ -1230,45 +1235,48 @@ public void Range_Add_Success(Uri remoteServer)
12301235
Assert.Equal(request.Headers["Range"], "bytes=1-5");
12311236
}
12321237

1233-
[Fact]
1234-
public async Task GetResponseAsync_PostRequestStream_ContainsData()
1238+
[Theory]
1239+
[InlineData(true)]
1240+
[InlineData(false)]
1241+
public async Task GetResponseAsync_PostRequestStream_ContainsData(bool useSsl)
12351242
{
1236-
await LoopbackServer.CreateServerAsync(async (server, uri) =>
1243+
var options = new LoopbackServer.Options { UseSsl = useSsl };
1244+
1245+
await LoopbackServer.CreateClientAndServerAsync(async uri =>
12371246
{
12381247
HttpWebRequest request = WebRequest.CreateHttp(uri);
1248+
request.ServerCertificateValidationCallback = (a, b, c, d) => true;
12391249
request.Method = HttpMethod.Post.Method;
1240-
12411250
using (Stream requestStream = await request.GetRequestStreamAsync())
12421251
{
12431252
requestStream.Write(_requestBodyBytes, 0, _requestBodyBytes.Length);
12441253
}
12451254

1246-
Task<WebResponse> getResponse = request.GetResponseAsync();
1247-
await server.HandleRequestAsync(content: RequestBody);
1248-
using (WebResponse response = await getResponse)
1255+
using (WebResponse response = await request.GetResponseAsync())
12491256
using (Stream myStream = response.GetResponseStream())
12501257
using (var sr = new StreamReader(myStream))
12511258
{
12521259
string strContent = sr.ReadToEnd();
12531260
Assert.True(strContent.Contains(RequestBody));
12541261
}
1255-
});
1262+
}, server => server.HandleRequestAsync(content: RequestBody), options);
12561263
}
12571264

1258-
[Fact]
1259-
public async Task GetResponseAsync_UseDefaultCredentials_ExpectSuccess()
1265+
[Theory]
1266+
[InlineData(true)]
1267+
[InlineData(false)]
1268+
public async Task GetResponseAsync_UseDefaultCredentials_ExpectSuccess(bool useSsl)
12601269
{
1261-
await LoopbackServer.CreateServerAsync(async (server, uri) =>
1270+
var options = new LoopbackServer.Options { UseSsl = useSsl };
1271+
1272+
await LoopbackServer.CreateClientAndServerAsync(async uri =>
12621273
{
12631274
HttpWebRequest request = WebRequest.CreateHttp(uri);
1275+
request.ServerCertificateValidationCallback = (a, b, c, d) => true;
12641276
request.UseDefaultCredentials = true;
12651277

1266-
Task<WebResponse> getResponse = request.GetResponseAsync();
1267-
await server.HandleRequestAsync(content: RequestBody);
1268-
using (WebResponse response = await getResponse)
1269-
{
1270-
}
1271-
});
1278+
using WebResponse response = await request.GetResponseAsync();
1279+
}, server => server.HandleRequestAsync(), options);
12721280
}
12731281

12741282
[OuterLoop] // fails on networks with DNS servers that provide a dummy page for invalid addresses
@@ -1293,38 +1301,40 @@ await LoopbackServer.CreateClientAndServerAsync(async uri =>
12931301
$"HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n"));
12941302
}
12951303

1296-
[Fact]
1297-
public async Task HaveResponse_GetResponseAsync_ExpectTrue()
1304+
[Theory]
1305+
[InlineData(true)]
1306+
[InlineData(false)]
1307+
public async Task HaveResponse_GetResponseAsync_ExpectTrue(bool useSsl)
12981308
{
1299-
await LoopbackServer.CreateServerAsync(async (server, uri) =>
1309+
var options = new LoopbackServer.Options { UseSsl = useSsl };
1310+
1311+
await LoopbackServer.CreateClientAndServerAsync(async uri =>
13001312
{
13011313
HttpWebRequest request = WebRequest.CreateHttp(uri);
1314+
request.ServerCertificateValidationCallback = (a, b, c, d) => true;
1315+
request.UseDefaultCredentials = true;
13021316

1303-
Task<WebResponse> getResponse = request.GetResponseAsync();
1304-
await server.HandleRequestAsync();
1305-
using (WebResponse response = await getResponse)
1306-
{
1307-
Assert.True(request.HaveResponse);
1308-
}
1309-
});
1317+
using WebResponse response = await request.GetResponseAsync();
1318+
Assert.True(request.HaveResponse);
1319+
}, server => server.HandleRequestAsync(), options);
13101320
}
13111321

1312-
[Fact]
1313-
public async Task Headers_GetResponseHeaders_ContainsExpectedValue()
1322+
[Theory]
1323+
[InlineData(true)]
1324+
[InlineData(false)]
1325+
public async Task Headers_GetResponseHeaders_ContainsExpectedValue(bool useSsl)
13141326
{
1315-
await LoopbackServer.CreateServerAsync(async (server, uri) =>
1327+
var options = new LoopbackServer.Options { UseSsl = useSsl };
1328+
1329+
const string HeadersPartialContent = "application/json";
1330+
await LoopbackServer.CreateClientAndServerAsync(async uri =>
13161331
{
13171332
HttpWebRequest request = WebRequest.CreateHttp(uri);
1318-
1319-
const string HeadersPartialContent = "application/json";
1320-
Task<WebResponse> getResponse = request.GetResponseAsync();
1321-
HttpRequestData requestData = await server.HandleRequestAsync(headers: new HttpHeaderData[] { new HttpHeaderData("Content-Type", HeadersPartialContent) });
1322-
using (WebResponse response = await getResponse)
1323-
{
1324-
string headersString = response.Headers.ToString();
1325-
Assert.Equal(HeadersPartialContent, response.Headers[HttpResponseHeader.ContentType]);
1326-
}
1327-
});
1333+
request.ServerCertificateValidationCallback = (a, b, c, d) => true;
1334+
using WebResponse response = await request.GetResponseAsync();
1335+
string headersString = response.Headers.ToString();
1336+
Assert.Equal(HeadersPartialContent, response.Headers[HttpResponseHeader.ContentType]);
1337+
}, server => server.HandleRequestAsync(headers: new HttpHeaderData[] { new HttpHeaderData("Content-Type", HeadersPartialContent) }), options);
13281338
}
13291339

13301340
[Theory, MemberData(nameof(EchoServers))]
@@ -1403,20 +1413,20 @@ public void RequestUri_CreateHttpThenGet_ExpectSameUri(Uri remoteServer)
14031413
Assert.Equal(remoteServer, request.RequestUri);
14041414
}
14051415

1406-
[Fact]
1407-
public async Task ResponseUri_GetResponseAsync_ExpectSameUri()
1416+
[Theory]
1417+
[InlineData(true)]
1418+
[InlineData(false)]
1419+
public async Task ResponseUri_GetResponseAsync_ExpectSameUri(bool useSsl)
14081420
{
1409-
await LoopbackServer.CreateServerAsync(async (server, uri) =>
1421+
var options = new LoopbackServer.Options { UseSsl = useSsl };
1422+
1423+
await LoopbackServer.CreateClientAndServerAsync(async uri =>
14101424
{
14111425
HttpWebRequest request = WebRequest.CreateHttp(uri);
1412-
1413-
Task<WebResponse> getResponse = request.GetResponseAsync();
1414-
await server.HandleRequestAsync();
1415-
using (WebResponse response = await getResponse)
1416-
{
1417-
Assert.Equal(uri, response.ResponseUri);
1418-
}
1419-
});
1426+
request.ServerCertificateValidationCallback = (a, b, c, d) => true;
1427+
using WebResponse response = await request.GetResponseAsync();
1428+
Assert.Equal(uri, response.ResponseUri);
1429+
}, server => server.HandleRequestAsync(), options);
14201430
}
14211431

14221432
[Theory, MemberData(nameof(EchoServers))]
@@ -1426,62 +1436,65 @@ public void SupportsCookieContainer_GetDefault_ExpectTrue(Uri remoteServer)
14261436
Assert.True(request.SupportsCookieContainer);
14271437
}
14281438

1429-
[Fact]
1430-
public async Task SimpleScenario_UseGETVerb_Success()
1439+
[Theory]
1440+
[InlineData(true)]
1441+
[InlineData(false)]
1442+
public async Task SimpleScenario_UseGETVerb_Success(bool useSsl)
14311443
{
1432-
await LoopbackServer.CreateServerAsync(async (server, uri) =>
1444+
var options = new LoopbackServer.Options { UseSsl = useSsl };
1445+
1446+
await LoopbackServer.CreateClientAndServerAsync(async uri =>
14331447
{
14341448
HttpWebRequest request = WebRequest.CreateHttp(uri);
1435-
1436-
Task<WebResponse> getResponse = request.GetResponseAsync();
1437-
await server.HandleRequestAsync();
1438-
using (HttpWebResponse response = (HttpWebResponse)await getResponse)
1439-
{
1440-
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
1441-
}
1442-
});
1449+
request.ServerCertificateValidationCallback = (a, b, c, d) => true;
1450+
using HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync();
1451+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
1452+
}, server => server.HandleRequestAsync(), options);
14431453
}
14441454

1445-
[Fact]
1446-
public async Task SimpleScenario_UsePOSTVerb_Success()
1455+
[Theory]
1456+
[InlineData(true)]
1457+
[InlineData(false)]
1458+
public async Task SimpleScenario_UsePOSTVerb_Success(bool useSsl)
14471459
{
1448-
await LoopbackServer.CreateServerAsync(async (server, uri) =>
1460+
var options = new LoopbackServer.Options { UseSsl = useSsl };
1461+
1462+
await LoopbackServer.CreateClientAndServerAsync(async uri =>
14491463
{
14501464
HttpWebRequest request = WebRequest.CreateHttp(uri);
1465+
request.ServerCertificateValidationCallback = (a, b, c, d) => true;
14511466
request.Method = HttpMethod.Post.Method;
1452-
14531467
using (Stream requestStream = await request.GetRequestStreamAsync())
14541468
{
14551469
requestStream.Write(_requestBodyBytes, 0, _requestBodyBytes.Length);
14561470
}
14571471

1458-
Task<WebResponse> getResponse = request.GetResponseAsync();
1459-
await server.HandleRequestAsync();
1460-
using (HttpWebResponse response = (HttpWebResponse)await getResponse)
1461-
{
1462-
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
1463-
}
1464-
});
1472+
using HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync();
1473+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
1474+
}, server => server.HandleRequestAsync(), options);
14651475
}
14661476

1467-
[Fact]
1468-
public async Task ContentType_AddHeaderWithNoContent_SendRequest_HeaderGetsSent()
1477+
[Theory]
1478+
[InlineData(true)]
1479+
[InlineData(false)]
1480+
public async Task ContentType_AddHeaderWithNoContent_SendRequest_HeaderGetsSent(bool useSsl)
14691481
{
14701482
const string ContentType = "text/plain; charset=utf-8";
1483+
var options = new LoopbackServer.Options { UseSsl = useSsl };
14711484

1472-
await LoopbackServer.CreateServerAsync(async (server, uri) =>
1485+
await LoopbackServer.CreateClientAndServerAsync(async uri =>
14731486
{
14741487
HttpWebRequest request = WebRequest.CreateHttp(uri);
1488+
request.ServerCertificateValidationCallback = (a, b, c, d) => true;
14751489
request.ContentType = ContentType;
14761490

1477-
Task<WebResponse> getResponse = request.GetResponseAsync();
1491+
using HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync();
1492+
Assert.Equal(ContentType, response.Headers[HttpResponseHeader.ContentType]);
1493+
}, async server =>
1494+
{
14781495
HttpRequestData requestData = await server.HandleRequestAsync(headers: new HttpHeaderData[] { new HttpHeaderData("Content-Type", ContentType) });
14791496
Assert.Equal(ContentType, requestData.GetSingleHeaderValue("Content-Type"));
1480-
using (HttpWebResponse response = (HttpWebResponse)await getResponse)
1481-
{
1482-
Assert.Equal(ContentType, response.Headers[HttpResponseHeader.ContentType]);
1483-
}
1484-
});
1497+
}, options);
14851498
}
14861499

14871500
[Theory, MemberData(nameof(EchoServers))]

src/System.Net.Requests/tests/System.Net.Requests.Tests.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<ProjectGuid>{E520B5FD-C6FF-46CF-8079-6C8098013EA3}</ProjectGuid>
44
<StringResourcesPath>../src/Resources/Strings.resx</StringResourcesPath>
@@ -43,4 +43,7 @@
4343
<Compile Include="HttpRequestCachePolicyTest.cs" />
4444
<Compile Include="HttpWebResponseHeaderTest.cs" />
4545
</ItemGroup>
46+
<ItemGroup>
47+
<SupplementalTestData Include="$(NuGetPackageRoot)system.net.testdata\1.0.2\content\**\*.*" DestinationDir="TestData" />
48+
</ItemGroup>
4649
</Project>

0 commit comments

Comments
 (0)