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

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

namespace Microsoft.AspNetCore.Routing
{
    public class DefaultEndpointDataSourceTests
    {
        [Fact]
        public void Constructor_Params_EndpointsInitialized()
        {
            // Arrange & Act
            var dataSource = new DefaultEndpointDataSource(
                new Endpoint(TestConstants.EmptyRequestDelegate, EndpointMetadataCollection.Empty, "1"),
                new Endpoint(TestConstants.EmptyRequestDelegate, EndpointMetadataCollection.Empty, "2")
                );

            // Assert
            Assert.Collection(dataSource.Endpoints,
                endpoint => Assert.Equal("1", endpoint.DisplayName),
                endpoint => Assert.Equal("2", endpoint.DisplayName));
        }

        [Fact]
        public void Constructor_Params_ShouldMakeCopyOfEndpoints()
        {
            // Arrange
            var endpoint1 = new Endpoint(TestConstants.EmptyRequestDelegate, EndpointMetadataCollection.Empty, "1");
            var endpoint2 = new Endpoint(TestConstants.EmptyRequestDelegate, EndpointMetadataCollection.Empty, "2");
            var endpoints = new[] { endpoint1, endpoint2 };

            // Act
            var dataSource = new DefaultEndpointDataSource(endpoints);
            Array.Resize(ref endpoints, 1);
            endpoints[0] = null;

            // Assert
            Assert.Equal(2, dataSource.Endpoints.Count);
            Assert.Contains(endpoint1, dataSource.Endpoints);
            Assert.Contains(endpoint2, dataSource.Endpoints);
        }

        [Fact]
        public void Constructor_Params_ShouldThrowArgumentNullExceptionWhenEndpointsIsNull()
        {
            Endpoint[] endpoints = null;

            var actual = Assert.Throws<ArgumentNullException>(() => new DefaultEndpointDataSource(endpoints));
            Assert.Equal("endpoints", actual.ParamName);
        }

        [Fact]
        public void Constructor_Enumerable_EndpointsInitialized()
        {
            // Arrange & Act
            var dataSource = new DefaultEndpointDataSource(new List<Endpoint>
            {
                new Endpoint(TestConstants.EmptyRequestDelegate, EndpointMetadataCollection.Empty, "1"),
                new Endpoint(TestConstants.EmptyRequestDelegate, EndpointMetadataCollection.Empty, "2")
            });

            // Assert
            Assert.Collection(dataSource.Endpoints,
                endpoint => Assert.Equal("1", endpoint.DisplayName),
                endpoint => Assert.Equal("2", endpoint.DisplayName));
        }

        [Fact]
        public void Constructor_Enumerable_ShouldMakeCopyOfEndpoints()
        {
            // Arrange
            var endpoint1 = new Endpoint(TestConstants.EmptyRequestDelegate, EndpointMetadataCollection.Empty, "1");
            var endpoint2 = new Endpoint(TestConstants.EmptyRequestDelegate, EndpointMetadataCollection.Empty, "2");
            var endpoints = new List<Endpoint> { endpoint1, endpoint2 };

            // Act
            var dataSource = new DefaultEndpointDataSource((IEnumerable<Endpoint>)endpoints);
            endpoints.RemoveAt(0);
            endpoints[0] = null;

            // Assert
            Assert.Equal(2, dataSource.Endpoints.Count);
            Assert.Contains(endpoint1, dataSource.Endpoints);
            Assert.Contains(endpoint2, dataSource.Endpoints);
        }

        [Fact]
        public void Constructor_Enumerable_ShouldThrowArgumentNullExceptionWhenEndpointsIsNull()
        {
            IEnumerable<Endpoint> endpoints = null;

            var actual = Assert.Throws<ArgumentNullException>(() => new DefaultEndpointDataSource(endpoints));
            Assert.Equal("endpoints", actual.ParamName);
        }
    }
}