6 releases

0.1.5 Jul 9, 2024
0.1.4 May 22, 2024
0.1.3 Mar 9, 2024
0.1.2 Nov 22, 2023

#1943 in Network programming

Download history 188/week @ 2024-09-24 21/week @ 2024-10-01 11/week @ 2024-10-08 42/week @ 2024-10-15 23/week @ 2024-10-22 28/week @ 2024-10-29 6/week @ 2024-11-05 147/week @ 2024-11-12 22/week @ 2024-11-19 186/week @ 2024-11-26 15/week @ 2024-12-03 158/week @ 2024-12-10 37/week @ 2024-12-17 23/week @ 2024-12-31 51/week @ 2025-01-07

116 downloads per month

MIT/Apache

7KB
60 lines

dns-mock-server

Implementation of a DNS mock server for use in tests, based on hickory-server.

Usage

The following example shows the basic usage for the library, where we create a new server, add some records and then spawn it on a background task.

use std::net::{IpAddr, Ipv4Addr, SocketAddrV4};

use dns_mock_server::Server;
use tokio::net::UdpSocket;

type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;

#[tokio::test]
async fn check_something() -> Result<()> {
    let mut server = Server::default();

    let records = vec![IpAddr::V4(Ipv4Addr::LOCALHOST)];
    server.add_records("example.com", records)?

    let addr = SocketAddrV4::new(Ipv4Addr::LOCALHOST, 0);
    let socket = UdpSocket::bind(&addr).await?;
    let local_addr = socket.local_addr()?;

    tokio::spawn(async move {
        server.start(socket).await.unwrap();
    });

    // Point your DNS handling at `local_addr` and make requests

    Ok(())
}

Dependencies

~16–27MB
~485K SLoC