|
| 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 | +} |
0 commit comments