Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelsavara committed Jun 14, 2021
1 parent 1c5f82d commit 6874a12
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ await LoopbackServerFactory.CreateServerAsync(async (server, url) =>
[InlineData("nocolon")]
[InlineData("no colon")]
[InlineData("Content-Length ")]
[ActiveIssue("https://github.com/dotnet/runtime/issues/54160", TestPlatforms.Browser)]
public async Task GetAsync_InvalidHeaderNameValue_ThrowsHttpRequestException(string invalidHeader)
{
if (UseVersion == HttpVersion30)
Expand All @@ -353,14 +354,15 @@ await LoopbackServer.CreateClientAndServerAsync(async uri =>
{
await Assert.ThrowsAsync<HttpRequestException>(() => client.GetStringAsync(uri));
}
}, server => server.AcceptConnectionSendCustomResponseAndCloseAsync($"HTTP/1.1 200 OK\r\n{invalidHeader}\r\nContent-Length: 11\r\n\r\nhello world"));
}, server => server.AcceptConnectionSendCustomResponseAndCloseAsync($"HTTP/1.1 200 OK\r\n{invalidHeader}\r\n{LoopbackServer.CorsHeaders}Content-Length: 11\r\n\r\nhello world"));
}

[Theory]
[InlineData(false, false)]
[InlineData(true, false)]
[InlineData(false, true)]
[InlineData(true, true)]
[ActiveIssue("https://github.com/dotnet/runtime/issues/54160", TestPlatforms.Browser)]
public async Task GetAsync_IncompleteData_ThrowsHttpRequestException(bool failDuringHeaders, bool getString)
{
if (IsWinHttpHandler)
Expand All @@ -384,11 +386,12 @@ await LoopbackServer.CreateClientAndServerAsync(async uri =>
}
}, server =>
failDuringHeaders ?
server.AcceptConnectionSendCustomResponseAndCloseAsync("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n") :
server.AcceptConnectionSendCustomResponseAndCloseAsync("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\nhe"));
server.AcceptConnectionSendCustomResponseAndCloseAsync($"HTTP/1.1 200 OK\r\n{LoopbackServer.CorsHeaders}Content-Length: 5\r\n") :
server.AcceptConnectionSendCustomResponseAndCloseAsync($"HTTP/1.1 200 OK\r\n{LoopbackServer.CorsHeaders}Content-Length: 5\r\n\r\nhe"));
}

