Ejemplo n.º 1
0
        public async Task TestQuery()
        {
            using var tokenSource = new CancellationTokenSource(TimeSpan.FromMilliseconds(500));

            var endpoint = GenerateEndPoint();

            // Create a real upstream DNS client to serve the request (todo: mock this)
            using var upstream = new DnsUdpClient(IPAddress.Parse("1.1.1.1"));

            // Create a loopback server
            using var server = new DnsHttpServer(upstream, x => x.ConfigureKestrel(y => y.Listen(endpoint)));

            // Start recieving
            using var receiveTask = server.Listen(tokenSource.Token);

            // Create a loopback DNS client to talk to the server
            using var client = new DnsHttpClient(new HttpClient { BaseAddress = new Uri($"http://{endpoint.Address}:{endpoint.Port}") });

            try
            {
                var query = DnsQueryFactory.CreateQuery("google.com");

                // Send a DNS request to the server, verify the results
                var response = await client.Query(query, tokenSource.Token);

                Assert.Equal(query.Id, response.Header.Id);
                Assert.Equal(query.Host, response.Header.Host);
                Assert.True(response.Answers.Count > 0);
            }
            finally
            {
                tokenSource.Cancel();
                await receiveTask;
            }
        }
Ejemplo n.º 2
0
        public async Task TestStartupShutdown()
        {
            using var tokenSource = new CancellationTokenSource(TimeSpan.FromMilliseconds(500));

            using var server = new DnsHttpServer(new DnsUdpClient(IPAddress.Loopback), x => x.ConfigureKestrel(y => y.Listen(GenerateEndPoint())));

            await server.Listen(tokenSource.Token);
        }