2
0
Эх сурвалжийг харах

Unit test route name with RouteUrl and ambient values (#6719)

James Newton-King 7 жил өмнө
parent
commit
df7bfe5243

+ 22 - 0
src/Mvc/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RoutingTestsBase.cs

@@ -26,6 +26,28 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
 
         public HttpClient Client { get; }
 
+        [Theory]
+        [InlineData("http://localhost/Login/Index", "Login", "Index", "http://localhost/Login")]
+        [InlineData("http://localhost/Login/Sso", "Login", "Sso", "http://localhost/Login/Sso")]
+        [InlineData("http://localhost/Contact/Index", "Contact", "Index", "http://localhost/Contact")]
+        [InlineData("http://localhost/Contact/Sso", "Contact", "Sso", "http://localhost/Contact/Sso")]
+        public async Task ConventionalRoutedAction_RouteUrl_AmbientValues(string requestUrl, string controller, string action, string expectedUrl)
+        {
+            // Arrange & Act
+            var response = await Client.GetAsync(requestUrl);
+
+            // Assert
+            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
+
+            var body = await response.Content.ReadAsStringAsync();
+            var result = JsonConvert.DeserializeObject<RoutingResult>(body);
+
+            Assert.Equal(controller, result.Controller);
+            Assert.Equal(action, result.Action);
+
+            Assert.Equal(expectedUrl, Assert.Single(result.ExpectedUrls));
+        }
+
         [Fact]
         public async Task ConventionalRoutedAction_RouteContainsPage_RouteNotMatched()
         {

+ 28 - 0
src/Mvc/test/WebSites/RoutingWebSite/Controllers/ContactController.cs

@@ -0,0 +1,28 @@
+// 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.
+
+using Microsoft.AspNetCore.Mvc;
+
+namespace RoutingWebSite
+{
+    // This controller is reachable via traditional routing.
+    public class ContactController : Controller
+    {
+        private readonly TestResponseGenerator _generator;
+
+        public ContactController(TestResponseGenerator generator)
+        {
+            _generator = generator;
+        }
+
+        public IActionResult Index()
+        {
+            return _generator.Generate(Url.RouteUrl("ActionAsMethod", null, Url.ActionContext.HttpContext.Request.Scheme));
+        }
+
+        public IActionResult Sso()
+        {
+            return _generator.Generate(Url.RouteUrl("ActionAsMethod", null, Url.ActionContext.HttpContext.Request.Scheme));
+        }
+    }
+}

+ 28 - 0
src/Mvc/test/WebSites/RoutingWebSite/Controllers/LoginController.cs

@@ -0,0 +1,28 @@
+// 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.
+
+using Microsoft.AspNetCore.Mvc;
+
+namespace RoutingWebSite
+{
+    // This controller is reachable via traditional routing.
+    public class LoginController : Controller
+    {
+        private readonly TestResponseGenerator _generator;
+
+        public LoginController(TestResponseGenerator generator)
+        {
+            _generator = generator;
+        }
+
+        public IActionResult Index()
+        {
+            return _generator.Generate(Url.RouteUrl("ActionAsMethod", null, Url.ActionContext.HttpContext.Request.Scheme));
+        }
+
+        public IActionResult Sso()
+        {
+            return _generator.Generate(Url.RouteUrl("ActionAsMethod", null, Url.ActionContext.HttpContext.Request.Scheme));
+        }
+    }
+}