[Fact]
[ActiveIssue("https://github.com/dotnet/runtime/issues/42852", TestPlatforms.Browser)]
public async Task PostAsync_ManyDifferentRequestHeaders_SentCorrectly()
{
if (IsWinHttpHandler && UseVersion >= HttpVersion20.Value)
Expand Down Expand Up @@ -577,7 +580,10 @@ await LoopbackServer.CreateClientAndServerAsync(async uri =>
{
Assert.Equal("1.1", resp.Version.ToString());
Assert.Equal(HttpStatusCode.OK, resp.StatusCode);
Assert.Contains("*", resp.Headers.GetValues("Access-Control-Allow-Origin"));
if(PlatformDetection.IsNotBrowser)
{
Assert.Contains("*", resp.Headers.GetValues("Access-Control-Allow-Origin"));
}
Assert.Contains("text/example;charset=utf-8", resp.Headers.GetValues("Accept-Patch"));
Assert.Contains("bytes", resp.Headers.AcceptRanges);
Assert.Equal(TimeSpan.FromSeconds(12), resp.Headers.Age.GetValueOrDefault());
Expand All @@ -593,7 +599,10 @@ await LoopbackServer.CreateClientAndServerAsync(async uri =>
Assert.Contains("gzip", resp.Content.Headers.ContentEncoding);
Assert.Contains("da", resp.Content.Headers.ContentLanguage);
Assert.Equal(new Uri("/index.htm", UriKind.Relative), resp.Content.Headers.ContentLocation);
Assert.Equal(Convert.FromBase64String("Q2hlY2sgSW50ZWdyaXR5IQ=="), resp.Content.Headers.ContentMD5);
if(PlatformDetection.IsNotBrowser)
{
Assert.Equal(Convert.FromBase64String("Q2hlY2sgSW50ZWdyaXR5IQ=="), resp.Content.Headers.ContentMD5);
}
Assert.Equal("bytes", resp.Content.Headers.ContentRange.Unit);
Assert.Equal(21010, resp.Content.Headers.ContentRange.From.GetValueOrDefault());
Assert.Equal(47021, resp.Content.Headers.ContentRange.To.GetValueOrDefault());
Expand All @@ -612,7 +621,11 @@ await LoopbackServer.CreateClientAndServerAsync(async uri =>
Assert.Contains("max-age=2592000; pin-sha256=\"E9CZ9INDbd+2eRQozYqqbQ2yXLVKB9+xcprMF+44U1g=\"", resp.Headers.GetValues("Public-Key-Pins"));
Assert.Equal(TimeSpan.FromSeconds(120), resp.Headers.RetryAfter.Delta.GetValueOrDefault());
Assert.Contains(new ProductInfoHeaderValue("Apache", "2.4.1"), resp.Headers.Server);
Assert.Contains("UserID=JohnDoe; Max-Age=3600; Version=1", resp.Headers.GetValues("Set-Cookie"));

if(PlatformDetection.IsNotBrowser)
{
Assert.Contains("UserID=JohnDoe; Max-Age=3600; Version=1", resp.Headers.GetValues("Set-Cookie"));
}
Assert.Contains("max-age=16070400; includeSubDomains", resp.Headers.GetValues("Strict-Transport-Security"));
Assert.Contains("Max-Forwards", resp.Headers.Trailer);
Assert.Contains("?", resp.Headers.GetValues("Tk"));
Expand All @@ -639,6 +652,7 @@ await LoopbackServer.CreateClientAndServerAsync(async uri =>
}, server => server.AcceptConnectionSendCustomResponseAndCloseAsync(
$"HTTP/1.1 200 OK{newline}" +
$"Access-Control-Allow-Origin:{fold} *{newline}" +
$"Access-Control-Expose-Headers:{fold} *{newline}" +
$"Accept-Patch:{fold} text/example;charset=utf-8{newline}" +
$"Accept-Ranges:{fold} bytes{newline}" +
$"Age: {fold}12{newline}" +
Expand Down Expand Up @@ -693,6 +707,7 @@ await LoopbackServer.CreateClientAndServerAsync(async uri =>
dribble ? new LoopbackServer.Options { StreamWrapper = s => new DribbleStream(s) } : null);
}

[ActiveIssue("https://github.com/dotnet/runtime/issues/54160", TestPlatforms.Browser)]
[Fact]
public async Task GetAsync_NonTraditionalChunkSizes_Accepted()
{
Expand All @@ -712,6 +727,7 @@ await TestHelper.WhenAllCompletedOrAnyFailed(
server.AcceptConnectionSendCustomResponseAndCloseAsync(
"HTTP/1.1 200 OK\r\n" +
"Connection: close\r\n" +
LoopbackServer.CorsHeaders +
"Transfer-Encoding: chunked\r\n" +
"\r\n" +
"4 \r\n" + // whitespace after size
Expand Down Expand Up @@ -757,6 +773,7 @@ await LoopbackServer.CreateServerAsync(async (server, url) =>
using (HttpClient client = CreateHttpClient())
{
string partialResponse = "HTTP/1.1 200 OK\r\n" +
LoopbackServer.CorsHeaders +
"Transfer-Encoding: chunked\r\n" +
"\r\n" +
$"{chunkSize}\r\n";
Expand Down Expand Up @@ -792,6 +809,7 @@ await LoopbackServer.CreateClientAndServerAsync(async url =>
}, server => server.AcceptConnectionSendCustomResponseAndCloseAsync(
"HTTP/1.1 200 OK\r\n" +
"Connection: close\r\n" +
LoopbackServer.CorsHeaders +
"Transfer-Encoding: chunked\r\n" +
"\r\n" +
"5\r\n" +
Expand Down Expand Up @@ -820,7 +838,7 @@ await LoopbackServer.CreateServerAsync(async (server, url) =>
var tcs = new TaskCompletionSource<bool>();
Task serverTask = server.AcceptConnectionAsync(async connection =>
{
await connection.ReadRequestHeaderAndSendCustomResponseAsync("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n");
await connection.ReadRequestHeaderAndSendCustomResponseAsync("HTTP/1.1 200 OK\r\n" + LoopbackServer.CorsHeaders + "Transfer-Encoding: chunked\r\n\r\n");
try
{
while (!cts.IsCancellationRequested) // infinite to make sure implementation doesn't OOM
Expand All @@ -840,6 +858,7 @@ await LoopbackServer.CreateServerAsync(async (server, url) =>
}

[Fact]
[SkipOnPlatform(TestPlatforms.Browser, "CORS is required on Browser")]
public async Task SendAsync_TransferEncodingSetButNoRequestContent_Throws()
{
var req = new HttpRequestMessage(HttpMethod.Post, "http://bing.com") { Version = UseVersion };
Expand Down Expand Up @@ -871,6 +890,7 @@ await server.AcceptConnectionAsync(async connection =>
await connection.ReadRequestHeaderAndSendCustomResponseAsync(
"HTTP/1.1 200 OK\r\n" +
$"Date: {DateTimeOffset.UtcNow:R}\r\n" +
LoopbackServer.CorsHeaders +
"Content-Length: 16000\r\n" +
"\r\n" +
"less than 16000 bytes");
Expand All @@ -894,6 +914,7 @@ await connection.ReadRequestHeaderAndSendCustomResponseAsync(
[InlineData(true)]
[InlineData(false)]
[InlineData(null)]
[ActiveIssue("https://github.com/dotnet/runtime/issues/54159", TestPlatforms.Browser)]
public async Task ReadAsStreamAsync_HandlerProducesWellBehavedResponseStream(bool? chunked)
{
if (IsWinHttpHandler && UseVersion >= HttpVersion20.Value)
Expand Down Expand Up @@ -1059,6 +1080,7 @@ await server.AcceptConnectionAsync(async connection =>
}

[Fact]
[ActiveIssue("https://github.com/dotnet/runtime/issues/54159", TestPlatforms.Browser)]
public async Task ReadAsStreamAsync_EmptyResponseBody_HandlerProducesWellBehavedResponseStream()
{
if (IsWinHttpHandler && UseVersion >= HttpVersion20.Value)
Expand Down Expand Up @@ -1223,6 +1245,7 @@ await LoopbackServerFactory.CreateServerAsync(async (server3, url3) =>
[Theory]
[InlineData(99)]
[InlineData(1000)]
[ActiveIssue("https://github.com/dotnet/runtime/issues/54160", TestPlatforms.Browser)]
public async Task GetAsync_StatusCodeOutOfRange_ExpectedException(int statusCode)
{
if (UseVersion == HttpVersion30)
Expand All @@ -1239,6 +1262,7 @@ await LoopbackServer.CreateServerAsync(async (server, url) =>
await server.AcceptConnectionSendCustomResponseAndCloseAsync(
$"HTTP/1.1 {statusCode}\r\n" +
$"Date: {DateTimeOffset.UtcNow:R}\r\n" +
LoopbackServer.CorsHeaders +
"Connection: close\r\n" +
"\r\n");

Expand Down Expand Up @@ -1267,6 +1291,7 @@ public async Task GetAsync_UnicodeHostName_SuccessStatusCodeInResponse()
#region Post Methods Tests

[Fact]
[ActiveIssue("https://github.com/dotnet/runtime/issues/53876", TestPlatforms.Browser)]
public async Task GetAsync_ExpectContinueTrue_NoContent_StillSendsHeader()
{
if (IsWinHttpHandler && UseVersion >= HttpVersion20.Value)
Expand Down Expand Up @@ -1493,6 +1518,7 @@ await server.AcceptConnectionAsync(async connection =>
}

[Fact]
[ActiveIssue("https://github.com/dotnet/runtime/issues/54160", TestPlatforms.Browser)]
public async Task SendAsync_Expect100Continue_RequestBodyFails_ThrowsContentException()
{
if (IsWinHttpHandler)
Expand Down Expand Up @@ -1589,6 +1615,7 @@ await server.AcceptConnectionAsync(async connection =>
}

[Fact]
[SkipOnPlatform(TestPlatforms.Browser, "Switching protocol is not supported on Browser")]
public async Task SendAsync_101SwitchingProtocolsResponse_Success()
{
// WinHttpHandler and CurlHandler will hang, waiting for additional response.
Expand Down Expand Up @@ -1629,6 +1656,7 @@ await connection.ReadRequestHeaderAndSendCustomResponseAsync(
[Theory]
[InlineData(false)]
[InlineData(true)]
[ActiveIssue("https://github.com/dotnet/runtime/issues/54160", TestPlatforms.Browser)]
public async Task PostAsync_ThrowFromContentCopy_RequestFails(bool syncFailure)
{
if (UseVersion == HttpVersion30)
Expand Down Expand Up @@ -1664,6 +1692,7 @@ await LoopbackServer.CreateServerAsync(async (server, uri) =>
[Theory]
[InlineData(HttpStatusCode.MethodNotAllowed, "Custom description")]
[InlineData(HttpStatusCode.MethodNotAllowed, "")]
[ActiveIssue("https://github.com/dotnet/runtime/issues/54163", TestPlatforms.Browser)]
public async Task GetAsync_CallMethod_ExpectedStatusLine(HttpStatusCode statusCode, string reasonPhrase)
{
if (LoopbackServerFactory.Version >= HttpVersion20.Value)
Expand All @@ -1681,13 +1710,14 @@ await LoopbackServer.CreateClientAndServerAsync(async uri =>
Assert.Equal(reasonPhrase, response.ReasonPhrase);
}
}, server => server.AcceptConnectionSendCustomResponseAndCloseAsync(
$"HTTP/1.1 {(int)statusCode} {reasonPhrase}\r\nContent-Length: 0\r\n\r\n"));
$"HTTP/1.1 {(int)statusCode} {reasonPhrase}\r\n{LoopbackServer.CorsHeaders}Content-Length: 0\r\n\r\n"));
}

#endregion

#region Version tests

[SkipOnPlatform(TestPlatforms.Browser, "Version is not supported on Browser")]
[Fact]
public async Task SendAsync_RequestVersion10_ServerReceivesVersion10Request()
{
Expand All @@ -1701,13 +1731,15 @@ public async Task SendAsync_RequestVersion10_ServerReceivesVersion10Request()
Assert.Equal(new Version(1, 0), receivedRequestVersion);
}

[SkipOnPlatform(TestPlatforms.Browser, "Version is not supported on Browser")]
[Fact]
public async Task SendAsync_RequestVersion11_ServerReceivesVersion11Request()
{
Version receivedRequestVersion = await SendRequestAndGetRequestVersionAsync(new Version(1, 1));
Assert.Equal(new Version(1, 1), receivedRequestVersion);
}

[SkipOnPlatform(TestPlatforms.Browser, "Version is not supported on Browser")]
[Fact]
public async Task SendAsync_RequestVersionNotSpecified_ServerReceivesVersion11Request()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public async Task<List<string>> AcceptConnectionSendResponseAndCloseAsync(HttpSt
// We'll close the connection after reading the request header and sending the response.
await AcceptConnectionAsync(async connection =>
{
lines = await connection.ReadRequestHeaderAndSendResponseAsync(statusCode, additionalHeaders + LoopbackServer.CorsHeaders + "Connection: close\r\n", content).ConfigureAwait(false);
lines = await connection.ReadRequestHeaderAndSendResponseAsync(statusCode, additionalHeaders + "Connection: close\r\n", content).ConfigureAwait(false);
}).ConfigureAwait(false);

return lines;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

namespace System.Net.Http.Functional.Tests
{
[ConditionalClass(typeof(PlatformDetection), nameof(PlatformDetection.IsNotBrowser))]
public class HttpRequestMessageTest : HttpClientHandlerTestBase
{
private readonly Version _expectedRequestMessageVersion = HttpVersion.Version11;
Expand Down Expand Up @@ -229,6 +228,10 @@ public void ToString_DefaultAndNonDefaultInstance_DumpAllFields()
[InlineData("HEAD")]
public async Task HttpRequest_BodylessMethod_NoContentLength(string method)
{
if(PlatformDetection.IsBrowser && (method=="DELETE" || method=="OPTIONS")){
// [ActiveIssue("https://github.com/dotnet/runtime/issues/42852", TestPlatforms.Browser)] //TODO pre-flight OPTIONS
return;
}
using (HttpClient client = CreateHttpClient())
{
await LoopbackServer.CreateServerAsync(async (server, uri) =>
Expand Down

0 comments on commit 6874a12

Please sign in to comment.