|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using Microsoft.AspNetCore.Builder.Internal; |
| 8 | +using Microsoft.AspNetCore.Http; |
| 9 | +using Microsoft.AspNetCore.Http.Features; |
| 10 | +using Xunit; |
| 11 | + |
| 12 | +namespace Microsoft.AspNetCore.Builder.Extensions |
| 13 | +{ |
| 14 | + public class UsePathBaseExtensionsTests |
| 15 | + { |
| 16 | + [Theory] |
| 17 | + [InlineData(null)] |
| 18 | + [InlineData("")] |
| 19 | + [InlineData("/")] |
| 20 | + public void EmptyOrNullPathBase_DoNotAddMiddleware(string pathBase) |
| 21 | + { |
| 22 | + // Arrange |
| 23 | + var useCalled = false; |
| 24 | + var builder = new ApplicationBuilderWrapper(CreateBuilder(), () => useCalled = true) |
| 25 | + .UsePathBase(pathBase); |
| 26 | + |
| 27 | + // Act |
| 28 | + builder.Build(); |
| 29 | + |
| 30 | + // Assert |
| 31 | + Assert.False(useCalled); |
| 32 | + } |
| 33 | + |
| 34 | + private class ApplicationBuilderWrapper : IApplicationBuilder |
| 35 | + { |
| 36 | + private readonly IApplicationBuilder _wrappedBuilder; |
| 37 | + private readonly Action _useCallback; |
| 38 | + |
| 39 | + public ApplicationBuilderWrapper(IApplicationBuilder applicationBuilder, Action useCallback) |
| 40 | + { |
| 41 | + _wrappedBuilder = applicationBuilder; |
| 42 | + _useCallback = useCallback; |
| 43 | + } |
| 44 | + |
| 45 | + public IApplicationBuilder Use(Func<RequestDelegate, RequestDelegate> middleware) |
| 46 | + { |
| 47 | + _useCallback(); |
| 48 | + return _wrappedBuilder.Use(middleware); |
| 49 | + } |
| 50 | + |
| 51 | + public IServiceProvider ApplicationServices |
| 52 | + { |
| 53 | + get { return _wrappedBuilder.ApplicationServices; } |
| 54 | + set { _wrappedBuilder.ApplicationServices = value; } |
| 55 | + } |
| 56 | + |
| 57 | + public IDictionary<string, object> Properties => _wrappedBuilder.Properties; |
| 58 | + public IFeatureCollection ServerFeatures => _wrappedBuilder.ServerFeatures; |
| 59 | + public RequestDelegate Build() => _wrappedBuilder.Build(); |
| 60 | + public IApplicationBuilder New() => _wrappedBuilder.New(); |
| 61 | + |
| 62 | + } |
| 63 | + |
| 64 | + [Theory] |
| 65 | + [InlineData("/base", "", "/base", "/base", "")] |
| 66 | + [InlineData("/base", "", "/base/", "/base", "/")] |
| 67 | + [InlineData("/base", "", "/base/something", "/base", "/something")] |
| 68 | + [InlineData("/base", "", "/base/something/", "/base", "/something/")] |
| 69 | + [InlineData("/base/more", "", "/base/more", "/base/more", "")] |
| 70 | + [InlineData("/base/more", "", "/base/more/something", "/base/more", "/something")] |
| 71 | + [InlineData("/base/more", "", "/base/more/something/", "/base/more", "/something/")] |
| 72 | + [InlineData("/base", "/oldbase", "/base", "/oldbase/base", "")] |
| 73 | + [InlineData("/base", "/oldbase", "/base/", "/oldbase/base", "/")] |
| 74 | + [InlineData("/base", "/oldbase", "/base/something", "/oldbase/base", "/something")] |
| 75 | + [InlineData("/base", "/oldbase", "/base/something/", "/oldbase/base", "/something/")] |
| 76 | + [InlineData("/base/more", "/oldbase", "/base/more", "/oldbase/base/more", "")] |
| 77 | + [InlineData("/base/more", "/oldbase", "/base/more/something", "/oldbase/base/more", "/something")] |
| 78 | + [InlineData("/base/more", "/oldbase", "/base/more/something/", "/oldbase/base/more", "/something/")] |
| 79 | + public void RequestPathBaseContainingPathBase_IsSplit(string registeredPathBase, string pathBase, string requestPath, string expectedPathBase, string expectedPath) |
| 80 | + { |
| 81 | + TestPathBase(registeredPathBase, pathBase, requestPath, expectedPathBase, expectedPath); |
| 82 | + } |
| 83 | + |
| 84 | + [Theory] |
| 85 | + [InlineData("/base", "", "/something", "", "/something")] |
| 86 | + [InlineData("/base", "", "/baseandsomething", "", "/baseandsomething")] |
| 87 | + [InlineData("/base", "", "/ba", "", "/ba")] |
| 88 | + [InlineData("/base", "", "/ba/se", "", "/ba/se")] |
| 89 | + [InlineData("/base", "/oldbase", "/something", "/oldbase", "/something")] |
| 90 | + [InlineData("/base", "/oldbase", "/baseandsomething", "/oldbase", "/baseandsomething")] |
| 91 | + [InlineData("/base", "/oldbase", "/ba", "/oldbase", "/ba")] |
| 92 | + [InlineData("/base", "/oldbase", "/ba/se", "/oldbase", "/ba/se")] |
| 93 | + public void RequestPathBaseNotContainingPathBase_IsNotSplit(string registeredPathBase, string pathBase, string requestPath, string expectedPathBase, string expectedPath) |
| 94 | + { |
| 95 | + TestPathBase(registeredPathBase, pathBase, requestPath, expectedPathBase, expectedPath); |
| 96 | + } |
| 97 | + |
| 98 | + [Theory] |
| 99 | + [InlineData("", "", "/", "", "/")] |
| 100 | + [InlineData("/", "", "/", "", "/")] |
| 101 | + [InlineData("/base", "", "/base/", "/base", "/")] |
| 102 | + [InlineData("/base/", "", "/base", "/base", "")] |
| 103 | + [InlineData("/base/", "", "/base/", "/base", "/")] |
| 104 | + [InlineData("", "/oldbase", "/", "/oldbase", "/")] |
| 105 | + [InlineData("/", "/oldbase", "/", "/oldbase", "/")] |
| 106 | + [InlineData("/base", "/oldbase", "/base/", "/oldbase/base", "/")] |
| 107 | + [InlineData("/base/", "/oldbase", "/base", "/oldbase/base", "")] |
| 108 | + [InlineData("/base/", "/oldbase", "/base/", "/oldbase/base", "/")] |
| 109 | + public void PathBaseNeverEndsWithSlash(string registeredPathBase, string pathBase, string requestPath, string expectedPathBase, string expectedPath) |
| 110 | + { |
| 111 | + TestPathBase(registeredPathBase, pathBase, requestPath, expectedPathBase, expectedPath); |
| 112 | + } |
| 113 | + |
| 114 | + [Theory] |
| 115 | + [InlineData("/base", "", "/Base/Something", "/Base", "/Something")] |
| 116 | + [InlineData("/base", "/OldBase", "/Base/Something", "/OldBase/Base", "/Something")] |
| 117 | + public void PathBaseAndPathPreserveRequestCasing(string registeredPathBase, string pathBase, string requestPath, string expectedPathBase, string expectedPath) |
| 118 | + { |
| 119 | + TestPathBase(registeredPathBase, pathBase, requestPath, expectedPathBase, expectedPath); |
| 120 | + } |
| 121 | + |
| 122 | + [Theory] |
| 123 | + [InlineData("/b♫se", "", "/b♫se/something", "/b♫se", "/something")] |
| 124 | + [InlineData("/b♫se", "", "/B♫se/something", "/B♫se", "/something")] |
| 125 | + [InlineData("/b♫se", "", "/b♫se/Something", "/b♫se", "/Something")] |
| 126 | + [InlineData("/b♫se", "/oldb♫se", "/b♫se/something", "/oldb♫se/b♫se", "/something")] |
| 127 | + [InlineData("/b♫se", "/oldb♫se", "/b♫se/Something", "/oldb♫se/b♫se", "/Something")] |
| 128 | + [InlineData("/b♫se", "/oldb♫se", "/B♫se/something", "/oldb♫se/B♫se", "/something")] |
| 129 | + public void PathBaseCanHaveUnicodeCharacters(string registeredPathBase, string pathBase, string requestPath, string expectedPathBase, string expectedPath) |
| 130 | + { |
| 131 | + TestPathBase(registeredPathBase, pathBase, requestPath, expectedPathBase, expectedPath); |
| 132 | + } |
| 133 | + |
| 134 | + private static void TestPathBase(string registeredPathBase, string pathBase, string requestPath, string expectedPathBase, string expectedPath) |
| 135 | + { |
| 136 | + HttpContext requestContext = CreateRequest(pathBase, requestPath); |
| 137 | + var builder = CreateBuilder() |
| 138 | + .UsePathBase(registeredPathBase); |
| 139 | + builder.Run(context => |
| 140 | + { |
| 141 | + context.Items["test.Path"] = context.Request.Path; |
| 142 | + context.Items["test.PathBase"] = context.Request.PathBase; |
| 143 | + return Task.FromResult(0); |
| 144 | + }); |
| 145 | + builder.Build().Invoke(requestContext).Wait(); |
| 146 | + |
| 147 | + // Assert path and pathBase are split after middleware |
| 148 | + Assert.Equal(expectedPath, ((PathString)requestContext.Items["test.Path"]).Value); |
| 149 | + Assert.Equal(expectedPathBase, ((PathString)requestContext.Items["test.PathBase"]).Value); |
| 150 | + // Assert path and pathBase are reset after request |
| 151 | + Assert.Equal(pathBase, requestContext.Request.PathBase.Value); |
| 152 | + Assert.Equal(requestPath, requestContext.Request.Path.Value); |
| 153 | + } |
| 154 | + |
| 155 | + private static HttpContext CreateRequest(string pathBase, string requestPath) |
| 156 | + { |
| 157 | + HttpContext context = new DefaultHttpContext(); |
| 158 | + context.Request.PathBase = new PathString(pathBase); |
| 159 | + context.Request.Path = new PathString(requestPath); |
| 160 | + return context; |
| 161 | + } |
| 162 | + |
| 163 | + private static ApplicationBuilder CreateBuilder() |
| 164 | + { |
| 165 | + return new ApplicationBuilder(serviceProvider: null); |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments