Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/foxcpp/go-mockdns
Boilerplate for testing of code involving DNS lookups, including unholy hacks to redirect net.Lookup* calls.
https://github.com/foxcpp/go-mockdns
dns golang testing
Last synced: 9 days ago
JSON representation
Boilerplate for testing of code involving DNS lookups, including unholy hacks to redirect net.Lookup* calls.
- Host: GitHub
- URL: https://github.com/foxcpp/go-mockdns
- Owner: foxcpp
- License: mit
- Created: 2019-11-03T21:29:10.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2024-06-12T22:46:07.000Z (5 months ago)
- Last Synced: 2024-10-15T21:07:23.122Z (23 days ago)
- Topics: dns, golang, testing
- Language: Go
- Homepage:
- Size: 18.6 KB
- Stars: 42
- Watchers: 3
- Forks: 10
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
go-mockdns
============[![Reference](https://godoc.org/github.com/foxcpp/go-mockdns?status.svg)](https://godoc.org/github.com/foxcpp/go-mockdns)
Boilerplate for testing of code involving DNS lookups, including ~~unholy~~
hacks to redirect `net.Lookup*` calls.Example
---------Trivial mock resolver, for cases where tested code supports custom resolvers:
```go
r := mockdns.Resolver{
Zones: map[string]mockdns.Zone{
"example.org.": {
A: []string{"1.2.3.4"},
},
},
}addrs, err := r.LookupHost(context.Background(), "example.org")
fmt.Println(addrs, err)// Output:
// [1.2.3.4]
```~~Unholy~~ hack for cases where it doesn't:
```go
srv, _ := mockdns.NewServer(map[string]mockdns.Zone{
"example.org.": {
A: []string{"1.2.3.4"},
},
}, false)
defer srv.Close()srv.PatchNet(net.DefaultResolver)
defer mockdns.UnpatchNet(net.DefaultResolver)addrs, err := net.LookupHost("example.org")
fmt.Println(addrs, err)// Output:
// [1.2.3.4]
```Note, if you need to replace net.Dial calls and tested code supports custom
net.Dial, patch the resolver object inside it instead of net.DefaultResolver.
If tested code supports Dialer-like objects - use Resolver itself, it
implements Dial and DialContext methods.