public void CreateHttpClientCanParseAHostWithoutPort()
        {
            var host     = "dev.test.com";
            var testBase = new CertificateTestBase();
            var client   = testBase.CreateHttpClient("http", host, null, 30, 30);

            client.BaseAddress.Host.Should().Be(host, "because the host should have been used");
        }
        public void CreateHttpClientDoesNotRequestACertificateWithoutAThumbprint()
        {
            var testBase = new CertificateTestBase();

            testBase.CreateHttpClient("http", "google.com", null, 30, 30);

            testBase.CertificateRequested.Should().BeFalse("because no thumbprint was passed");
        }
        public void CreateHttpClientThrowsWhenNoCertificateIsFound()
        {
            var cert     = default(X509Certificate2);
            var thumb    = "omg, hai1";
            var testBase = new CertificateTestBase(cert);

            Action actionUnderTest = () => testBase.CreateHttpClient("http", "google.com", thumb, 30, 30);

            actionUnderTest.ShouldThrow <MissingDependencyException>("because the certificate was not found");
        }
        public void CreateHttpClientRequestsACertificateWhenAThumbprintIsProvided()
        {
            var cert     = new X509Certificate2();
            var thumb    = "omg, hai1";
            var testBase = new CertificateTestBase(cert);

            testBase.CreateHttpClient("http", "google.com", thumb, 30, 30);

            testBase.CertificateRequested.Should().BeTrue("because the thumbprint was passed");
            testBase.RequestedThumbprint.Should().Be(thumb, "because the proper thumbprint should be used");
        }
        public void CreateHttpClientIsInitialized()
        {
            var cert     = new X509Certificate2();
            var thumb    = "omg, hai1";
            var timeout  = 30;
            var testBase = new CertificateTestBase(cert);

            using (var client = testBase.CreateHttpClient("http", "google.com", thumb, timeout, timeout))
            {
                client.Should().NotBeNull("because a client should have been created");
                client.BaseAddress.ShouldBeEquivalentTo(new Uri("http://google.com"), "because the protocol and base address should have been used");
                client.DefaultRequestHeaders.Accept.Should().HaveCount(1, "because only JSON should be accepted");
                client.DefaultRequestHeaders.Accept.First().MediaType.Should().Be(MimeTypes.Json, "because the JSON MIME type should be set");
                client.DefaultRequestHeaders.CacheControl?.NoStore.Should().BeTrue("because the client should not cache responses");
                client.Timeout.Should().Be(TimeSpan.FromSeconds(timeout), "because the timeout should have been set");

                var handlerField = typeof(HttpMessageInvoker).GetField("handler", BindingFlags.Instance | BindingFlags.NonPublic);
                var handler      = (handlerField?.GetValue(client) as WebRequestHandler);

                handler.Should().NotBeNull("because the base handler for the client should hve been retrievable");
                handler.ClientCertificates.Should().HaveCount(1, "because only the requested client certificate should be associated with the handler");
                handler.ClientCertificates[0].Should().Be(cert, "because the found certificate should be associated with the client");
            }
        }