Welcome to mirror list, hosted at ThFree Co, Russian Federation.

DefaultLinkParserTest.cs « UnitTests « test « Routing « Http « src - github.com/dotnet/aspnetcore.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 46a8170876e6adee07664b16aae8de5d35ee3ded (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Routing.Matching;
using Microsoft.AspNetCore.Routing.TestObjects;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Testing;
using Xunit;

namespace Microsoft.AspNetCore.Routing;

// Tests LinkParser functionality using ParsePathByAddress - see tests for the extension
// methods for more E2E tests.
//
// Does not cover template processing in detail, those scenarios are validated by other tests.
public class DefaultLinkParserTest : LinkParserTestBase
{
    [Fact]
    public void ParsePathByAddresss_NoMatchingEndpoint_ReturnsNull()
    {
        // Arrange
        var endpoint = EndpointFactory.CreateRouteEndpoint("{controller}/{action}/{id?}", displayName: "Test1", metadata: new object[] { new IntMetadata(1), });

        var sink = new TestSink();
        var loggerFactory = new TestLoggerFactory(sink, enabled: true);
        var parser = CreateLinkParser(services => { services.AddSingleton<ILoggerFactory>(loggerFactory); }, endpoint);

        // Act
        var values = parser.ParsePathByAddress(0, "/Home/Index/17");

        // Assert
        Assert.Null(values);

        Assert.Collection(
            sink.Writes,
            w => Assert.Equal("No endpoints found for address 0", w.Message));
    }

    [Fact]
    public void ParsePathByAddresss_HasMatches_ReturnsNullWhenParsingFails()
    {
        // Arrange
        var endpoint1 = EndpointFactory.CreateRouteEndpoint("{controller}/{action}/{id}", displayName: "Test1", metadata: new object[] { new IntMetadata(1), });
        var endpoint2 = EndpointFactory.CreateRouteEndpoint("{controller}/{action}/{id2}", displayName: "Test2", metadata: new object[] { new IntMetadata(0), });

        var sink = new TestSink();
        var loggerFactory = new TestLoggerFactory(sink, enabled: true);
        var parser = CreateLinkParser(services => { services.AddSingleton<ILoggerFactory>(loggerFactory); }, endpoint1, endpoint2);

        // Act
        var values = parser.ParsePathByAddress(0, "/");

        // Assert
        Assert.Null(values);

        Assert.Collection(
            sink.Writes,
            w => Assert.Equal("Found the endpoints Test2 for address 0", w.Message),
            w => Assert.Equal("Path parsing failed for endpoints Test2 and URI path /", w.Message));
    }

    [Fact]
    public void ParsePathByAddresss_HasMatches_ReturnsFirstSuccessfulParse()
    {
        // Arrange
        var endpoint0 = EndpointFactory.CreateRouteEndpoint("{controller}/{action}", displayName: "Test1", metadata: new object[] { new IntMetadata(0), });
        var endpoint1 = EndpointFactory.CreateRouteEndpoint("{controller}/{action}/{id}", displayName: "Test2", metadata: new object[] { new IntMetadata(0), });
        var endpoint2 = EndpointFactory.CreateRouteEndpoint("{controller}/{action}/{id2}", displayName: "Test3", metadata: new object[] { new IntMetadata(0), });

        var sink = new TestSink();
        var loggerFactory = new TestLoggerFactory(sink, enabled: true);
        var parser = CreateLinkParser(services => { services.AddSingleton<ILoggerFactory>(loggerFactory); }, endpoint0, endpoint1, endpoint2);

        // Act
        var values = parser.ParsePathByAddress(0, "/Home/Index/17");

        // Assert
        MatcherAssert.AssertRouteValuesEqual(new { controller = "Home", action = "Index", id = "17" }, values);

        Assert.Collection(
            sink.Writes,
            w => Assert.Equal("Found the endpoints Test1, Test2, Test3 for address 0", w.Message),
            w => Assert.Equal("Path parsing succeeded for endpoint Test2 and URI path /Home/Index/17", w.Message));
    }

    [Fact]
    public void ParsePathByAddresss_HasMatches_IncludesDefaults()
    {
        // Arrange
        var endpoint = EndpointFactory.CreateRouteEndpoint("{controller=Home}/{action=Index}/{id?}", metadata: new object[] { new IntMetadata(0), });

        var parser = CreateLinkParser(endpoint);

        // Act
        var values = parser.ParsePathByAddress(0, "/");

        // Assert
        MatcherAssert.AssertRouteValuesEqual(new { controller = "Home", action = "Index", }, values);
    }

    [Fact]
    public void ParsePathByAddresss_HasMatches_RunsConstraints()
    {
        // Arrange
        var endpoint0 = EndpointFactory.CreateRouteEndpoint("{controller}/{action}/{id:int}", metadata: new object[] { new IntMetadata(0), });
        var endpoint1 = EndpointFactory.CreateRouteEndpoint("{controller}/{action}/{id2:alpha}", metadata: new object[] { new IntMetadata(0), });

        var parser = CreateLinkParser(endpoint0, endpoint1);

        // Act
        var values = parser.ParsePathByAddress(0, "/Home/Index/abc");

        // Assert
        MatcherAssert.AssertRouteValuesEqual(new { controller = "Home", action = "Index", id2 = "abc" }, values);
    }

    [Fact]
    public void GetRoutePatternMatcher_CanCache()
    {
        // Arrange
        var endpoint1 = EndpointFactory.CreateRouteEndpoint("{controller}/{action}/{id}", metadata: new object[] { new IntMetadata(1), });
        var dataSource = new DynamicEndpointDataSource(endpoint1);

        var parser = CreateLinkParser(dataSources: new[] { dataSource });

        var expected = parser.GetMatcherState(endpoint1);

        // Act
        var actual = parser.GetMatcherState(endpoint1);

        // Assert
        Assert.Same(expected.Matcher, actual.Matcher);
        Assert.Same(expected.Constraints, actual.Constraints);
    }

    [Fact]
    public void GetRoutePatternMatcherr_CanClearCache()
    {
        // Arrange
        var endpoint1 = EndpointFactory.CreateRouteEndpoint("{controller}/{action}/{id}", metadata: new object[] { new IntMetadata(1), });
        var dataSource = new DynamicEndpointDataSource(endpoint1);

        var parser = CreateLinkParser(dataSources: new[] { dataSource });
        var original = parser.GetMatcherState(endpoint1);

        var endpoint2 = EndpointFactory.CreateRouteEndpoint("{controller}/{action}/{id}", metadata: new object[] { new IntMetadata(1), });
        dataSource.AddEndpoint(endpoint2);

        // Act
        var actual = parser.GetMatcherState(endpoint1);

        // Assert
        Assert.NotSame(original.Matcher, actual.Matcher);
        Assert.NotSame(original.Constraints, actual.Constraints);
    }

    protected override void AddAdditionalServices(IServiceCollection services)
    {
        services.AddSingleton<IEndpointAddressScheme<int>, IntAddressScheme>();
    }

    private class IntAddressScheme : IEndpointAddressScheme<int>
    {
        private readonly EndpointDataSource _dataSource;

        public IntAddressScheme(EndpointDataSource dataSource)
        {
            _dataSource = dataSource;
        }

        public IEnumerable<Endpoint> FindEndpoints(int address)
        {
            return _dataSource.Endpoints.Where(e => e.Metadata.GetMetadata<IntMetadata>().Value == address);
        }
    }

    private class IntMetadata
    {
        public IntMetadata(int value)
        {
            Value = value;
        }
        public int Value { get; }
    }
}