public async Task duplicated_UseScopedHttpContext_are_ignored_Async() { using (var host = await new HostBuilder().ConfigureWebHost( builder => { builder.UseTestServer(); builder.UseScopedHttpContext(); builder.UseScopedHttpContext(); builder.Configure(app => { app.Run(context => { context.Response.StatusCode = (int)HttpStatusCode.Unused; return(Task.CompletedTask); }); }); } ).StartAsync()) using (var client = new TestServerClient(host)) { using (HttpResponseMessage test = await client.GetAsync("")) { test.StatusCode.Should().Be(HttpStatusCode.Unused); } } }
public async Task there_is_no_scoped_HttpContext_injection_by_default_Async(string mode) { bool testDone = false; using (IHost host = await new HostBuilder().ConfigureWebHost(webBuilder => { webBuilder .ConfigureServices(services => services.AddScoped <HttpContextDependentService>()) .Configure(conf => { conf.Use((context, next) => { try { var s = context.RequestServices.GetService <HttpContextDependentService>(true); s.HttpContextIsHere.Should().BeTrue(); mode.Should().Be("WithScopedHttpContext"); testDone = true; } catch (InvalidOperationException ex) { ex.Message.Should().Be("Unable to resolve service for type 'CK.AspNet.ScopedHttpContext' while attempting to activate 'CK.AspNet.Tests.HttpContextDependentService'."); mode.Should().Be("NoScopedHttpContext"); testDone = true; } return(next()); }); conf.Run(context => { context.Response.StatusCode = (int)HttpStatusCode.Unused; return(Task.CompletedTask); }); } ).UseTestServer(); if (mode == "WithScopedHttpContext") { webBuilder.UseScopedHttpContext(); } }).StartAsync()) using (var client = new TestServerClient(host)) using (HttpResponseMessage test = await client.GetAsync("")) { test.StatusCode.Should().Be(HttpStatusCode.Unused); } testDone.Should().BeTrue(); }
public async Task Should_Call_Server_100_Times() { uint optionsIterations = 100; await TestServerClient.GetAsync(_counterReset); await Loadit.Execute.Run <HttpClient>(options => { options.VUs = 1; options.Iterations = optionsIterations; }, async (http, token) => { using var res = await http.GetAsync(Endpoint + _counterIncrement, token); }); var result = await TestServerClient.GetAsync(_counterResult); Assert.True(result.IsSuccessStatusCode); var resultCount = await result.Content.ReadAsStringAsync(); Assert.Equal(optionsIterations.ToString(), resultCount); }
public async Task request_monitor_handles_exceptions_but_does_not_swallow_them_by_default_Async(bool swallow) { var text = new TextGrandOutputHandlerConfiguration(); var config = new GrandOutputConfiguration(); config.AddHandler(text); GrandOutput.EnsureActiveDefault(config); int rootExceptionCount = 0; try { var b = Tester.WebHostBuilderFactory.Create(null, null, services => { services.AddSingleton <StupidService>(); // This test does not use the IHostBuilder.UseMonitoring(). // We must inject the IActivityMonitor explicitly. services.AddScoped <IActivityMonitor>(_ => new ActivityMonitor()); }, app => { app.Use(async(context, next) => { try { await next.Invoke(); } catch (Exception) { ++rootExceptionCount; context.Response.StatusCode = StatusCodes.Status500InternalServerError; } }); app.UseGuardRequestMonitor(o => { o.SwallowErrors = swallow; }); app.UseMiddleware <StupidMiddleware>(); }); using (var client = new TestServerClient(b.Start())) { using (HttpResponseMessage bug = await client.GetAsync("?bug")) { bug.StatusCode.Should().Be(HttpStatusCode.InternalServerError); await Task.Delay(100); var t = text.GetText(); t.Should().Contain("Bug!"); } using (HttpResponseMessage asyncBug = await client.GetAsync("?asyncBug")) { asyncBug.StatusCode.Should().Be(HttpStatusCode.InternalServerError); await Task.Delay(100); var t = text.GetText(); t.Should().Contain("AsyncBug!"); } } } finally { GrandOutput.Default.Dispose(); } if (swallow) { Assert.That(rootExceptionCount, Is.EqualTo(0)); } else { Assert.That(rootExceptionCount, Is.EqualTo(2)); } }