Пример #1
0
        private static async Task <string> GetPageAsync(string page, string hostname = null, string[] pins = null, byte[] clientCert = null, string certPassword = null, byte[] caCert = null)
        {
            var secureHttpClientHandler = SecureHttpClientHandlerBuilder.Build();

            if (pins != null)
            {
                secureHttpClientHandler.AddCertificatePinner(hostname, pins);
            }
            if (clientCert != null)
            {
                var provider = new ImportedClientCertificateProvider();
                provider.Import(clientCert, certPassword);
                secureHttpClientHandler.SetClientCertificates(provider);
            }
            if (caCert != null)
            {
                secureHttpClientHandler.SetTrustedRoots(caCert);
            }
            string result;

            using (var httpClient = new HttpClient(secureHttpClientHandler))
                using (var response = await httpClient.GetAsync(page).ConfigureAwait(false))
                {
                    result = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                    if (!response.IsSuccessStatusCode)
                    {
                        throw new HttpResponseException(response.StatusCode, result);
                    }
                }
            return(result);
        }
Пример #2
0
        public async Task HttpTest_SetCookies()
        {
            const string cookie1 = "k1=v1; Path=/";
            const string cookie2 = "k2=v2; Path=/";
            Dictionary <string, string> cookies;
            IEnumerable <string>        respCookies;
            var secureHttpClientHandler = SecureHttpClientHandlerBuilder.Build();

            using (var httpClient = new HttpClient(secureHttpClientHandler))
            {
                var page1     = $@"https://httpbin.org/response-headers?Set-Cookie={WebUtility.UrlEncode(cookie1)}&Set-Cookie={WebUtility.UrlEncode(cookie2)}";
                var response1 = await httpClient.GetAsync(page1).ConfigureAwait(false);

                response1.Headers.TryGetValues("set-cookie", out respCookies);
                respCookies = respCookies?.SelectMany(c => c.Split(',')).Select(c => c.Trim());
                const string page2  = @"https://httpbin.org/cookies";
                var          result = await GetPageAsync(httpClient, page2).ConfigureAwait(false);

                var json = JToken.Parse(result);
                cookies = json["cookies"].ToObject <Dictionary <string, string> >();
            }
            Assert.Equal(new List <string> {
                cookie1, cookie2
            }, respCookies);
            Assert.Contains(new KeyValuePair <string, string>("k1", "v1"), cookies);
            Assert.Contains(new KeyValuePair <string, string>("k2", "v2"), cookies);
        }
        public async Task CertificatePinnerTest_OneHost_Failure()
        {
            var secureHttpClientHandler = SecureHttpClientHandlerBuilder.Build();

            secureHttpClientHandler.AddCertificatePinner(Hostname, PinsKo);

            await AssertCertificatePinnerFailureAsync(Page, secureHttpClientHandler).ConfigureAwait(false);
        }
        public async Task CertificatePinnerTest_TwoHosts_SecondHostFails()
        {
            var secureHttpClientHandler = SecureHttpClientHandlerBuilder.Build();

            secureHttpClientHandler.AddCertificatePinner(Hostname, PinsOk);
            secureHttpClientHandler.AddCertificatePinner(Hostname2, Pins2Ko);

            await AssertCertificatePinnerSuccessAsync(Page, secureHttpClientHandler).ConfigureAwait(false);
            await AssertCertificatePinnerFailureAsync(Page2, secureHttpClientHandler).ConfigureAwait(false);
        }
Пример #5
0
        private static async Task <byte[]> GetBytesAsync(string page)
        {
            byte[] result;
            var    secureHttpClientHandler = SecureHttpClientHandlerBuilder.Build();

            using (var httpClient = new HttpClient(secureHttpClientHandler))
                using (var response = await httpClient.GetAsync(page).ConfigureAwait(false))
                {
                    result = await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
                }
            return(result);
        }
Пример #6
0
        public async Task HttpTest_DeleteCookie()
        {
            Dictionary <string, string> cookies;
            var secureHttpClientHandler = SecureHttpClientHandlerBuilder.Build();

            using (var httpClient = new HttpClient(secureHttpClientHandler))
            {
                const string page1 = @"https://httpbin.org/cookies/set?k1=v1";
                await GetPageAsync(httpClient, page1).ConfigureAwait(false);

                const string page2  = @"https://httpbin.org/cookies/delete?k1";
                var          result = await GetPageAsync(httpClient, page2).ConfigureAwait(false);

                var json = JToken.Parse(result);
                cookies = json["cookies"].ToObject <Dictionary <string, string> >();
            }
            Assert.DoesNotContain(new KeyValuePair <string, string>("k1", "v1"), cookies);
        }