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

RouteOptionsTests.cs « UnitTests « test « Routing « Http « src - github.com/dotnet/aspnetcore.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: da342ba11cfa7f2672f109c65d8cd5267c39b2d5 (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
// 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.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Xunit;

namespace Microsoft.AspNetCore.Routing.Tests;

public class RouteOptionsTests
{
    [Fact]
    public void ConfigureRouting_ConfiguresOptionsProperly()
    {
        // Arrange
        var services = new ServiceCollection();
        services.AddOptions();

        // Act
        services.AddRouting(options => options.ConstraintMap.Add("foo", typeof(TestRouteConstraint)));
        var serviceProvider = services.BuildServiceProvider();

        // Assert
        var accessor = serviceProvider.GetRequiredService<IOptions<RouteOptions>>();
        Assert.Equal("TestRouteConstraint", accessor.Value.ConstraintMap["foo"].Name);
    }

    [Fact]
    public void EndpointDataSources_WithDependencyInjection_AddedDataSourcesAddedToEndpointDataSource()
    {
        // Arrange
        var services = new ServiceCollection();
        services.AddOptions();
        services.AddRouting();
        var serviceProvider = services.BuildServiceProvider();

        var endpoint1 = new Endpoint((c) => Task.CompletedTask, EndpointMetadataCollection.Empty, string.Empty);
        var endpoint2 = new Endpoint((c) => Task.CompletedTask, EndpointMetadataCollection.Empty, string.Empty);

        var options = serviceProvider.GetRequiredService<IOptions<RouteOptions>>().Value;
        var endpointDataSource = serviceProvider.GetRequiredService<EndpointDataSource>();

        // Act 1
        options.EndpointDataSources.Add(new DefaultEndpointDataSource(endpoint1));

        // Assert 1
        var result = Assert.Single(endpointDataSource.Endpoints);
        Assert.Same(endpoint1, result);

        // Act 2
        options.EndpointDataSources.Add(new DefaultEndpointDataSource(endpoint2));

        // Assert 2
        Assert.Collection(endpointDataSource.Endpoints,
            ep => Assert.Same(endpoint1, ep),
            ep => Assert.Same(endpoint2, ep));
    }

    private class TestRouteConstraint : IRouteConstraint
    {
        public TestRouteConstraint(string pattern)
        {
            Pattern = pattern;
        }

        public string Pattern { get; private set; }
        public bool Match(
            HttpContext httpContext,
            IRouter route,
            string routeKey,
            RouteValueDictionary values,
            RouteDirection routeDirection)
        {
            throw new NotImplementedException();
        }
    }
}