public async Task Can_Clean_Context()
        {
            var context = new MockOwinContext();

            var sut = new OperationIdContextMiddleware(
                new NoopMiddleware(),
                new OperationIdContextMiddlewareConfiguration());

            await sut.Invoke(context);

            context.Get<string>(Consts.OperationIdContextKey).Should().BeNull();
            OperationIdContext.Get().Should().BeNull();
        }
        public async Task Can_Establish_Id_Context()
        {
            var actual = new OperationIdCollectingMiddleware();

            var sut = new OperationIdContextMiddleware(
                actual,
                new OperationIdContextMiddlewareConfiguration());

            await sut.Invoke(new MockOwinContext());

            actual.OperationIdFromAmbientContext.Should().NotBeNullOrEmpty();
            actual.OperationIdFromAmbientContext.Should()
                .Be(actual.OperationIdFromEnvironment);
        }
        public async Task Can_Send_Request_Telemetry()
        {
            // given
            var channel = new MockTelemetryChannel();

            var request = Mock.Of<IOwinRequest>(r =>
                r.Method == "GET" &&
                r.Path == new PathString("/path") &&
                r.Uri == new Uri("http://google.com/path")
                );

            var response = Mock.Of<IOwinResponse>(r => r.StatusCode == 200);

            var context = new MockOwinContextBuilder()
                .WithRequest(request)
                .WithResponse(response)
                .Build();

            var configuration = new TelemetryConfigurationBuilder()
                .WithChannel(channel)
                .Build();


            var sut = new OperationIdContextMiddleware(
                new HttpRequestTrackingMiddleware(
                    new NoopMiddleware(), configuration),
                new OperationIdContextMiddlewareConfiguration());

            // when
            await sut.Invoke(context);

            // then
            channel.SentTelemetries.Count.Should().Be(1);

            var telemetry = channel.SentTelemetries.First() as RequestTelemetry;
            telemetry.Should().NotBeNull();

            telemetry.HttpMethod.Should().Be("GET");
            telemetry.Name.Should().Be("GET /path");
            telemetry.Context.Operation.Name.Should().Be("GET /path");
            telemetry.Id.Should().NotBeNullOrEmpty();
            telemetry.Success.Should().BeTrue();
            telemetry.Url.Should().Be(new Uri("http://google.com/path"));
            telemetry.StartTime.Date.Should().Be(DateTimeOffset.Now.Date);
        }
        public async Task Should_Send_Request_Telemetry_When_Not_Filtered_Out()
        {
            // given
            var channel = new MockTelemetryChannel();

            var request = Mock.Of<IOwinRequest>(r =>
                r.Method == "GET" &&
                r.Path == new PathString("/path") &&
                r.Uri == new Uri("http://google.com/path")
                );

            var response = Mock.Of<IOwinResponse>(r => r.StatusCode == 200);

            var context = new MockOwinContextBuilder()
                .WithRequest(request)
                .WithResponse(response)
                .Build();

            var configuration = new TelemetryConfigurationBuilder()
                .WithChannel(channel)
                .Build();


            var sut = new OperationIdContextMiddleware(
                new HttpRequestTrackingMiddleware(
                    new NoopMiddleware(), configuration, (req, resp) => true),
                new OperationIdContextMiddlewareConfiguration());

            // when
            await sut.Invoke(context);

            // then
            channel.SentTelemetries.Count.Should().Be(1);

            var telemetry = channel.SentTelemetries.First() as RequestTelemetry;
            telemetry.Should().NotBeNull();
        }
        public async Task Can_Pass_Request_Details_For_Filtering()
        {
            // given
            var channel = new MockTelemetryChannel();

            var request = Mock.Of<IOwinRequest>(r =>
                r.Method == "GET" &&
                r.Path == new PathString("/path") &&
                r.Uri == new Uri("http://google.com/path")
                );

            var response = Mock.Of<IOwinResponse>(r => r.StatusCode == 200);

            var context = new MockOwinContextBuilder()
                .WithRequest(request)
                .WithResponse(response)
                .Build();

            var configuration = new TelemetryConfigurationBuilder()
                .WithChannel(channel)
                .Build();
            IOwinRequest filteredRequest = null;
            IOwinResponse filteredResponse = null;

            var sut = new OperationIdContextMiddleware(
                new HttpRequestTrackingMiddleware(
                    new NoopMiddleware(), configuration, (req, resp) =>
                    {
                        filteredRequest = req;
                        filteredResponse = resp;
                        return false;
                    }),
                new OperationIdContextMiddlewareConfiguration());

            // when
            await sut.Invoke(context);

            // then
            filteredRequest.ShouldBeEquivalentTo(request);
            filteredResponse.ShouldBeEquivalentTo(response);

        }