Exemplo n.º 1
0
        public async Task GetOrderReturnsTheCorrectResultForUnrecoverableFailures()
        {
            var responseStatus = HttpStatusCode.BadRequest;

            using (var response = new HttpResponseMessage(responseStatus))
            {
                var config = new EcommerceClientConfiguration
                {
                    RequestProtocol               = "https",
                    ServiceHostAddress            = "google.com",
                    GetOrderUrlTemplate           = "/orders/{order}",
                    RequestTimeoutSeconds         = 60,
                    ConnectionLeaseTimeoutSeconds = 300
                };

                var client = new TestECommerceClient(config, response);

                var result = await client.GetOrderDetailsAsync("ABX", null);

                result.Should().NotBeNull("because the result should have been returned");
                result.Outcome.Should().Be(Outcome.Failure, "because the request failed");
                result.Reason.Should().Be(responseStatus.ToString(), "because the status code should be used as the reason");
                result.Recoverable.Should().Be(Recoverability.Final, "because the response was not a retriable code");
                result.Payload.Should().BeNull("because there was no content from the failed request");
            }
        }
Exemplo n.º 2
0
        public async Task GetOrderDetailsAddsACorrelationId()
        {
            using (var response = new HttpResponseMessage())
            {
                var config = new EcommerceClientConfiguration
                {
                    RequestProtocol               = "https",
                    ServiceHostAddress            = "google.com",
                    GetOrderUrlTemplate           = "/orders/{order}",
                    RequestTimeoutSeconds         = 60,
                    ConnectionLeaseTimeoutSeconds = 300
                };

                var client = new TestECommerceClient(config, response);
                var expectedCorrelation = "Hello";

                await client.GetOrderDetailsAsync("ABX", expectedCorrelation);

                client.Request.Headers.TryGetValues(HttpHeaders.CorrelationId, out var correlationValues).Should().BeTrue("because there should be a correlation header");
                client.Request.Headers.TryGetValues(HttpHeaders.DefaultApplicationInsightsOperationId, out var operationValues).Should().BeTrue("because there should be an operation header");
                correlationValues.Should().HaveCount(1, "because there should be a single correlation header");
                operationValues.Should().HaveCount(1, "because there should be a single operation header");
                correlationValues.First().Should().Be(expectedCorrelation, "because the correlation id should have been set");
                operationValues.First().Should().Be(expectedCorrelation, "because the correlation id should have been set as the operation id");
            }
        }
Exemplo n.º 3
0
        public async Task GetOrderReturnsTheCorrectResultForSuccessfulRequests()
        {
            var responseStatus = HttpStatusCode.OK;

            using (var response = new HttpResponseMessage(responseStatus))
            {
                var config = new EcommerceClientConfiguration
                {
                    RequestProtocol               = "https",
                    ServiceHostAddress            = "google.com",
                    GetOrderUrlTemplate           = "/orders/{order}",
                    RequestTimeoutSeconds         = 60,
                    ConnectionLeaseTimeoutSeconds = 300
                };

                var client          = new TestECommerceClient(config, response);
                var expectedContent = "Hello";

                response.Content = new StringContent(expectedContent);

                var result = await client.GetOrderDetailsAsync("ABX", null);

                result.Should().NotBeNull("because the result should have been returned");
                result.Outcome.Should().Be(Outcome.Success, "because the request was successful");
                result.Reason.Should().Be(responseStatus.ToString(), "because the status code should be used as the reason");
                result.Recoverable.Should().Be(Recoverability.Final, "because the request was successful and final");
                result.Payload.Should().Be(expectedContent, "because the payload should have been set form response content");
            }
        }
Exemplo n.º 4
0
        public async Task GetOrderDetailsAddsStaticHeaders()
        {
            using (var response = new HttpResponseMessage())
            {
                var headers = new Dictionary <string, string>
                {
                    { "First", "One" },
                    { "Second", "Two" }
                };

                var config = new EcommerceClientConfiguration
                {
                    RequestProtocol               = "https",
                    ServiceHostAddress            = "google.com",
                    GetOrderUrlTemplate           = "/orders/{order}",
                    RequestTimeoutSeconds         = 60,
                    ConnectionLeaseTimeoutSeconds = 300,
                    StaticHeadersJson             = JsonConvert.SerializeObject(headers)
                };

                var client = new TestECommerceClient(config, response);

                await client.GetOrderDetailsAsync("ABX", null);

                foreach (var pair in headers)
                {
                    client.Request.Headers.TryGetValues(pair.Key, out var values).Should().BeTrue("because there should be a {0} header", pair.Key);
                    values.Should().HaveCount(1, "because there should be a single {0} header", pair.Key);
                    values.First().Should().Be(pair.Value, "because the {0} header should have the corresponding value", pair.Key);
                }
            }
        }
