Skip to content

Commit 7487a1f

Browse files
authored
Fix MapPath and UsePathBase's use of implicit PathStrings (#41382)
1 parent b1d2844 commit 7487a1f

File tree

5 files changed

+30
-15
lines changed

5 files changed

+30
-15
lines changed

src/Http/Http.Abstractions/src/Extensions/MapMiddleware.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public Task Invoke(HttpContext context)
6464
return _next(context);
6565
}
6666

67-
private async Task InvokeCore(HttpContext context, string matchedPath, string remainingPath)
67+
private async Task InvokeCore(HttpContext context, PathString matchedPath, PathString remainingPath)
6868
{
6969
var path = context.Request.Path;
7070
var pathBase = context.Request.PathBase;

src/Http/Http.Abstractions/src/Extensions/UsePathBaseExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
@@ -26,7 +26,7 @@ public static IApplicationBuilder UsePathBase(this IApplicationBuilder app, Path
2626
}
2727

2828
// Strip trailing slashes
29-
pathBase = pathBase.Value?.TrimEnd('/');
29+
pathBase = new PathString(pathBase.Value?.TrimEnd('/'));
3030
if (!pathBase.HasValue)
3131
{
3232
return app;
@@ -35,4 +35,4 @@ public static IApplicationBuilder UsePathBase(this IApplicationBuilder app, Path
3535
return app.UseMiddleware<UsePathBaseMiddleware>(pathBase);
3636
}
3737
}
38-
}
38+
}

src/Http/Http.Abstractions/src/Extensions/UsePathBaseMiddleware.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public Task Invoke(HttpContext context)
5555
return _next(context);
5656
}
5757

58-
private async Task InvokeCore(HttpContext context, string matchedPath, string remainingPath)
58+
private async Task InvokeCore(HttpContext context, PathString matchedPath, PathString remainingPath)
5959
{
6060
var originalPath = context.Request.Path;
6161
var originalPathBase = context.Request.PathBase;

src/Http/Http.Abstractions/test/MapPathMiddlewareTests.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,27 @@ public void NullArguments_ArgumentNullException()
4343
}
4444

4545
[Theory]
46-
[InlineData("/foo", "", "/foo")]
47-
[InlineData("/foo", "", "/foo/")]
48-
[InlineData("/foo", "/Bar", "/foo")]
49-
[InlineData("/foo", "/Bar", "/foo/cho")]
50-
[InlineData("/foo", "/Bar", "/foo/cho/")]
51-
[InlineData("/foo/cho", "/Bar", "/foo/cho")]
52-
[InlineData("/foo/cho", "/Bar", "/foo/cho/do")]
53-
public async Task PathMatchFunc_BranchTaken(string matchPath, string basePath, string requestPath)
46+
[InlineData("/foo", "", "/foo", "/foo", "")]
47+
[InlineData("/foo", "", "/foo/", "/foo", "/")]
48+
[InlineData("/foo", "/Bar", "/foo", "/Bar/foo", "")]
49+
[InlineData("/foo", "/Bar", "/foo/cho", "/Bar/foo", "/cho")]
50+
[InlineData("/foo", "/Bar", "/foo/cho/", "/Bar/foo", "/cho/")]
51+
[InlineData("/foo/cho", "/Bar", "/foo/cho", "/Bar/foo/cho", "")]
52+
[InlineData("/foo/cho", "/Bar", "/foo/cho/do", "/Bar/foo/cho", "/do")]
53+
[InlineData("/foo%42/cho", "/Bar%42", "/foo%42/cho/do%42", "/Bar%42/foo%42/cho", "/do%42")]
54+
public async Task PathMatchFunc_BranchTaken(string matchPath, string basePath, string requestPath, string expectedPathBase, string expectedPath)
5455
{
5556
HttpContext context = CreateRequest(basePath, requestPath);
5657
var builder = new ApplicationBuilder(serviceProvider: null!);
57-
builder.Map(matchPath, UseSuccess);
58+
builder.Map(new PathString(matchPath), UseSuccess);
5859
var app = builder.Build();
5960
await app.Invoke(context);
6061

6162
Assert.Equal(200, context.Response.StatusCode);
6263
Assert.Equal(basePath, context.Request.PathBase.Value);
6364
Assert.Equal(requestPath, context.Request.Path.Value);
65+
Assert.Equal(expectedPathBase, (string)context.Items["test.PathBase"]!);
66+
Assert.Equal(expectedPath, context.Items["test.Path"]);
6467
}
6568

6669
[Theory]

src/Http/Http.Abstractions/test/UsePathBaseExtensionsTests.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,23 @@ public Task PathBaseCanHaveUnicodeCharacters(string registeredPathBase, string p
131131
return TestPathBase(registeredPathBase, pathBase, requestPath, expectedPathBase, expectedPath);
132132
}
133133

134+
[Theory]
135+
[InlineData("/b%42", "", "/b%42/something%42", "/b%42", "/something%42")]
136+
[InlineData("/b%42", "", "/B%42/something%42", "/B%42", "/something%42")]
137+
[InlineData("/b%42", "", "/b%42/Something%42", "/b%42", "/Something%42")]
138+
[InlineData("/b%42", "/oldb%42", "/b%42/something%42", "/oldb%42/b%42", "/something%42")]
139+
[InlineData("/b%42", "/oldb%42", "/b%42/Something%42", "/oldb%42/b%42", "/Something%42")]
140+
[InlineData("/b%42", "/oldb%42", "/B%42/something%42", "/oldb%42/B%42", "/something%42")]
141+
public Task PathBaseCanHavePercentCharacters(string registeredPathBase, string pathBase, string requestPath, string expectedPathBase, string expectedPath)
142+
{
143+
return TestPathBase(registeredPathBase, pathBase, requestPath, expectedPathBase, expectedPath);
144+
}
145+
134146
private static async Task TestPathBase(string registeredPathBase, string pathBase, string requestPath, string expectedPathBase, string expectedPath)
135147
{
136148
HttpContext requestContext = CreateRequest(pathBase, requestPath);
137149
var builder = CreateBuilder()
138-
.UsePathBase(registeredPathBase);
150+
.UsePathBase(new PathString(registeredPathBase));
139151
builder.Run(context =>
140152
{
141153
context.Items["test.Path"] = context.Request.Path;

0 commit comments

Comments
 (0)