From a0a8ce681afda6b662e450ac9532b0d4c125520c Mon Sep 17 00:00:00 2001 From: Pavel Savara Date: Thu, 27 Jul 2023 11:50:38 +0200 Subject: [PATCH] [browser][http] test and compatibility improvements (#89504) --- .../tests/System/Net/Http/HttpClientHandlerTest.cs | 22 +++++++++++++--------- .../tests/System/Net/Http/ResponseStreamTest.cs | 7 +++++-- .../Http/BrowserHttpHandler/BrowserHttpHandler.cs | 4 ++++ .../HttpClientHandlerTest.Headers.cs | 4 ++-- 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.cs b/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.cs index 369a6d1..fb8a2d8 100644 --- a/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.cs +++ b/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.cs @@ -351,7 +351,7 @@ namespace System.Net.Http.Functional.Tests [InlineData("nocolon")] [InlineData("no colon")] [InlineData("Content-Length ")] - [ActiveIssue("https://github.com/dotnet/runtime/issues/54160", TestPlatforms.Browser)] + [SkipOnPlatform(TestPlatforms.Browser, "Browser is relaxed about validating HTTP headers")] public async Task GetAsync_InvalidHeaderNameValue_ThrowsHttpRequestException(string invalidHeader) { if (UseVersion == HttpVersion30) @@ -373,7 +373,7 @@ namespace System.Net.Http.Functional.Tests [InlineData(true, false)] [InlineData(false, true)] [InlineData(true, true)] - [ActiveIssue("https://github.com/dotnet/runtime/issues/54160", TestPlatforms.Browser)] + [ActiveIssue("https://github.com/dotnet/runtime/issues/86317", typeof(PlatformDetection), nameof(PlatformDetection.IsNodeJS))] public async Task GetAsync_IncompleteData_ThrowsHttpRequestException(bool failDuringHeaders, bool getString) { if (IsWinHttpHandler) @@ -386,6 +386,11 @@ namespace System.Net.Http.Functional.Tests return; } + if (PlatformDetection.IsBrowser && failDuringHeaders) + { + return; // chrome browser doesn't validate this + } + await LoopbackServer.CreateClientAndServerAsync(async uri => { using (HttpClient client = CreateHttpClient()) @@ -763,7 +768,7 @@ namespace System.Net.Http.Functional.Tests } [Fact] - [ActiveIssue("https://github.com/dotnet/runtime/issues/54160", TestPlatforms.Browser)] + [SkipOnPlatform(TestPlatforms.Browser, "TypeError: Failed to fetch on a Browser")] public async Task GetAsync_NonTraditionalChunkSizes_Accepted() { if (LoopbackServerFactory.Version >= HttpVersion20.Value) @@ -816,7 +821,7 @@ namespace System.Net.Http.Functional.Tests [InlineData("xyz")] // non-hex [InlineData("7gibberish")] // valid size then gibberish [InlineData("7\v\f")] // unacceptable whitespace - [ActiveIssue("https://github.com/dotnet/runtime/issues/54160", TestPlatforms.Browser)] + [ActiveIssue("https://github.com/dotnet/runtime/issues/86317", typeof(PlatformDetection), nameof(PlatformDetection.IsNodeJS))] public async Task GetAsync_InvalidChunkSize_ThrowsHttpRequestException(string chunkSize) { if (UseVersion != HttpVersion.Version11) @@ -849,7 +854,7 @@ namespace System.Net.Http.Functional.Tests } [Fact] - [ActiveIssue("https://github.com/dotnet/runtime/issues/54160", TestPlatforms.Browser)] + [ActiveIssue("https://github.com/dotnet/runtime/issues/86317", typeof(PlatformDetection), nameof(PlatformDetection.IsNodeJS))] public async Task GetAsync_InvalidChunkTerminator_ThrowsHttpRequestException() { if (UseVersion != HttpVersion.Version11) @@ -878,7 +883,7 @@ namespace System.Net.Http.Functional.Tests } [Fact] - [ActiveIssue("https://github.com/dotnet/runtime/issues/54160", TestPlatforms.Browser)] + [ActiveIssue("https://github.com/dotnet/runtime/issues/86317", typeof(PlatformDetection), nameof(PlatformDetection.IsNodeJS))] public async Task GetAsync_InfiniteChunkSize_ThrowsHttpRequestException() { if (UseVersion != HttpVersion.Version11) @@ -1468,7 +1473,7 @@ namespace System.Net.Http.Functional.Tests [Theory] [InlineData(99)] [InlineData(1000)] - [ActiveIssue("https://github.com/dotnet/runtime/issues/54160", TestPlatforms.Browser)] + [SkipOnPlatform(TestPlatforms.Browser, "Browser is relaxed about validating HTTP headers")] public async Task GetAsync_StatusCodeOutOfRange_ExpectedException(int statusCode) { if (UseVersion == HttpVersion30) @@ -1743,7 +1748,7 @@ namespace System.Net.Http.Functional.Tests } [Fact] - [ActiveIssue("https://github.com/dotnet/runtime/issues/54160", TestPlatforms.Browser)] + [SkipOnPlatform(TestPlatforms.Browser, "ExpectContinue not supported on Browser")] public async Task SendAsync_Expect100Continue_RequestBodyFails_ThrowsContentException() { if (IsWinHttpHandler) @@ -1885,7 +1890,6 @@ namespace System.Net.Http.Functional.Tests [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) diff --git a/src/libraries/Common/tests/System/Net/Http/ResponseStreamTest.cs b/src/libraries/Common/tests/System/Net/Http/ResponseStreamTest.cs index 78aede8..992851b 100644 --- a/src/libraries/Common/tests/System/Net/Http/ResponseStreamTest.cs +++ b/src/libraries/Common/tests/System/Net/Http/ResponseStreamTest.cs @@ -268,14 +268,17 @@ namespace System.Net.Http.Functional.Tests [InlineData(TransferType.ContentLength, TransferError.ContentLengthTooLarge)] [InlineData(TransferType.Chunked, TransferError.MissingChunkTerminator)] [InlineData(TransferType.Chunked, TransferError.ChunkSizeTooLarge)] - [ActiveIssue("https://github.com/dotnet/runtime/issues/54160", TestPlatforms.Browser)] public async Task ReadAsStreamAsync_InvalidServerResponse_ThrowsIOException( TransferType transferType, TransferError transferError) { await StartTransferTypeAndErrorServer(transferType, transferError, async uri => { - if (IsWinHttpHandler) + if (PlatformDetection.IsBrowser) // TypeError: Failed to fetch + { + await Assert.ThrowsAsync(() => ReadAsStreamHelper(uri)); + } + else if (IsWinHttpHandler) { await Assert.ThrowsAsync(() => ReadAsStreamHelper(uri)); } diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/BrowserHttpHandler/BrowserHttpHandler.cs b/src/libraries/System.Net.Http/src/System/Net/Http/BrowserHttpHandler/BrowserHttpHandler.cs index ed8abc2..73cb9f7 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/BrowserHttpHandler/BrowserHttpHandler.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/BrowserHttpHandler/BrowserHttpHandler.cs @@ -235,6 +235,10 @@ namespace System.Net.Http JSObject fetchResponse = await BrowserHttpInterop.CancelationHelper(promise, cancellationToken, abortController, null).ConfigureAwait(true); return new WasmFetchResponse(fetchResponse, abortRegistration.Value); } + catch (JSException jse) + { + throw new HttpRequestException(jse.Message, jse); + } catch (Exception) { // this would also trigger abort diff --git a/src/libraries/System.Net.Http/tests/FunctionalTests/HttpClientHandlerTest.Headers.cs b/src/libraries/System.Net.Http/tests/FunctionalTests/HttpClientHandlerTest.Headers.cs index 6bfc628..949fdae 100644 --- a/src/libraries/System.Net.Http/tests/FunctionalTests/HttpClientHandlerTest.Headers.cs +++ b/src/libraries/System.Net.Http/tests/FunctionalTests/HttpClientHandlerTest.Headers.cs @@ -138,7 +138,7 @@ namespace System.Net.Http.Functional.Tests [Theory] [InlineData("\u05D1\u05F1")] [InlineData("jp\u30A5")] - [ActiveIssue("https://github.com/dotnet/runtime/issues/54160", TestPlatforms.Browser)] + [SkipOnPlatform(TestPlatforms.Browser, "Browser is relaxed about validating HTTP headers")] public async Task SendAsync_InvalidCharactersInHeader_Throw(string value) { await LoopbackServerFactory.CreateClientAndServerAsync(async uri => @@ -412,7 +412,7 @@ namespace System.Net.Http.Functional.Tests } [Fact] - [ActiveIssue("https://github.com/dotnet/runtime/issues/54160", TestPlatforms.Browser)] + [SkipOnPlatform(TestPlatforms.Browser, "Browser is relaxed about validating HTTP headers")] public async Task SendAsync_WithZeroLengthHeaderName_Throws() { await LoopbackServerFactory.CreateClientAndServerAsync( -- 2.7.4