From b7bb7f8fcfb62418d1e124c3197dfb7939bdf905 Mon Sep 17 00:00:00 2001 From: Chris Ross Date: Fri, 14 Nov 2014 09:43:33 -0800 Subject: [PATCH] Fix issue with empty path. --- src/Microsoft.AspNet.TestHost/TestServer.cs | 5 +++ .../ClientHandlerTests.cs | 15 +++++++ .../TestClientTests.cs | 42 +++++++++++++++++++ 3 files changed, 62 insertions(+) diff --git a/src/Microsoft.AspNet.TestHost/TestServer.cs b/src/Microsoft.AspNet.TestHost/TestServer.cs index 8a98adb5ad..496fc9da94 100644 --- a/src/Microsoft.AspNet.TestHost/TestServer.cs +++ b/src/Microsoft.AspNet.TestHost/TestServer.cs @@ -72,6 +72,11 @@ namespace Microsoft.AspNet.TestHost public HttpMessageHandler CreateHandler() { var pathBase = BaseAddress == null ? PathString.Empty : PathString.FromUriComponent(BaseAddress); + if (pathBase.Equals(new PathString("/"))) + { + // When we just have http://host/ the trailing slash is really part of the Path, not the PathBase. + pathBase = PathString.Empty; + } return new ClientHandler(Invoke, pathBase); } diff --git a/test/Microsoft.AspNet.TestHost.Tests/ClientHandlerTests.cs b/test/Microsoft.AspNet.TestHost.Tests/ClientHandlerTests.cs index 13b6f085c7..23f65bce8b 100644 --- a/test/Microsoft.AspNet.TestHost.Tests/ClientHandlerTests.cs +++ b/test/Microsoft.AspNet.TestHost.Tests/ClientHandlerTests.cs @@ -45,6 +45,21 @@ namespace Microsoft.AspNet.TestHost return httpClient.GetAsync("https://example.com/A/Path/and/file.txt?and=query"); } + [Fact] + public Task SingleSlashNotMovedToPathBase() + { + var handler = new ClientHandler(env => + { + var context = new DefaultHttpContext((IFeatureCollection)env); + Assert.Equal("", context.Request.PathBase.Value); + Assert.Equal("/", context.Request.Path.Value); + + return Task.FromResult(0); + }, new PathString("")); + var httpClient = new HttpClient(handler); + return httpClient.GetAsync("https://example.com/"); + } + [Fact] public async Task ResubmitRequestWorks() { diff --git a/test/Microsoft.AspNet.TestHost.Tests/TestClientTests.cs b/test/Microsoft.AspNet.TestHost.Tests/TestClientTests.cs index d9843657dd..c1ba2d9f59 100644 --- a/test/Microsoft.AspNet.TestHost.Tests/TestClientTests.cs +++ b/test/Microsoft.AspNet.TestHost.Tests/TestClientTests.cs @@ -45,6 +45,48 @@ namespace Microsoft.AspNet.TestHost Assert.Equal(expected, actual); } + [Fact] + public async Task NoTrailingSlash_NoPathBase() + { + // Arrange + var expected = "GET Response"; + RequestDelegate appDelegate = ctx => + { + Assert.Equal("", ctx.Request.PathBase.Value); + Assert.Equal("/", ctx.Request.Path.Value); + return ctx.Response.WriteAsync(expected); + }; + var server = TestServer.Create(_services, app => app.Run(appDelegate)); + var client = server.CreateClient(); + + // Act + var actual = await client.GetStringAsync("http://localhost:12345"); + + // Assert + Assert.Equal(expected, actual); + } + + [Fact] + public async Task SingleTrailingSlash_NoPathBase() + { + // Arrange + var expected = "GET Response"; + RequestDelegate appDelegate = ctx => + { + Assert.Equal("", ctx.Request.PathBase.Value); + Assert.Equal("/", ctx.Request.Path.Value); + return ctx.Response.WriteAsync(expected); + }; + var server = TestServer.Create(_services, app => app.Run(appDelegate)); + var client = server.CreateClient(); + + // Act + var actual = await client.GetStringAsync("http://localhost:12345/"); + + // Assert + Assert.Equal(expected, actual); + } + [Fact] public async Task PutAsyncWorks() {