Change test parameter transformer to slugify values (#8453)
This commit is contained in:
parent
4b83f7b510
commit
8791f9ad0d
|
|
@ -21,7 +21,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
|
||||||
public async Task ParameterTransformer_TokenReplacement_Found()
|
public async Task ParameterTransformer_TokenReplacement_Found()
|
||||||
{
|
{
|
||||||
// Arrange & Act
|
// Arrange & Act
|
||||||
var response = await Client.GetAsync("http://localhost/_ParameterTransformer_/_Test_");
|
var response = await Client.GetAsync("http://localhost/parameter-transformer/my-action");
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||||
|
|
@ -30,14 +30,14 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
|
||||||
var result = JsonConvert.DeserializeObject<RoutingResult>(body);
|
var result = JsonConvert.DeserializeObject<RoutingResult>(body);
|
||||||
|
|
||||||
Assert.Equal("ParameterTransformer", result.Controller);
|
Assert.Equal("ParameterTransformer", result.Controller);
|
||||||
Assert.Equal("Test", result.Action);
|
Assert.Equal("MyAction", result.Action);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task ParameterTransformer_TokenReplacement_NotFound()
|
public async Task ParameterTransformer_TokenReplacement_NotFound()
|
||||||
{
|
{
|
||||||
// Arrange & Act
|
// Arrange & Act
|
||||||
var response = await Client.GetAsync("http://localhost/ParameterTransformer/Test");
|
var response = await Client.GetAsync("http://localhost/ParameterTransformer/MyAction");
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
||||||
|
|
@ -79,7 +79,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
|
||||||
public async Task AttributeRoutedAction_ParameterTransformer_Found()
|
public async Task AttributeRoutedAction_ParameterTransformer_Found()
|
||||||
{
|
{
|
||||||
// Arrange & Act
|
// Arrange & Act
|
||||||
var response = await Client.GetAsync("http://localhost/_EndpointRouting_/ParameterTransformer");
|
var response = await Client.GetAsync("http://localhost/endpoint-routing/ParameterTransformer");
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||||
|
|
@ -105,7 +105,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
|
||||||
public async Task AttributeRoutedAction_ParameterTransformer_LinkToSelf()
|
public async Task AttributeRoutedAction_ParameterTransformer_LinkToSelf()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var url = LinkFrom("http://localhost/_EndpointRouting_/ParameterTransformer").To(new { });
|
var url = LinkFrom("http://localhost/endpoint-routing/ParameterTransformer").To(new { });
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var response = await Client.GetAsync(url);
|
var response = await Client.GetAsync(url);
|
||||||
|
|
@ -118,14 +118,14 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
|
||||||
Assert.Equal("EndpointRouting", result.Controller);
|
Assert.Equal("EndpointRouting", result.Controller);
|
||||||
Assert.Equal("ParameterTransformer", result.Action);
|
Assert.Equal("ParameterTransformer", result.Action);
|
||||||
|
|
||||||
Assert.Equal("/_EndpointRouting_/ParameterTransformer", result.Link);
|
Assert.Equal("/endpoint-routing/ParameterTransformer", result.Link);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task AttributeRoutedAction_ParameterTransformer_LinkWithAmbientController()
|
public async Task AttributeRoutedAction_ParameterTransformer_LinkWithAmbientController()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var url = LinkFrom("http://localhost/_EndpointRouting_/ParameterTransformer").To(new { action = "Get", id = 5 });
|
var url = LinkFrom("http://localhost/endpoint-routing/ParameterTransformer").To(new { action = "Get", id = 5 });
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var response = await Client.GetAsync(url);
|
var response = await Client.GetAsync(url);
|
||||||
|
|
@ -138,14 +138,14 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
|
||||||
Assert.Equal("EndpointRouting", result.Controller);
|
Assert.Equal("EndpointRouting", result.Controller);
|
||||||
Assert.Equal("ParameterTransformer", result.Action);
|
Assert.Equal("ParameterTransformer", result.Action);
|
||||||
|
|
||||||
Assert.Equal("/_EndpointRouting_/5", result.Link);
|
Assert.Equal("/endpoint-routing/5", result.Link);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task AttributeRoutedAction_ParameterTransformer_LinkToAttributeRoutedController()
|
public async Task AttributeRoutedAction_ParameterTransformer_LinkToAttributeRoutedController()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var url = LinkFrom("http://localhost/_EndpointRouting_/ParameterTransformer").To(new { action = "ShowPosts", controller = "Blog" });
|
var url = LinkFrom("http://localhost/endpoint-routing/ParameterTransformer").To(new { action = "ShowPosts", controller = "Blog" });
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var response = await Client.GetAsync(url);
|
var response = await Client.GetAsync(url);
|
||||||
|
|
@ -165,7 +165,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
|
||||||
public async Task AttributeRoutedAction_ParameterTransformer_LinkToConventionalController()
|
public async Task AttributeRoutedAction_ParameterTransformer_LinkToConventionalController()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var url = LinkFrom("http://localhost/_EndpointRouting_/ParameterTransformer").To(new { action = "Index", controller = "Home" });
|
var url = LinkFrom("http://localhost/endpoint-routing/ParameterTransformer").To(new { action = "Index", controller = "Home" });
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var response = await Client.GetAsync(url);
|
var response = await Client.GetAsync(url);
|
||||||
|
|
@ -292,7 +292,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
|
||||||
public async Task ConventionalRoutedAction_ParameterTransformer()
|
public async Task ConventionalRoutedAction_ParameterTransformer()
|
||||||
{
|
{
|
||||||
// Arrange & Act
|
// Arrange & Act
|
||||||
var response = await Client.GetAsync("http://localhost/ConventionalTransformerRoute/_ConventionalTransformer_/Index");
|
var response = await Client.GetAsync("http://localhost/ConventionalTransformerRoute/conventional-transformer/Index");
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||||
|
|
@ -317,7 +317,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
|
||||||
public async Task ConventionalRoutedAction_ParameterTransformer_DefaultValue()
|
public async Task ConventionalRoutedAction_ParameterTransformer_DefaultValue()
|
||||||
{
|
{
|
||||||
// Arrange & Act
|
// Arrange & Act
|
||||||
var response = await Client.GetAsync("http://localhost/ConventionalTransformerRoute/_ConventionalTransformer_");
|
var response = await Client.GetAsync("http://localhost/ConventionalTransformerRoute/conventional-transformer");
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||||
|
|
@ -332,7 +332,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
|
||||||
public async Task ConventionalRoutedAction_ParameterTransformer_WithParam()
|
public async Task ConventionalRoutedAction_ParameterTransformer_WithParam()
|
||||||
{
|
{
|
||||||
// Arrange & Act
|
// Arrange & Act
|
||||||
var response = await Client.GetAsync("http://localhost/ConventionalTransformerRoute/_ConventionalTransformer_/Param/_value_");
|
var response = await Client.GetAsync("http://localhost/ConventionalTransformerRoute/conventional-transformer/Param/my-value");
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||||
|
|
@ -342,14 +342,14 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
|
||||||
Assert.Equal("ConventionalTransformer", result.Controller);
|
Assert.Equal("ConventionalTransformer", result.Controller);
|
||||||
Assert.Equal("Param", result.Action);
|
Assert.Equal("Param", result.Action);
|
||||||
|
|
||||||
Assert.Equal("/ConventionalTransformerRoute/_ConventionalTransformer_/Param/_value_", Assert.Single(result.ExpectedUrls));
|
Assert.Equal("/ConventionalTransformerRoute/conventional-transformer/Param/my-value", Assert.Single(result.ExpectedUrls));
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task ConventionalRoutedAction_ParameterTransformer_LinkToConventionalController()
|
public async Task ConventionalRoutedAction_ParameterTransformer_LinkToConventionalController()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var url = LinkFrom("http://localhost/ConventionalTransformerRoute/_ConventionalTransformer_/Index").To(new { action = "Index", controller = "Home" });
|
var url = LinkFrom("http://localhost/ConventionalTransformerRoute/conventional-transformer/Index").To(new { action = "Index", controller = "Home" });
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var response = await Client.GetAsync(url);
|
var response = await Client.GetAsync(url);
|
||||||
|
|
@ -368,7 +368,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
|
||||||
public async Task ConventionalRoutedAction_ParameterTransformer_LinkToConventionalControllerWithParam()
|
public async Task ConventionalRoutedAction_ParameterTransformer_LinkToConventionalControllerWithParam()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var url = LinkFrom("http://localhost/ConventionalTransformerRoute/_ConventionalTransformer_/Index").To(new { action = "Param", controller = "ConventionalTransformer", param = "value" });
|
var url = LinkFrom("http://localhost/ConventionalTransformerRoute/conventional-transformer/Index").To(new { action = "Param", controller = "ConventionalTransformer", param = "MyValue" });
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var response = await Client.GetAsync(url);
|
var response = await Client.GetAsync(url);
|
||||||
|
|
@ -380,14 +380,14 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
|
||||||
|
|
||||||
Assert.Equal("ConventionalTransformer", result.Controller);
|
Assert.Equal("ConventionalTransformer", result.Controller);
|
||||||
Assert.Equal("Index", result.Action);
|
Assert.Equal("Index", result.Action);
|
||||||
Assert.Equal("/ConventionalTransformerRoute/_ConventionalTransformer_/Param/_value_", result.Link);
|
Assert.Equal("/ConventionalTransformerRoute/conventional-transformer/Param/my-value", result.Link);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task ConventionalRoutedAction_ParameterTransformer_LinkToSelf()
|
public async Task ConventionalRoutedAction_ParameterTransformer_LinkToSelf()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var url = LinkFrom("http://localhost/ConventionalTransformerRoute/_ConventionalTransformer_/Index").To(new {});
|
var url = LinkFrom("http://localhost/ConventionalTransformerRoute/conventional-transformer/Index").To(new {});
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var response = await Client.GetAsync(url);
|
var response = await Client.GetAsync(url);
|
||||||
|
|
@ -399,7 +399,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
|
||||||
|
|
||||||
Assert.Equal("ConventionalTransformer", result.Controller);
|
Assert.Equal("ConventionalTransformer", result.Controller);
|
||||||
Assert.Equal("Index", result.Action);
|
Assert.Equal("Index", result.Action);
|
||||||
Assert.Equal("/ConventionalTransformerRoute/_ConventionalTransformer_", result.Link);
|
Assert.Equal("/ConventionalTransformerRoute/conventional-transformer", result.Link);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ namespace RoutingWebSite
|
||||||
|
|
||||||
public IActionResult Param(string param)
|
public IActionResult Param(string param)
|
||||||
{
|
{
|
||||||
return _generator.Generate($"/ConventionalTransformerRoute/_ConventionalTransformer_/Param/{param}");
|
return _generator.Generate($"/ConventionalTransformerRoute/conventional-transformer/Param/{param}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
namespace RoutingWebSite
|
namespace RoutingWebSite
|
||||||
{
|
{
|
||||||
[Route("/{controller:test-transformer}")]
|
[Route("/{controller:slugify}")]
|
||||||
public class EndpointRoutingController : Controller
|
public class EndpointRoutingController : Controller
|
||||||
{
|
{
|
||||||
private readonly TestResponseGenerator _generator;
|
private readonly TestResponseGenerator _generator;
|
||||||
|
|
@ -21,16 +21,16 @@ namespace RoutingWebSite
|
||||||
return _generator.Generate("/EndpointRouting/Index", "/EndpointRouting");
|
return _generator.Generate("/EndpointRouting/Index", "/EndpointRouting");
|
||||||
}
|
}
|
||||||
|
|
||||||
[Route("/{controller:test-transformer}/{action}")]
|
[Route("/{controller:slugify}/{action}")]
|
||||||
public IActionResult ParameterTransformer()
|
public IActionResult ParameterTransformer()
|
||||||
{
|
{
|
||||||
return _generator.Generate("/_EndpointRouting_/ParameterTransformer");
|
return _generator.Generate("/endpoint-routing/ParameterTransformer");
|
||||||
}
|
}
|
||||||
|
|
||||||
[Route("{id}")]
|
[Route("{id}")]
|
||||||
public IActionResult Get(int id)
|
public IActionResult Get(int id)
|
||||||
{
|
{
|
||||||
return _generator.Generate("/_EndpointRouting_/" + id);
|
return _generator.Generate("/endpoint-routing/" + id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -19,9 +19,9 @@ namespace RoutingWebSite
|
||||||
_generator = generator;
|
_generator = generator;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IActionResult Test()
|
public IActionResult MyAction()
|
||||||
{
|
{
|
||||||
return _generator.Generate("/_ParameterTransformer_/_Test_");
|
return _generator.Generate("/parameter-transformer/my-action");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,13 +21,13 @@ namespace RoutingWebSite
|
||||||
// Add route token transformer to one controller
|
// Add route token transformer to one controller
|
||||||
options.Conventions.Add(new ControllerRouteTokenTransformerConvention(
|
options.Conventions.Add(new ControllerRouteTokenTransformerConvention(
|
||||||
typeof(ParameterTransformerController),
|
typeof(ParameterTransformerController),
|
||||||
new TestParameterTransformer()));
|
new SlugifyParameterTransformer()));
|
||||||
})
|
})
|
||||||
.SetCompatibilityVersion(CompatibilityVersion.Latest);
|
.SetCompatibilityVersion(CompatibilityVersion.Latest);
|
||||||
services
|
services
|
||||||
.AddRouting(options =>
|
.AddRouting(options =>
|
||||||
{
|
{
|
||||||
options.ConstraintMap["test-transformer"] = typeof(TestParameterTransformer);
|
options.ConstraintMap["slugify"] = typeof(SlugifyParameterTransformer);
|
||||||
});
|
});
|
||||||
|
|
||||||
services.AddScoped<TestResponseGenerator>();
|
services.AddScoped<TestResponseGenerator>();
|
||||||
|
|
@ -47,7 +47,7 @@ namespace RoutingWebSite
|
||||||
|
|
||||||
routes.MapRoute(
|
routes.MapRoute(
|
||||||
"ConventionalTransformerRoute",
|
"ConventionalTransformerRoute",
|
||||||
"ConventionalTransformerRoute/{controller:test-transformer}/{action=Index}/{param:test-transformer?}",
|
"ConventionalTransformerRoute/{controller:slugify}/{action=Index}/{param:slugify?}",
|
||||||
defaults: null,
|
defaults: null,
|
||||||
constraints: new { controller = "ConventionalTransformer" });
|
constraints: new { controller = "ConventionalTransformer" });
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,18 @@
|
||||||
// Copyright (c) .NET Foundation. All rights reserved.
|
// Copyright (c) .NET Foundation. All rights reserved.
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
using Microsoft.AspNetCore.Routing;
|
using Microsoft.AspNetCore.Routing;
|
||||||
|
|
||||||
namespace RoutingWebSite
|
namespace RoutingWebSite
|
||||||
{
|
{
|
||||||
public class TestParameterTransformer : IParameterTransformer
|
public class SlugifyParameterTransformer : IParameterTransformer
|
||||||
{
|
{
|
||||||
public string Transform(string value)
|
public string Transform(string value)
|
||||||
{
|
{
|
||||||
return "_" + value + "_";
|
// Slugify value
|
||||||
|
return Regex.Replace(value, "([a-z])([A-Z])", "$1-$2", RegexOptions.None, TimeSpan.FromMilliseconds(100)).ToLower();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue