public void UseEndpointRouting_CallWithBuilder_SetsEndpointDataSource()
        {
            // Arrange
            var matcherEndpointDataSources = new List <EndpointDataSource>();
            var matcherFactoryMock         = new Mock <MatcherFactory>();

            matcherFactoryMock
            .Setup(m => m.CreateMatcher(It.IsAny <EndpointDataSource>()))
            .Callback((EndpointDataSource arg) =>
            {
                matcherEndpointDataSources.Add(arg);
            })
            .Returns(new TestMatcher(false));

            var services = CreateServices(matcherFactoryMock.Object);

            var app = new ApplicationBuilder(services);

            // Act
            app.UseEndpointRouting(builder =>
            {
                builder.Map("/1", "Test endpoint 1", d => null);
                builder.Map("/2", "Test endpoint 2", d => null);
            });

            app.UseEndpointRouting(builder =>
            {
                builder.Map("/3", "Test endpoint 3", d => null);
                builder.Map("/4", "Test endpoint 4", d => null);
            });

            // This triggers the middleware to be created and the matcher factory to be called
            // with the datasource we want to test
            var requestDelegate = app.Build();

            requestDelegate(new DefaultHttpContext());

            // Assert
            Assert.Equal(2, matcherEndpointDataSources.Count);

            // Each middleware has its own endpoints
            Assert.Collection(matcherEndpointDataSources[0].Endpoints,
                              e => Assert.Equal("Test endpoint 1", e.DisplayName),
                              e => Assert.Equal("Test endpoint 2", e.DisplayName));
            Assert.Collection(matcherEndpointDataSources[1].Endpoints,
                              e => Assert.Equal("Test endpoint 3", e.DisplayName),
                              e => Assert.Equal("Test endpoint 4", e.DisplayName));

            var compositeEndpointBuilder = services.GetRequiredService <EndpointDataSource>();

            // Global middleware has all endpoints
            Assert.Collection(compositeEndpointBuilder.Endpoints,
                              e => Assert.Equal("Test endpoint 1", e.DisplayName),
                              e => Assert.Equal("Test endpoint 2", e.DisplayName),
                              e => Assert.Equal("Test endpoint 3", e.DisplayName),
                              e => Assert.Equal("Test endpoint 4", e.DisplayName));
        }
Example #2
0
        public async Task UseEndpointRouting_ServicesRegistered_Match_DoesNotSetsFeature()
        {
            // Arrange
            var endpoint = new RouteEndpoint(
                TestConstants.EmptyRequestDelegate,
                RoutePatternFactory.Parse("{*p}"),
                0,
                EndpointMetadataCollection.Empty,
                "Test");

            var services = CreateServices(endpoint);

            var app = new ApplicationBuilder(services);

            app.UseEndpointRouting();

            var appFunc     = app.Build();
            var httpContext = new DefaultHttpContext();

            // Act
            await appFunc(httpContext);

            // Assert
            var feature = httpContext.Features.Get <IEndpointFeature>();

            Assert.NotNull(feature);
            Assert.Same(endpoint, feature.Endpoint);
        }
Example #3
0
        public void UseEndpointRouting_ServicesNotRegistered_Throws()
        {
            // Arrange
            var app = new ApplicationBuilder(Mock.Of <IServiceProvider>());

            // Act
            var ex = Assert.Throws <InvalidOperationException>(() => app.UseEndpointRouting());

            // Assert
            Assert.Equal(
                "Unable to find the required services. " +
                "Please add all the required services by calling 'IServiceCollection.AddRouting' " +
                "inside the call to 'ConfigureServices(...)' in the application startup code.",
                ex.Message);
        }
Example #4
0
        public async Task UseEndpointRouting_ServicesRegistered_NoMatch_DoesNotSetFeature()
        {
            // Arrange
            var services = CreateServices();

            var app = new ApplicationBuilder(services);

            app.UseEndpointRouting();

            var appFunc     = app.Build();
            var httpContext = new DefaultHttpContext();

            // Act
            await appFunc(httpContext);

            // Assert
            Assert.Null(httpContext.Features.Get <IEndpointFeature>());
        }
        public void UseEndpointRouting_CallWithBuilder_SetsEndpointBuilder()
        {
            // Arrange
            var services = CreateServices();

            var app = new ApplicationBuilder(services);

            // Act
            app.UseEndpointRouting(builder =>
            {
                builder.MapEndpoint(d => null, "/", "Test endpoint");
            });

            // Assert
            var dataSourceBuilder = (DefaultEndpointDataSourceBuilder)services.GetRequiredService <EndpointDataSourceBuilder>();
            var endpointBuilder   = Assert.Single(dataSourceBuilder.Endpoints);

            Assert.Equal("Test endpoint", endpointBuilder.DisplayName);
        }