From 1f277501288940062d9b3682d1286e145679db54 Mon Sep 17 00:00:00 2001 From: James Sinclair Date: Wed, 4 Jul 2018 22:55:50 +0100 Subject: [PATCH 1/2] fix for https://github.com/aspnet/AspNetWebStack/issues/173 --- .../Batch/DefaultHttpBatchHandler.cs | 2 +- .../Batch/DefaultHttpBatchHandlerTest.cs | 32 +++++++++++++++++-- .../BatchingTest.cs | 3 +- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/System.Web.Http/Batch/DefaultHttpBatchHandler.cs b/src/System.Web.Http/Batch/DefaultHttpBatchHandler.cs index ca5412816..e40f1409b 100644 --- a/src/System.Web.Http/Batch/DefaultHttpBatchHandler.cs +++ b/src/System.Web.Http/Batch/DefaultHttpBatchHandler.cs @@ -187,7 +187,7 @@ public virtual async Task> ParseBatchRequestsAsync(Htt foreach (HttpContent httpContent in streamProvider.Contents) { cancellationToken.ThrowIfCancellationRequested(); - HttpRequestMessage innerRequest = await httpContent.ReadAsHttpRequestMessageAsync(); + HttpRequestMessage innerRequest = await httpContent.ReadAsHttpRequestMessageAsync(request.RequestUri.Scheme); innerRequest.CopyBatchRequestProperties(request); requests.Add(innerRequest); } diff --git a/test/System.Web.Http.Test/Batch/DefaultHttpBatchHandlerTest.cs b/test/System.Web.Http.Test/Batch/DefaultHttpBatchHandlerTest.cs index 5b3033684..06bba9863 100644 --- a/test/System.Web.Http.Test/Batch/DefaultHttpBatchHandlerTest.cs +++ b/test/System.Web.Http.Test/Batch/DefaultHttpBatchHandlerTest.cs @@ -102,7 +102,8 @@ public async Task ProcessBatchAsync_CallsRegisterForDispose() { new HttpMessageContent(new HttpRequestMessage(HttpMethod.Get, "http://example.com/")), new HttpMessageContent(new HttpRequestMessage(HttpMethod.Post, "http://example.org/")) - } + }, + RequestUri = new Uri("http://example.com/") }; var response = await batchHandler.ProcessBatchAsync(batchRequest, CancellationToken.None); @@ -269,7 +270,8 @@ public async Task ParseBatchRequestsAsync_Returns_RequestsFromMultipartContent() { new HttpMessageContent(new HttpRequestMessage(HttpMethod.Get, "http://example.com/")), new HttpMessageContent(new HttpRequestMessage(HttpMethod.Post, "http://example.com/values")) - } + }, + RequestUri = new Uri("http://example.com/") }; IList requests = await batchHandler.ParseBatchRequestsAsync(request, CancellationToken.None); @@ -281,6 +283,29 @@ public async Task ParseBatchRequestsAsync_Returns_RequestsFromMultipartContent() Assert.Equal("http://example.com/values", requests[1].RequestUri.AbsoluteUri); } + [Fact] + public async Task ParseBatchRequestsAsync_Returns_RequestsFromMultipartContent_WithUriSchemeSet_FromRequest() + { + DefaultHttpBatchHandler batchHandler = new DefaultHttpBatchHandler(new HttpServer()); + HttpRequestMessage request = new HttpRequestMessage + { + Content = new MultipartContent("mixed") + { + new HttpMessageContent(new HttpRequestMessage(HttpMethod.Get, "http://example.com/")), + new HttpMessageContent(new HttpRequestMessage(HttpMethod.Post, "https://example.com/values")) + }, + RequestUri = new Uri("https://example.com/") + }; + + IList requests = await batchHandler.ParseBatchRequestsAsync(request, CancellationToken.None); + + Assert.Equal(2, requests.Count); + Assert.Equal(HttpMethod.Get, requests[0].Method); + Assert.Equal("https://example.com/", requests[0].RequestUri.AbsoluteUri); + Assert.Equal(HttpMethod.Post, requests[1].Method); + Assert.Equal("https://example.com/values", requests[1].RequestUri.AbsoluteUri); + } + [Fact] public async Task ParseBatchRequestsAsync_CopiesPropertiesFromRequest_WithoutExcludedProperties() { @@ -291,7 +316,8 @@ public async Task ParseBatchRequestsAsync_CopiesPropertiesFromRequest_WithoutExc { new HttpMessageContent(new HttpRequestMessage(HttpMethod.Get, "http://example.com/")), new HttpMessageContent(new HttpRequestMessage(HttpMethod.Post, "http://example.com/values")) - } + }, + RequestUri = new Uri("http://example.com/") }; request.Properties.Add("foo", "bar"); request.SetRouteData(new HttpRouteData(new HttpRoute())); diff --git a/test/System.Web.Http.WebHost.Test/BatchingTest.cs b/test/System.Web.Http.WebHost.Test/BatchingTest.cs index 7ccb2ce26..57963809b 100644 --- a/test/System.Web.Http.WebHost.Test/BatchingTest.cs +++ b/test/System.Web.Http.WebHost.Test/BatchingTest.cs @@ -43,7 +43,8 @@ public async Task WebHost_Batching_WithSpecialCharactersInUrl() Content = new MultipartContent("mixed") { new HttpMessageContent(new HttpRequestMessage(HttpMethod.Post, "http://contoso.com/values/ space")) - } + }, + RequestUri = new Uri("http://contoso.com/") }; // Arrange From e940ddf9bd407644fe13e6330911d0b601ae4a98 Mon Sep 17 00:00:00 2001 From: JamesSinclairBiomni Date: Wed, 25 Jul 2018 16:24:07 +0100 Subject: [PATCH 2/2] Changes following review --- src/System.Web.Http/Batch/DefaultHttpBatchHandler.cs | 2 +- .../Batch/DefaultHttpBatchHandlerTest.cs | 9 +++------ test/System.Web.Http.WebHost.Test/BatchingTest.cs | 3 +-- 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/System.Web.Http/Batch/DefaultHttpBatchHandler.cs b/src/System.Web.Http/Batch/DefaultHttpBatchHandler.cs index e40f1409b..62171a318 100644 --- a/src/System.Web.Http/Batch/DefaultHttpBatchHandler.cs +++ b/src/System.Web.Http/Batch/DefaultHttpBatchHandler.cs @@ -187,7 +187,7 @@ public virtual async Task> ParseBatchRequestsAsync(Htt foreach (HttpContent httpContent in streamProvider.Contents) { cancellationToken.ThrowIfCancellationRequested(); - HttpRequestMessage innerRequest = await httpContent.ReadAsHttpRequestMessageAsync(request.RequestUri.Scheme); + HttpRequestMessage innerRequest = request.RequestUri == null ? await httpContent.ReadAsHttpRequestMessageAsync() : await httpContent.ReadAsHttpRequestMessageAsync(request.RequestUri.Scheme); innerRequest.CopyBatchRequestProperties(request); requests.Add(innerRequest); } diff --git a/test/System.Web.Http.Test/Batch/DefaultHttpBatchHandlerTest.cs b/test/System.Web.Http.Test/Batch/DefaultHttpBatchHandlerTest.cs index 06bba9863..d6283e1e9 100644 --- a/test/System.Web.Http.Test/Batch/DefaultHttpBatchHandlerTest.cs +++ b/test/System.Web.Http.Test/Batch/DefaultHttpBatchHandlerTest.cs @@ -102,8 +102,7 @@ public async Task ProcessBatchAsync_CallsRegisterForDispose() { new HttpMessageContent(new HttpRequestMessage(HttpMethod.Get, "http://example.com/")), new HttpMessageContent(new HttpRequestMessage(HttpMethod.Post, "http://example.org/")) - }, - RequestUri = new Uri("http://example.com/") + } }; var response = await batchHandler.ProcessBatchAsync(batchRequest, CancellationToken.None); @@ -270,8 +269,7 @@ public async Task ParseBatchRequestsAsync_Returns_RequestsFromMultipartContent() { new HttpMessageContent(new HttpRequestMessage(HttpMethod.Get, "http://example.com/")), new HttpMessageContent(new HttpRequestMessage(HttpMethod.Post, "http://example.com/values")) - }, - RequestUri = new Uri("http://example.com/") + } }; IList requests = await batchHandler.ParseBatchRequestsAsync(request, CancellationToken.None); @@ -316,8 +314,7 @@ public async Task ParseBatchRequestsAsync_CopiesPropertiesFromRequest_WithoutExc { new HttpMessageContent(new HttpRequestMessage(HttpMethod.Get, "http://example.com/")), new HttpMessageContent(new HttpRequestMessage(HttpMethod.Post, "http://example.com/values")) - }, - RequestUri = new Uri("http://example.com/") + } }; request.Properties.Add("foo", "bar"); request.SetRouteData(new HttpRouteData(new HttpRoute())); diff --git a/test/System.Web.Http.WebHost.Test/BatchingTest.cs b/test/System.Web.Http.WebHost.Test/BatchingTest.cs index 57963809b..7ccb2ce26 100644 --- a/test/System.Web.Http.WebHost.Test/BatchingTest.cs +++ b/test/System.Web.Http.WebHost.Test/BatchingTest.cs @@ -43,8 +43,7 @@ public async Task WebHost_Batching_WithSpecialCharactersInUrl() Content = new MultipartContent("mixed") { new HttpMessageContent(new HttpRequestMessage(HttpMethod.Post, "http://contoso.com/values/ space")) - }, - RequestUri = new Uri("http://contoso.com/") + } }; // Arrange