public static SecureHttpClientHandler Build() { var logger = new LoggerFactory().AddSerilog().CreateLogger(nameof(SecureHttpClientHandler)); var secureHttpClientHandler = new SecureHttpClientHandler(logger); return(secureHttpClientHandler); }
public async Task CertificatePinnerTest_OneHost_Failure() { var secureHttpClientHandler = new SecureHttpClientHandler(); secureHttpClientHandler.AddCertificatePinner(Hostname, PinsKo); await AssertCertificatePinnerFailureAsync(Page, secureHttpClientHandler).ConfigureAwait(false); }
public async Task CertificatePinnerTest_TwoHosts_SecondHostFails() { var secureHttpClientHandler = new SecureHttpClientHandler(); secureHttpClientHandler.AddCertificatePinner(Hostname, PinsOk); secureHttpClientHandler.AddCertificatePinner(Hostname2, Pins2Ko); await AssertCertificatePinnerSuccessAsync(Page, secureHttpClientHandler).ConfigureAwait(false); await AssertCertificatePinnerFailureAsync(Page2, secureHttpClientHandler).ConfigureAwait(false); }
private static async Task <byte[]> GetBytesAsync(string page) { byte[] result; var secureHttpClientHandler = new SecureHttpClientHandler(); using (var httpClient = new HttpClient(secureHttpClientHandler)) using (var response = await httpClient.GetAsync(page).ConfigureAwait(false)) { result = await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false); } return(result); }
private static async Task <string> GetPageAsync(string page) { string result; var secureHttpClientHandler = new SecureHttpClientHandler(); using (var httpClient = new HttpClient(secureHttpClientHandler)) using (var response = await httpClient.GetAsync(page).ConfigureAwait(false)) { result = await response.Content.ReadAsStringAsync().ConfigureAwait(false); } return(result); }
private static async Task <string> GetPageAsync(string page, string hostname = null, string[] pins = null) { var secureHttpClientHandler = new SecureHttpClientHandler(); if (pins != null) { secureHttpClientHandler.AddCertificatePinner(hostname, pins); } string result; using (var httpClient = new HttpClient(secureHttpClientHandler)) using (var response = await httpClient.GetAsync(page).ConfigureAwait(false)) { result = await response.Content.ReadAsStringAsync().ConfigureAwait(false); } return(result); }
public async Task HttpTest_DeleteCookie() { Dictionary <string, string> cookies; var secureHttpClientHandler = new SecureHttpClientHandler(); 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.Empty(cookies); }
public async Task HttpTest_SetCookieAgain() { Dictionary <string, string> cookies; var secureHttpClientHandler = new SecureHttpClientHandler(); 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/set?k1=v2"; var result = await GetPageAsync(httpClient, page2).ConfigureAwait(false); var json = JToken.Parse(result); cookies = json["cookies"].ToObject <Dictionary <string, string> >(); } Assert.Contains(new KeyValuePair <string, string>("k1", "v2"), cookies); }
public static HttpClient CreateHttpClient(string url) { HttpClient client; Uri baseAddress = new Uri(url); var innerHandler = new SecureHttpClientHandler(null); innerHandler.UseProxy = false; innerHandler.AddCertificatePinner(baseAddress.Host, CertificatePins); #if DEBUG var verbosity = HttpMessageParts.RequestBody | HttpMessageParts.RequestHeaders | HttpMessageParts.ResponseBody; client = new HttpClient(new HttpTracerHandler(innerHandler, verbosity)); #else client = new HttpClient(innerHandler); #endif client.BaseAddress = baseAddress; client.Timeout = TimeSpan.FromSeconds(60); return(client); }
public SecureHttpClientFactory(SecureHttpClientHandler secureHttpClientHandler) { _secureHttpClientHandler = secureHttpClientHandler; }
protected TestBase(TestFixture fixture) { _secureHttpClientHandler = fixture.ServiceProvider.GetRequiredService <HttpClientHandler>() as SecureHttpClientHandler; _httpClient = new HttpClient(_secureHttpClientHandler); }