Skip to content

Commit b55ec1c

Browse files
author
Ryan Nowak
committed
add functional test
1 parent 259000f commit b55ec1c

File tree

3 files changed

+157
-0
lines changed

3 files changed

+157
-0
lines changed

src/Http/Routing/src/DependencyInjection/RoutingServiceCollectionExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public static IServiceCollection AddRouting(this IServiceCollection services)
8585
services.TryAddSingleton<LinkGenerator, DefaultLinkGenerator>();
8686
services.TryAddSingleton<IEndpointAddressScheme<string>, EndpointNameAddressScheme>();
8787
services.TryAddSingleton<IEndpointAddressScheme<RouteValuesAddress>, RouteValuesAddressScheme>();
88+
services.TryAddSingleton<LinkParser, DefaultLinkParser>();
8889

8990
//
9091
// Endpoint Selection
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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.Linq;
5+
using System.Net;
6+
using System.Net.Http;
7+
using System.Threading.Tasks;
8+
using Microsoft.AspNetCore.Hosting;
9+
using Newtonsoft.Json.Linq;
10+
using Xunit;
11+
12+
namespace Microsoft.AspNetCore.Mvc.FunctionalTests
13+
{
14+
// Functional tests for MVC's scenarios with LinkParser
15+
public class LinkParserTest : IClassFixture<MvcTestFixture<RoutingWebSite.StartupForLinkGenerator>>
16+
{
17+
public LinkParserTest(MvcTestFixture<RoutingWebSite.StartupForLinkGenerator> fixture)
18+
{
19+
var factory = fixture.Factories.FirstOrDefault() ?? fixture.WithWebHostBuilder(ConfigureWebHostBuilder);
20+
Client = factory.CreateDefaultClient();
21+
}
22+
23+
private static void ConfigureWebHostBuilder(IWebHostBuilder builder) =>
24+
builder.UseStartup<RoutingWebSite.StartupForLinkGenerator>();
25+
26+
public HttpClient Client { get; }
27+
28+
[Fact]
29+
public async Task ParsePathByEndpoint_CanParsedWithDefaultRoute()
30+
{
31+
// Act
32+
var response = await Client.GetAsync("LinkParser/Index/18");
33+
var values = await response.Content.ReadAsAsync<JObject>();
34+
35+
// Assert
36+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
37+
Assert.Collection(
38+
values.Properties().OrderBy(p => p.Name),
39+
p =>
40+
{
41+
Assert.Equal("action", p.Name);
42+
Assert.Equal("Index", p.Value.Value<string>());
43+
},
44+
p =>
45+
{
46+
Assert.Equal("controller", p.Name);
47+
Assert.Equal("LinkParser", p.Value.Value<string>());
48+
},
49+
p =>
50+
{
51+
Assert.Equal("id", p.Name);
52+
Assert.Equal("18", p.Value.Value<string>());
53+
});
54+
}
55+
56+
[Fact]
57+
public async Task ParsePathByEndpoint_CanParsedWithNamedAttributeRoute()
58+
{
59+
// Act
60+
//
61+
// %2F => /
62+
var response = await Client.GetAsync("LinkParser/Another?path=%2Fsome-path%2Fa%2Fb%2Fc");
63+
var values = await response.Content.ReadAsAsync<JObject>();
64+
65+
// Assert
66+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
67+
Assert.Collection(
68+
values.Properties().OrderBy(p => p.Name),
69+
p =>
70+
{
71+
Assert.Equal("action", p.Name);
72+
Assert.Equal("AnotherRoute", p.Value.Value<string>());
73+
},
74+
p =>
75+
{
76+
Assert.Equal("controller", p.Name);
77+
Assert.Equal("LinkParser", p.Value.Value<string>());
78+
},
79+
p =>
80+
{
81+
Assert.Equal("x", p.Name);
82+
Assert.Equal("a", p.Value.Value<string>());
83+
},
84+
p =>
85+
{
86+
Assert.Equal("y", p.Name);
87+
Assert.Equal("b", p.Value.Value<string>());
88+
},
89+
p =>
90+
{
91+
Assert.Equal("z", p.Name);
92+
Assert.Equal("c", p.Value.Value<string>());
93+
});
94+
}
95+
}
96+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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.Linq;
6+
using Microsoft.AspNetCore.Mvc;
7+
using Microsoft.AspNetCore.Routing;
8+
using Newtonsoft.Json.Linq;
9+
10+
namespace RoutingWebSite.Controllers
11+
{
12+
public class LinkParserController : Controller
13+
{
14+
private readonly LinkParser _linkParser;
15+
16+
public LinkParserController(LinkParser linkParser)
17+
{
18+
_linkParser = linkParser;
19+
}
20+
21+
public JObject Index()
22+
{
23+
var parsed = _linkParser.ParsePathByEndpointName("default", HttpContext.Request.Path);
24+
if (parsed == null)
25+
{
26+
throw new Exception("Parsing failed.");
27+
}
28+
29+
return ToJObject(parsed);
30+
}
31+
32+
public JObject Another(string path)
33+
{
34+
var parsed = _linkParser.ParsePathByEndpointName("AnotherRoute", path);
35+
if (parsed == null)
36+
{
37+
throw new Exception("Parsing failed.");
38+
}
39+
40+
return ToJObject(parsed);
41+
}
42+
43+
[Route("some-path/{x}/{y}/{z?}", Name = "AnotherRoute")]
44+
public void AnotherRoute()
45+
{
46+
throw null;
47+
}
48+
49+
private static JObject ToJObject(RouteValueDictionary values)
50+
{
51+
var obj = new JObject();
52+
foreach (var kvp in values)
53+
{
54+
obj.Add(kvp.Key, new JValue(kvp.Value.ToString()));
55+
}
56+
57+
return obj;
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)