コード例 #1
0
        public async Task request_monitor_handles_exceptions_but_does_not_swallow_them_by_default(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>();
                },
                                                            app =>
                {
                    app.Use(async(context, next) =>
                    {
                        try
                        {
                            await next.Invoke();
                        }
                        catch
                        {
                            ++rootExceptionCount;
                            context.Response.StatusCode = StatusCodes.Status500InternalServerError;
                        }
                    });
                    app.UseRequestMonitor(o => { o.SwallowErrors = swallow; });
                    app.UseMiddleware <StupidMiddleware>();
                });
                using (var client = new TestServerClient(new TestServer(b)))
                {
                    using (HttpResponseMessage bug = await client.Get("?bug"))
                    {
                        bug.StatusCode.Should().Be(HttpStatusCode.InternalServerError);
                        text.GetText().Should().Contain("Bug!");
                    }
                    using (HttpResponseMessage asyncBug = await client.Get("?asyncBug"))
                    {
                        asyncBug.StatusCode.Should().Be(HttpStatusCode.InternalServerError);
                        text.GetText().Should().Contain("AsyncBug!");
                    }
                }
            }
            finally
            {
                GrandOutput.Default.Dispose();
            }
            if (swallow)
            {
                Assert.That(rootExceptionCount, Is.EqualTo(0));
            }
            else
            {
                Assert.That(rootExceptionCount, Is.EqualTo(2));
            }
        }
コード例 #2
0
 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);
             }
         }
 }
コード例 #3
0
        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();
        }
コード例 #4
0
        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);
        }