/// <summary> /// Configure the <see cref="HttpClient"/> with the specified <see cref="HaveIBeenPwnedClientSettings"/> /// </summary> /// <param name="client"> /// The <see cref="HttpClient"/> to setup /// </param> /// <param name="settings"> /// The <see cref="HaveIBeenPwnedClientSettings"/> /// </param> /// <returns> /// The configured <see cref="HttpClient"/> /// </returns> private static HttpClient ConfigureHttpClient(HttpClient client, HaveIBeenPwnedClientSettings settings) { var acceptJsonHeader = new MediaTypeWithQualityHeaderValue("application/json"); client.DefaultRequestHeaders.Accept.Add(acceptJsonHeader); client.DefaultRequestHeaders.Add("User-Agent", settings.ApplicationName); return(client); }
public void Ctor_WithNullValueForApplicationNameInSettingsParam_ThrowsArgumentNullException() { var s = new HaveIBeenPwnedClientSettings() { ApplicationName = null, }; Assert.Throws <ArgumentNullException>(() => new HaveIBeenPwnedClient(s)); }
protected static HaveIBeenPwnedClientSettings CreateSettings() { var result = new HaveIBeenPwnedClientSettings() { ApiKey = "DUMMYKEY", ApplicationName = "AtleX.HaveIBeenPwned.IntegrationTests", }; return(result); }
public HaveIBeenPwnedClientTestsBase() { var settings = new HaveIBeenPwnedClientSettings() { ApiKey = "DUMMYKEY", ApplicationName = "Unit.Tests", RequestPaddingForPwnedPasswordResponses = true, }; this.ClientSettings = settings; }
public void Ctor_WithNullValueForApplicationNameInSettingsParamAndValidHttpClient_ThrowsArgumentNullException() { using (var httpClient = new HttpClient()) { var s = new HaveIBeenPwnedClientSettings() { ApplicationName = null, }; Assert.Throws <ArgumentNullException>(() => new HaveIBeenPwnedClient(s, httpClient)); } }
/// <summary> /// Initializes a new instance of <see cref="HaveIBeenPwnedClient"/> with /// the specified <see cref="HaveIBeenPwnedClientSettings"/> and <see cref="HttpClient"/> /// </summary> /// <param name="settings"> /// The <see cref="HaveIBeenPwnedClientSettings"/> to use /// </param> /// <param name="client"> /// The <see cref="HttpClient"/> to use when communicating with the /// HaveIBeenPwned API /// </param> /// <param name="mustDisposeClient"> /// True when the <see cref="HttpClient"/> was created by this <see /// cref="HaveIBeenPwnedClient"/> and must be disposed; false otherwise /// </param> private HaveIBeenPwnedClient(HaveIBeenPwnedClientSettings settings, HttpClient client, bool mustDisposeClient) { Throw.ArgumentNull.WhenNull(settings, nameof(settings)); Throw.ArgumentNull.WhenNullOrWhiteSpace(settings.ApplicationName, nameof(settings.ApplicationName), $"{nameof(HaveIBeenPwnedClientSettings)}.{nameof(settings.ApplicationName)} cannot be null or empty"); Throw.ArgumentNull.WhenNull(client, nameof(client)); this._clientSettings = settings; this._httpClient = ConfigureHttpClient(client, settings); this._enableClientDisposing = mustDisposeClient; }
public async Task GetPastesAsync_WithCancellationTokenAndInvalidApiKey_ThrowsInvalidApiKeyException() { var settings = new HaveIBeenPwnedClientSettings() { ApiKey = "DUMMYAPIKEY", }; using var cancellationTokenSource = new CancellationTokenSource(); using var httpClient = new HttpClient(); using var c = new HaveIBeenPwnedClient(settings, httpClient); await Assert.ThrowsAsync <InvalidApiKeyException>(() => c.GetPastesAsync("*****@*****.**", cancellationTokenSource.Token)); }
public void GlobalSetup() { var mockMessageHandler = new MockHttpMessageHandler(); var testHttpClient = new HttpClient(mockMessageHandler); var settings = new HaveIBeenPwnedClientSettings() { ApiKey = "DUMMYKEY", ApplicationName = "Unit.Tests", }; this._client = new HaveIBeenPwnedClient(settings, testHttpClient); }
/// <summary> /// Initializes a new instance of <see cref="HaveIBeenPwnedClient"/> with the /// specified <see cref="HaveIBeenPwnedClientSettings"/> and <see cref="HttpClient"/> /// </summary> /// <param name="settings"> /// The <see cref="HaveIBeenPwnedClientSettings"/> to use /// </param> /// <param name="client"> /// The <see cref="HttpClient"/> to use when communicating with the /// HaveIBeenPwned API /// </param> public HaveIBeenPwnedClient(HaveIBeenPwnedClientSettings settings, HttpClient client) : this(settings, client, mustDisposeClient : false) { }
/// <summary> /// Initializes a new instance of <see cref="HaveIBeenPwnedClient"/> with the /// specified <see cref="HaveIBeenPwnedClientSettings"/> and <see cref="HttpClient"/> /// </summary> /// <param name="settings"> /// The <see cref="HaveIBeenPwnedClientSettings"/> to use /// </param> public HaveIBeenPwnedClient(HaveIBeenPwnedClientSettings settings) : this(settings, new HttpClient(), mustDisposeClient : true) { }