// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. 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(() => new DefaultEndpointDataSource(endpoints)); Assert.Equal("endpoints", actual.ParamName); } [Fact] public void Constructor_Enumerable_EndpointsInitialized() { // Arrange & Act var dataSource = new DefaultEndpointDataSource(new List { 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 { endpoint1, endpoint2 }; // Act var dataSource = new DefaultEndpointDataSource((IEnumerable)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 endpoints = null; var actual = Assert.Throws(() => new DefaultEndpointDataSource(endpoints)); Assert.Equal("endpoints", actual.ParamName); } } }