public async Task Resolve_ShouldCall_DnsClientResolve_WithHostParameter() { // Arrange const string host = "hostname"; const string ip = "125.247.41.7"; _ = _dnsClient.Resolve(host, Arg.Any <CancellationToken>()).Returns(ip); // Act var result = await DnsClientExtensions.Resolve(_dnsClient, host, TimeSpan.Zero); // Assert result.Should().Be(ip); }
public async Task Resolve_ShouldBe_OriginResolve() { // Arrange const string ip = "135.11.47.210"; _dnsClient.Resolve("", Arg.Any <CancellationToken>()).ReturnsForAnyArgs(ip); var client = new SafeDnsClient(_dnsClient); // Act var result = await client.Resolve("host", CancellationToken.None); // Assert result.Should().Be(ip); }
public async Task <string> Resolve(string host, CancellationToken token) { try { return(await _origin.Resolve(host, token)); } catch (Exception e) when(e is SocketException || e is DnsResponseException || e is OperationCanceledException) { return(null); } }
public static async Task <string> Resolve(this IDnsClient dnsClient, string host, TimeSpan timeout) { var cts = new CancellationTokenSource(); try { return(await dnsClient.Resolve(host, cts.Token).TimeoutAfter(timeout)); } catch (TimeoutException) { cts.Cancel(); return(string.Empty); } }
private async Task <HttpRequestMessage> ModifyRequest(HttpRequestMessage message, CancellationToken token) { var ip = await _dnsClient.Resolve(message.RequestUri.Host, token); if (!string.IsNullOrEmpty(ip)) { var uriBuilder = new UriBuilder(message.RequestUri) { Host = ip }; message.Headers.Host = message.RequestUri.Host; message.RequestUri = uriBuilder.Uri; } return(message); }