public TestModule(TestClock clock) : base("/test") { Get["/action"] = _ => { clock.Advance(TimeUnit.Milliseconds, 100); return Response.AsText("response"); }; Post["/post"] = _ => { clock.Advance(TimeUnit.Milliseconds, 200); return HttpStatusCode.OK; }; Get["/error"] = _ => { throw new InvalidOperationException(); }; }
public TestModule(TestClock clock) : base("/test") { this.MetricForRequestTimeAndResponseSize("ActionRequest", "Get", "/"); this.MetricForRequestSize("RequestSize", "Put", "/"); Get["/action"] = _ => { clock.Advance(TimeUnit.Milliseconds, 100); return Response.AsText("response"); }; Get["/contentWithLength"] = _ => { clock.Advance(TimeUnit.Milliseconds, 100); return Response.AsText("response").WithHeader("Content-Length", "100"); }; Put["/size"] = _ => HttpStatusCode.OK; }
public OwinMetricsTestData() { const int timePerRequest = 100; const string json = "{ 'id': '1'} "; Clock = new TestClock(); var scheduler = new TestScheduler(Clock); TimerMetric = new TimerMetric(SamplingType.SlidingWindow, new MeterMetric(Clock, scheduler), Clock); CounterMetric = new TestCounter(); HistogramMetric = new HistogramMetric(); MeterMetric = new MeterMetric(Clock, scheduler); var server = TestServer.Create(app => { var registery = new TestRegistry { TimerInstance = TimerMetric, CounterInstance = CounterMetric, HistogramInstance = HistogramMetric, MeterInstance = MeterMetric }; OwinMetricsConfig owin = new OwinMetricsConfig(middleware => app.Use(middleware), registery, Metric.Config.HealthStatus); owin.WithRequestMetricsConfig(c => c.RegisterAllMetrics()); app.Run(context => { Clock.Advance(TimeUnit.Milliseconds, timePerRequest); if (context.Request.Path.ToString() == "/test/action") { return context.Response.WriteAsync("response"); } if (context.Request.Path.ToString() == "/test/error") { context.Response.StatusCode = 500; return context.Response.WriteAsync("response"); } if (context.Request.Path.ToString() == "/test/size") { return context.Response.WriteAsync("response"); } if (context.Request.Path.ToString() == "/test/post") { return context.Response.WriteAsync("response"); } context.Response.StatusCode = 404; return context.Response.WriteAsync("not found"); }); }); ExpectedResults = new OwinExpectedMetrics(timePerRequest, 6, 1); server.HttpClient.GetAsync("http://local.test/test/error").Result.StatusCode.Should().Be(HttpStatusCode.InternalServerError); server.HttpClient.GetAsync("http://local.test/test/action").Result.StatusCode.Should().Be(HttpStatusCode.OK); server.HttpClient.GetAsync("http://local.test/test/action").Result.StatusCode.Should().Be(HttpStatusCode.OK); server.HttpClient.GetAsync("http://local.test/test/action").Result.StatusCode.Should().Be(HttpStatusCode.OK); server.HttpClient.GetAsync("http://local.test/test/action").Result.StatusCode.Should().Be(HttpStatusCode.OK); var postContent = new StringContent(json); postContent.Headers.Add("Content-Length", json.Length.ToString()); server.HttpClient.PostAsync("http://local.test/test/post", postContent); }