public async Task RequestDepthExcluded_ExistingRequestWithoutDepth_HeaderWithRequestId()
        {
            var existingRequestId = new RequestIdBuilder()
                                    .WithNoDepth()
                                    .Build();

            var options = new RequestChainOptions
            {
                IncludeRequestDepth = false
            };

            Guid requestIdGuid;

            using (var server = new RequestIdTestServer(options))
                using (var client = server.CreateClient())
                {
                    client.ApplyRequestChain(existingRequestId);

                    var result = await client.MakeRequestAndGetRequestHeader();

                    result.Should()
                    .NotBeNullOrWhiteSpace();
                    result.Should()
                    .NotContain(":", "the header only contains the request, not the depth");
                    Guid.TryParse(result, out requestIdGuid)
                    .Should()
                    .BeTrue();
                }
        }
        public async Task RequestDepth_FromCustomRequestHeader_RequestId()
        {
            var customRequestHeader = "custom-rc-header";

            var originalDepth = 4;
            var expectedDepth = 5;

            var existingRequestId = new RequestIdBuilder()
                                    .WithDepth(originalDepth)
                                    .WithHeader(customRequestHeader)
                                    .Build();

            var options = new RequestChainOptions
            {
                RequestIdHeaderKey = customRequestHeader
            };

            using (var server = new RequestIdTestServer(options))
                using (var client = server.CreateClient())
                {
                    client.ApplyRequestChain(existingRequestId);

                    var result = await client.MakeRequestAndGetRequestDepth();

                    result.Should()
                    .Be(expectedDepth, "the original depth should increment when ApplyRequestChain is called on client");
                }
        }
        public async Task RequestId_FromCustomRequestHeader_RequestId()
        {
            var customRequestHeader = "custom-rc-header";
            var expectedId          = Guid.NewGuid();

            var existingRequestId = new RequestIdBuilder()
                                    .WithRequetId(expectedId)
                                    .WithHeader(customRequestHeader)
                                    .Build();

            var options = new RequestChainOptions
            {
                RequestIdHeaderKey = customRequestHeader
            };

            using (var server = new RequestIdTestServer(options))
                using (var client = server.CreateClient())
                {
                    client.ApplyRequestChain(existingRequestId);

                    var result = await client.MakeRequestAndGetRequestId();

                    result.Should()
                    .Be(expectedId, "the requestId comes from the existing request");
                }
        }
        public async Task BackchannelHttpHandler_NextRequest_HasDepthOf2()
        {
            using (var server = new RequestIdTestServer())
            {
                var serverHandler      = server.CreateHandler();
                var backchannelHandler = server.ServiceProvider
                                         .GetRequestChainBackchannelHttpHandler(serverHandler);

                server.SetBackchannelHttpHandler(backchannelHandler);

                using (var client = server.CreateClient())
                {
                    var response = await client.GetAsync("backchannel-depth");

                    response.EnsureSuccessStatusCode();

                    response.StatusCode.Should()
                    .Be(HttpStatusCode.OK, "the endpoint returns ok when the request id matches");

                    var backchannelDepthStr = await response.Content.ReadAsStringAsync();

                    int backchannelDepth;

                    int.TryParse(backchannelDepthStr, out backchannelDepth).Should()
                    .BeTrue("the response content should be the depth");

                    backchannelDepth.Should()
                    .Be(1, "the backchannel is the second step in the process (the first is 0)");
                }
            }
        }
        public async Task RequestDepthExcluded_OriginalRequest_ValidRequestId()
        {
            var options = new RequestChainOptions
            {
                IncludeRequestDepth = false
            };

            using (var server = new RequestIdTestServer(options))
                using (var client = server.CreateClient())
                {
                    var result = await client.MakeRequestAndGetRequestId();

                    result.Should()
                    .NotBe(default(Guid), "it should be created by the server");
                }
        }
        public async Task RequestDepthExcluded_OriginalRequest_DepthNull()
        {
            var options = new RequestChainOptions
            {
                IncludeRequestDepth = false
            };

            using (var server = new RequestIdTestServer(options))
                using (var client = server.CreateClient())
                {
                    var result = await client.MakeRequestAndGetRequestDepth();

                    result.Should()
                    .Be(null, "the request depth is disabled in options");
                }
        }
        public async Task BackchannelHttpHandler_NextRequest_SameId()
        {
            using (var server = new RequestIdTestServer())
            {
                var serverHandler      = server.CreateHandler();
                var backchannelHandler = server.ServiceProvider
                                         .GetRequestChainBackchannelHttpHandler(serverHandler);

                server.SetBackchannelHttpHandler(backchannelHandler);

                using (var client = server.CreateClient())
                {
                    var result = await client.GetAsync("backchannel-id");

                    result.EnsureSuccessStatusCode();

                    result.StatusCode.Should()
                    .Be(HttpStatusCode.OK, "the endpoint returns ok when the request id matches");
                }
            }
        }
        public async Task RequestDepthExcluded_ExistingRequestWithoutDepth_DepthNull()
        {
            var existingRequestId = new RequestIdBuilder()
                                    .WithNoDepth()
                                    .Build();

            var options = new RequestChainOptions
            {
                IncludeRequestDepth = false
            };

            using (var server = new RequestIdTestServer(options))
                using (var client = server.CreateClient())
                {
                    client.ApplyRequestChain(existingRequestId);

                    var result = await client.MakeRequestAndGetRequestDepth();

                    result.Should()
                    .Be(null, "the request depth is disabled in options");
                }
        }
 public DefaultServerFixture()
 {
     _server = new RequestIdTestServer();
 }