dns-mock-server

Crates.iodns-mock-server
lib.rsdns-mock-server
version0.1.5
sourcesrc
created_at2023-11-15 21:10:27.18947
updated_at2024-07-09 17:16:55.098688
descriptionMock DNS server for tests
homepage
repositoryhttps://github.com/alexander-jackson/dns-mock-server
max_upload_size
id1036832
size9,774
Alexander Jackson (alexander-jackson)

documentation

README

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(())
}
Commit count: 19

cargo fmt