public void AddMiddlewareWithProvidedOptions() { // Arrange var app = MockApplicationBuilder.MockUpWithRequiredServices(out _, out _, out _); var options = new CorrelationOptions(); RequestDelegate next = new RequestDelegate((HttpContext _) => Task.CompletedTask); var middlewareWasCalled = false; Func <RequestDelegate, RequestDelegate> middlewareSupplied = null; app.OnUseCalled = (Func <RequestDelegate, RequestDelegate> middleware) => { middlewareSupplied = middleware; middlewareWasCalled = true; }; // Act IApplicationBuilder returned = app.UseCorrelationId(options); // Assert returned.Should().Be(app, "the extension method should return the application builder to allow for chaining"); middlewareWasCalled.Should().Be(true, "the correlation id middleware should have been added to the pipeline"); // Arrange if (middlewareSupplied != null) { RequestDelegate result = middlewareSupplied.Invoke(next); result.Target.Should().BeAssignableTo(typeof(CorrelationMiddleware), "the build delegate target should be an instance of CorrelationIdMiddleware"); CorrelationMiddleware middlewareInstance = result.Target as CorrelationMiddleware; middlewareInstance.Options.Should().Be(options, "the middleware should be initialized with the supplied CorrelationIdOptions"); } }
public void UseSpa_RootPathNotSet_ShouldThrowInvalidOperationException() { var services = GetServiceCollection(); services.AddSpaStaticFilesWithUrlRewrite(_ => { }); var serviceProvider = services.BuildServiceProvider(); var applicationBuilder = new MockApplicationBuilder(serviceProvider); var exception = Assert.Throws <InvalidOperationException>( () => applicationBuilder.UseSpa(_ => { })); Assert.Contains(nameof(UrlRewriteSpaStaticFilesOptions.RootPath), exception.Message, StringComparison.Ordinal); }
public void UseSpa_ShouldNotThrowException() { var services = GetServiceCollection(); services.AddSpaStaticFilesWithUrlRewrite(configuration => { configuration.RootPath = "Root"; }); var serviceProvider = services.BuildServiceProvider(); var applicationBuilder = new MockApplicationBuilder(serviceProvider); applicationBuilder.UseSpa(x => { }); }
private static (RequestDelegate, HttpContext, MockContexts) GetMiddleware(BandwidthOptions options, Action <MockHttpContext> buildHttpContext = null) { var builder = new MockApplicationBuilder(); builder.UseBandwidth(options); Assert.NotNull(builder.Middleware); var context = new MockHttpContext(); var mockContexts = new MockContexts(); context.AddService <IMemoryCache>(new MockMemoryCache(mockContexts.MemoryCache)); context.AddService <IApplication>(new MockApplication(mockContexts.Application)); context.AddService <IPhoneNumber>(new MockPhoneNumber(mockContexts.PhoneNumber)); context.AddService <IAvailableNumber>(new MockAvailableNumber(mockContexts.AvailableNumber)); context.AddService <IDomain>(new MockDomain(mockContexts.Domain)); if (buildHttpContext != null) { buildHttpContext(context); } return(builder.Middleware(c => Task.FromResult(0)), context, mockContexts); }