예제 #1
0
        public void UseEndpoints_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.UseRouting();
            app.UseEndpoints(builder =>
            {
                builder.Map("/1", d => null).WithDisplayName("Test endpoint 1");
                builder.Map("/2", d => null).WithDisplayName("Test endpoint 2");
            });

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

            // 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 UseRouter has its own data source collection
            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 collection 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));
        }
예제 #2
0
        public async Task UseRouting_ServicesRegistered_Match_DoesNotSetsFeature()
        {
            // Arrange
            var endpoint = new RouteEndpoint(
                TestConstants.EmptyRequestDelegate,
                RoutePatternFactory.Parse("{*p}"),
                0,
                EndpointMetadataCollection.Empty,
                "Test");

            var services = CreateServices();

            var app = new ApplicationBuilder(services);

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.DataSources.Add(new DefaultEndpointDataSource(endpoint));
            });

            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, httpContext.GetEndpoint());
        }
예제 #3
0
        public void UseEndpoints_WithGlobalEndpointRouteBuilderHasRoutes()
        {
            // Arrange
            var services = CreateServices();

            var app = new ApplicationBuilder(services);

            var mockRouteBuilder = new Mock <IEndpointRouteBuilder>();

            mockRouteBuilder.Setup(m => m.DataSources).Returns(new List <EndpointDataSource>());

            var routeBuilder = mockRouteBuilder.Object;

            app.Properties.Add("__GlobalEndpointRouteBuilder", routeBuilder);
            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.Map("/1", d => Task.CompletedTask).WithDisplayName("Test endpoint 1");
            });

            var requestDelegate = app.Build();

            var endpointDataSource = Assert.Single(mockRouteBuilder.Object.DataSources);

            Assert.Collection(endpointDataSource.Endpoints,
                              e => Assert.Equal("Test endpoint 1", e.DisplayName));

            var routeOptions = app.ApplicationServices.GetRequiredService <IOptions <RouteOptions> >();

            Assert.Equal(mockRouteBuilder.Object.DataSources, routeOptions.Value.EndpointDataSources);
        }
예제 #4
0
        public void UseEndpoint_ServicesNotRegistered_Throws()
        {
            // Arrange
            var app = new ApplicationBuilder(Mock.Of <IServiceProvider>());

            // Act
            var ex = Assert.Throws <InvalidOperationException>(() => app.UseEndpoints(endpoints => { }));

            // 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);
        }
예제 #5
0
        public void UseEndpoint_WithoutEndpointRoutingMiddleware_Throws()
        {
            // Arrange
            var services = CreateServices();

            var app = new ApplicationBuilder(services);

            // Act
            var ex = Assert.Throws <InvalidOperationException>(() => app.UseEndpoints(endpoints => { }));

            // Assert
            Assert.Equal(
                "EndpointRoutingMiddleware matches endpoints setup by EndpointMiddleware and so must be added to the request " +
                "execution pipeline before EndpointMiddleware. " +
                "Please add EndpointRoutingMiddleware by calling 'IApplicationBuilder.UseRouting' " +
                "inside the call to 'Configure(...)' in the application startup code.",
                ex.Message);
        }
예제 #6
0
        public async Task UseEndpoint_ServicesRegisteredAndEndpointRoutingRegistered_NoMatch_DoesNotSetFeature()
        {
            // Arrange
            var services = CreateServices();

            var app = new ApplicationBuilder(services);

            app.UseRouting();
            app.UseEndpoints(endpoints => { });

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

            // Act
            await appFunc(httpContext);

            // Assert
            Assert.Null(httpContext.Features.Get <IEndpointFeature>());
        }