public void Create_Returns_Client_With_DefaultRequestHeader_Accept_Json()
        {
            // setup
            var credentials = new VendorCredentials
            {
                Id           = Guid.NewGuid(),
                SharedSecret = Guid.NewGuid()
            };

            var subject = new HttpClientCreator(new ProxyConfiguration());

            // execute
            var client = subject.Create(credentials);

            // assert
            Assert.True(client.DefaultRequestHeaders.Accept.Contains(new MediaTypeWithQualityHeaderValue("application/json")), "Expected application/json header but was not found");
        }
        public void Create_Returns_Client_With_DefaultRequestHeader_VendorId()
        {
            // setup
            var credentials = new VendorCredentials
            {
                Id           = Guid.NewGuid(),
                SharedSecret = Guid.NewGuid()
            };

            var subject = new HttpClientCreator(new ProxyConfiguration());

            // execute
            var client = subject.Create(credentials);

            // assert
            Assert.True(client.DefaultRequestHeaders.Contains("X-InsuranceHub-VendorId"));
            Assert.Equal(credentials.Id.ToString("D"), client.DefaultRequestHeaders.GetValues(HttpClientCreator.VendorIdHeaderKey).First());
        }
        public void When_Credentials_Are_Same_Create_Returns_SameClient()
        {
            // setup
            var credentials = new VendorCredentials
            {
                Id = Guid.NewGuid(),

                SharedSecret = Guid.NewGuid()
            };

            var subject = new HttpClientCreator(new ProxyConfiguration());

            // execute
            var client1 = subject.Create(credentials);
            var client2 = subject.Create(credentials);

            // assert
            Assert.Same(client1, client2);
        }
        public void When_ThrowExceptions_IsTrue_InvalidCredentials_Request_Throws_WebException()
        {
            // set up
#if (NET452)
            // explicitly support TLS 1.2
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
#endif

#if NETFULL
            var vendorId = Guid.Parse(ConfigurationManager.AppSettings[VendorCredentialsFromConfig.IdKey]);
            var secret   = Guid.NewGuid();

            var defaultCredentials = new VendorCredentials
            {
                Id           = vendorId,
                SharedSecret = secret
            };

            var requestor = new OfferingRequestor(new OfferingRequestorConfiguration {
                ThrowExceptions = true
            }, new JsonSerializer(), new JsonDeserializer(), new HttpClientCreator(new ProxyConfiguration()), new TokenGenerator(new HmacSha256HashGenerator(Encoding.UTF8), new DateTimeProvider()), defaultCredentials);
#else
            var builder = new ConfigurationBuilder()
                          .SetBasePath(string.Concat(Directory.GetCurrentDirectory(), @"\..\..\..\..\..\..\InsuranceHub.Tests.Configuration"))
                          .AddJsonFile("Insurancehub.Client.Test.Acceptance.json");

            var rootConfig = builder.Build();

            var vendorCredentials = rootConfig.GetSection("insuranceHub:credentials").Get <VendorCredentials>();
            vendorCredentials.SharedSecret = Guid.NewGuid();

            var requestorConfig = rootConfig.GetSection("insuranceHub:offeringRequestService").Get <OfferingRequestorConfiguration>();
            requestorConfig.ThrowExceptions = true;

            var requestor = new OfferingRequestor(requestorConfig, new JsonSerializer(), new JsonDeserializer(), new HttpClientCreator(new ProxyConfiguration()), new TokenGenerator(new HmacSha256HashGenerator(Encoding.UTF8), new DateTimeProvider()), vendorCredentials);
#endif

            var vendorReference = Guid.NewGuid();

            var products = new List <Product>
            {
                new Product {
                    CategoryCode = "TKT", CurrencyCode = "GBP", Price = 10.50, CompletionDate = DateTime.UtcNow.AddMonths(2)
                }
            };

            var request = new OfferingRequest
            {
#if NETFULL
                VendorId = vendorId,
#else
                VendorId = vendorCredentials.Id,
#endif
                VendorRequestReference = vendorReference.ToString("N"),
                PremiumAsSummary       = true,
                Products = products.ToArray()
            };

            // exercise
            var ex = Assert.Throws <AggregateException>(() => requestor.Request(request));

            // verify
            Assert.IsType <WebException>(ex.GetBaseException());
            var baseEx = (WebException)ex.GetBaseException();
            Assert.NotNull(baseEx.Response);
            Assert.IsType <SimpleWebResponse>(baseEx.Response);
            var response = (SimpleWebResponse)baseEx.Response;
            Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
        }
        public void When_ThrowExcptions_IsFalse_InvalidCompletionDate_Request_Returns_Offering_WithErrorResponse_400()
        {
            // set up
#if (NET452)
            // explicitly support TLS 1.2
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
#endif

#if NETFULL
            var vendorId = Guid.Parse(ConfigurationManager.AppSettings[VendorCredentialsFromConfig.IdKey]);
            var secret   = Guid.Parse(ConfigurationManager.AppSettings[VendorCredentialsFromConfig.SharedSecretKey]);

            var defaultCredentials = new VendorCredentials
            {
                Id           = vendorId,
                SharedSecret = secret
            };

            var requestor = new OfferingRequestor(new OfferingRequestorConfiguration {
                ThrowExceptions = false
            }, new JsonSerializer(), new JsonDeserializer(), new HttpClientCreator(new ProxyConfiguration()), new TokenGenerator(new HmacSha256HashGenerator(Encoding.UTF8), new DateTimeProvider()), defaultCredentials);
#else
            var builder = new ConfigurationBuilder()
                          .SetBasePath(string.Concat(Directory.GetCurrentDirectory(), @"\..\..\..\..\..\..\InsuranceHub.Tests.Configuration"))
                          .AddJsonFile("Insurancehub.Client.Test.Acceptance.json");

            var rootConfig = builder.Build();

            var vendorCredentials = rootConfig.GetSection("insuranceHub:credentials").Get <VendorCredentials>();

            var requestorConfig = rootConfig.GetSection("insuranceHub:offeringRequestService").Get <OfferingRequestorConfiguration>();
            requestorConfig.ThrowExceptions = false;

            var requestor = new OfferingRequestor(requestorConfig, new JsonSerializer(), new JsonDeserializer(), new HttpClientCreator(new ProxyConfiguration()), new TokenGenerator(new HmacSha256HashGenerator(Encoding.UTF8), new DateTimeProvider()), vendorCredentials);
#endif

            var vendorReference = Guid.NewGuid();

            var products = new List <Product>
            {
                new Product {
                    CategoryCode = "TKT", CurrencyCode = "GBP", Price = 10.50, CompletionDate = DateTime.UtcNow.AddMonths(-2)
                }
            };

            var request = new OfferingRequest
            {
#if NETFULL
                VendorId = vendorId,
#else
                VendorId = vendorCredentials.Id,
#endif
                VendorRequestReference = vendorReference.ToString("N"),
                PremiumAsSummary       = true,
                Products = products.ToArray()
            };

            // exercise
            var actual = requestor.Request(request);

            // verify
            Assert.NotNull(actual);
            Assert.NotNull(actual.ErrorResponse);
            Assert.IsType <Offering>(actual);
            Assert.False(actual.Success);
            Assert.Equal("Invalid Request", actual.ErrorResponse.Message);
            Assert.NotEmpty(actual.ErrorResponse.ValidationMessages);
            Assert.Equal("Product completionDate can not be in the past", actual.ErrorResponse.ValidationMessages[0]);
            Assert.Equal(HttpStatusCode.BadRequest, actual.ErrorResponse.HttpStatusCode);
        }