public void UseEndpoint_WithApplicationBuilderMismatch_Throws() { // Arrange var services = CreateServices(); var app = new ApplicationBuilder(services); app.UseRouting(); // Act var ex = Assert.Throws <InvalidOperationException>(() => app.Map("/Test", b => b.UseEndpoints(endpoints => { }))); // Assert Assert.Equal( "The EndpointRoutingMiddleware and EndpointMiddleware must be added to the same IApplicationBuilder instance. " + "To use Endpoint Routing with 'Map(...)', make sure to call 'IApplicationBuilder.UseRouting' before " + "'IApplicationBuilder.UseEndpoints' for each branch of the middleware pipeline.", ex.Message); }
public void UseEndpoints_CallWithBuilder_SetsEndpointDataSource_WithMap() { // 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.Map("/foo", b => { b.UseRouting(); b.UseEndpoints(builder => { builder.Map("/1", d => null).WithDisplayName("Test endpoint 1"); builder.Map("/2", d => null).WithDisplayName("Test endpoint 2"); }); }); 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()); requestDelegate(new DefaultHttpContext() { Request = { Path = "/Foo", }, }); // Assert Assert.Equal(2, matcherEndpointDataSources.Count); // Each UseRouter has its own data source Assert.Collection(matcherEndpointDataSources[1].Endpoints, // app.UseRouter e => Assert.Equal("Test endpoint 1", e.DisplayName), e => Assert.Equal("Test endpoint 2", e.DisplayName)); Assert.Collection(matcherEndpointDataSources[0].Endpoints, // b.UseRouter 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)); }