Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hmage/dnstest
Equivalent of net/http/httptest, but for dns
https://github.com/hmage/dnstest
Last synced: about 1 month ago
JSON representation
Equivalent of net/http/httptest, but for dns
- Host: GitHub
- URL: https://github.com/hmage/dnstest
- Owner: hmage
- License: mit
- Created: 2024-09-14T00:30:52.000Z (4 months ago)
- Default Branch: master
- Last Pushed: 2024-09-14T02:34:35.000Z (4 months ago)
- Last Synced: 2024-10-16T00:33:17.582Z (3 months ago)
- Language: Go
- Size: 3.91 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![](https://godoc.org/github.com/hmage/dnstest?status.svg)](https://godoc.org/github.com/hmage/dnstest)
# dnstest
This is a spiritual equivalent of Go's `net/http/httptest` package - a small server that you can boot up during unit tests when you need to talk to an outside DNS server but want everything to be local.
## Example
Here's how you can use `dnstest` in your tests:
```go
package mainimport (
"fmt""github.com/hmage/dnstest"
"github.com/miekg/dns"
)func main() {
ts := dnstest.NewServerBind("example.com. 104 A 127.0.0.1\nexample.com. 104 MX 10 mail.example.com.")
defer ts.Close()q := dns.Msg{}
q.SetQuestion("example.com.", dns.TypeA)
resp, err := dns.Exchange(&q, ts.Addr())
if err != nil {
panic(err)
}fmt.Println(resp)
}
```Output:
```
;; opcode: QUERY, status: NOERROR, id: 15858
;; flags: qr rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0;; QUESTION SECTION:
;example.com. IN A;; ANSWER SECTION:
example.com. 104 IN A 127.0.0.1
```## Notes
- If you use `NewServerBind()`, it generates a _very_ simple DNS handler, which should be good enough to talk to during unit tests.
- `NewServerBind()` uses linear search when matching records, so don't include millions of records unnecessarily; otherwise, your tests will be slower.
- Only basic record types like A, AAAA, MX, TXT, SPF, NS, SRV, and SOA are supported.## Contributing
Contributions are welcome! Feel free to open issues or submit pull requests.
## License
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.