Exemplo n.º 5
0
        /// <summary>
        ///   Initializes a new instance of the <see cref="EcommerceClient"/> class.
        /// </summary>
        ///
        /// <param name="configuration">The configuration to use for the client.</param>
        ///
        public EcommerceClient(EcommerceClientConfiguration configuration,
                               JsonSerializerSettings serializerSettings)
        {
            this.configuration      = configuration ?? throw new ArgumentNullException(nameof(configuration));
            this.serializerSettings = serializerSettings;

            this.httpClient = new Lazy <HttpClient>(() => this.CreateHttpClient(configuration.RequestProtocol,
                                                                                configuration.ServiceHostAddress,
                                                                                configuration.ClientCertificateThumbprint,
                                                                                configuration.RequestTimeoutSeconds,
                                                                                configuration.ConnectionLeaseTimeoutSeconds),
                                                    LazyThreadSafetyMode.PublicationOnly);

            this.staticHeaders = new Lazy <Dictionary <string, string> >(() => JsonConvert.DeserializeObject <Dictionary <string, string> >(this.configuration.StaticHeadersJson ?? String.Empty, this.serializerSettings) ?? new Dictionary <string, string>(),
                                                                         LazyThreadSafetyMode.PublicationOnly);
        }
Exemplo n.º 6
0
        public void GetOrderDetailsHandlesNoStaticHeaders(string headerJson)
        {
            using (var response = new HttpResponseMessage())
            {
                var config = new EcommerceClientConfiguration
                {
                    RequestProtocol               = "https",
                    ServiceHostAddress            = "google.com",
                    GetOrderUrlTemplate           = "/orders/{order}",
                    RequestTimeoutSeconds         = 60,
                    ConnectionLeaseTimeoutSeconds = 300,
                    StaticHeadersJson             = headerJson
                };

                var client = new TestECommerceClient(config, response);

                Action actionUnderTest = () => client.GetOrderDetailsAsync("ABX", null).GetAwaiter().GetResult();
                actionUnderTest.ShouldNotThrow("because missing static header configuration should be valid");
            }
        }
Exemplo n.º 7
0
        public async Task GetOrderDetailsHonorsTheRequestTimeout()
        {
            using (var response = new HttpResponseMessage())
            {
                var config = new EcommerceClientConfiguration
                {
                    RequestProtocol               = "https",
                    ServiceHostAddress            = "google.com",
                    GetOrderUrlTemplate           = "/orders/{order}",
                    RequestTimeoutSeconds         = 60,
                    ConnectionLeaseTimeoutSeconds = 300
                };

                var client = new TestECommerceClient(config, response);
                var expectedCorrelation = "Hello";

                await client.GetOrderDetailsAsync("ABX", expectedCorrelation);

                client.RequestTimeout.Should().Be(TimeSpan.FromSeconds(config.RequestTimeoutSeconds), "because the configured timeout should be used");
            }
        }
Exemplo n.º 8
0
        public async Task GetOrderRequestsTheCorrectOrder()
        {
            using (var response = new HttpResponseMessage(HttpStatusCode.OK))
            {
                var config = new EcommerceClientConfiguration
                {
                    RequestProtocol               = "https",
                    ServiceHostAddress            = "google.com",
                    GetOrderUrlTemplate           = "/orders/{order}",
                    RequestTimeoutSeconds         = 60,
                    ConnectionLeaseTimeoutSeconds = 300
                };

                var order  = "ABX";
                var client = new TestECommerceClient(config, response);

                await client.GetOrderDetailsAsync(order, null);

                client.Request.RequestUri.OriginalString.Should().Be($"/orders/{order}", "because the correct request url should have been generated");
            }
        }
Exemplo n.º 9
0
        public async Task GetOrderDetailsDoesNotAddACorrelationWhenNotPassed()
        {
            using (var response = new HttpResponseMessage())
            {
                var config = new EcommerceClientConfiguration
                {
                    RequestProtocol               = "https",
                    ServiceHostAddress            = "google.com",
                    GetOrderUrlTemplate           = "/orders/{order}",
                    RequestTimeoutSeconds         = 60,
                    ConnectionLeaseTimeoutSeconds = 300
                };

                var client = new TestECommerceClient(config, response);

                await client.GetOrderDetailsAsync("ABX", null);

                client.Request.Headers.TryGetValues(HttpHeaders.CorrelationId, out var correlationValues).Should().BeFalse("because there should not be a correlation header");
                client.Request.Headers.TryGetValues(HttpHeaders.DefaultApplicationInsightsOperationId, out var operationValues).Should().BeFalse("because there should not be an operation header");
            }
        }
Exemplo n.º 10
0
 public TestECommerceClient(EcommerceClientConfiguration config,
                            HttpResponseMessage response = null) : base(config, null)
 {
     this.response = response;
 }