Skip to content

Commit df7bfe5

Browse files
authored
Unit test route name with RouteUrl and ambient values (#6719)
1 parent 17616a5 commit df7bfe5

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

src/Mvc/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RoutingTestsBase.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,28 @@ private static void ConfigureWebHostBuilder(IWebHostBuilder builder) =>
2626

2727
public HttpClient Client { get; }
2828

29+
[Theory]
30+
[InlineData("http://localhost/Login/Index", "Login", "Index", "http://localhost/Login")]
31+
[InlineData("http://localhost/Login/Sso", "Login", "Sso", "http://localhost/Login/Sso")]
32+
[InlineData("http://localhost/Contact/Index", "Contact", "Index", "http://localhost/Contact")]
33+
[InlineData("http://localhost/Contact/Sso", "Contact", "Sso", "http://localhost/Contact/Sso")]
34+
public async Task ConventionalRoutedAction_RouteUrl_AmbientValues(string requestUrl, string controller, string action, string expectedUrl)
35+
{
36+
// Arrange & Act
37+
var response = await Client.GetAsync(requestUrl);
38+
39+
// Assert
40+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
41+
42+
var body = await response.Content.ReadAsStringAsync();
43+
var result = JsonConvert.DeserializeObject<RoutingResult>(body);
44+
45+
Assert.Equal(controller, result.Controller);
46+
Assert.Equal(action, result.Action);
47+
48+
Assert.Equal(expectedUrl, Assert.Single(result.ExpectedUrls));
49+
}
50+
2951
[Fact]
3052
public async Task ConventionalRoutedAction_RouteContainsPage_RouteNotMatched()
3153
{
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 Microsoft.AspNetCore.Mvc;
5+
6+
namespace RoutingWebSite
7+
{
8+
// This controller is reachable via traditional routing.
9+
public class ContactController : Controller
10+
{
11+
private readonly TestResponseGenerator _generator;
12+
13+
public ContactController(TestResponseGenerator generator)
14+
{
15+
_generator = generator;
16+
}
17+
18+
public IActionResult Index()
19+
{
20+
return _generator.Generate(Url.RouteUrl("ActionAsMethod", null, Url.ActionContext.HttpContext.Request.Scheme));
21+
}
22+
23+
public IActionResult Sso()
24+
{
25+
return _generator.Generate(Url.RouteUrl("ActionAsMethod", null, Url.ActionContext.HttpContext.Request.Scheme));
26+
}
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 Microsoft.AspNetCore.Mvc;
5+
6+
namespace RoutingWebSite
7+
{
8+
// This controller is reachable via traditional routing.
9+
public class LoginController : Controller
10+
{
11+
private readonly TestResponseGenerator _generator;
12+
13+
public LoginController(TestResponseGenerator generator)
14+
{
15+
_generator = generator;
16+
}
17+
18+
public IActionResult Index()
19+
{
20+
return _generator.Generate(Url.RouteUrl("ActionAsMethod", null, Url.ActionContext.HttpContext.Request.Scheme));
21+
}
22+
23+
public IActionResult Sso()
24+
{
25+
return _generator.Generate(Url.RouteUrl("ActionAsMethod", null, Url.ActionContext.HttpContext.Request.Scheme));
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)