fakedns

package
v1.35.0-beta.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 19, 2025 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server is a fake DNS server that can be used to override DNS resolution in tests. This is particularly useful for testing components that rely on DNS without needing to set up an actual DNS server. The server can be configured with a map of domain names to IP addresses, and it will respond to DNS queries accordingly. To use the fake DNS server, create an instance using NewServer, then call Hijack to redirect DNS queries to the fake server. This helper does not support running tests in parallel.

func NewServer

func NewServer(hosts map[string]string) (*Server, error)

NewServer creates a new fake DNS server. The hosts map should contain domain names as keys and their corresponding IP addresses as string values. It returns an error if any of the IP addresses are invalid.

func (*Server) Hijack

func (s *Server) Hijack(t *testing.T)

Hijack takes control of the net.DefaultResolver to redirect DNS queries to the fake server. It uses t.Cleanup to automatically restore the original resolver after the test. This helper does not support running tests in parallel.

Example:

dnsServer, _ := fakedns.NewServer(hosts)
dnsServer.Hijack(t)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL