vendredi 30 juin 2017

How to mock a DNS request? (miekg/dns)

I have this small piece of code to explain the codebase I'm trying to test. I've skipped checking errors to make the question short.

func lastCNAME(domain string) (lastCNAME string) {
        ns := "8.8.8.8:53"

        c := dns.Client{}
        m := dns.Msg{}
        m.SetQuestion(domain, dns.TypeA)
        r, _, _ := c.Exchange(&m, ns)
        // Last CNAME
        for _, ans := range r.Answer {
                cname, ok := ans.(*dns.CNAME)
                if ok {
                        lastCNAME = cname.Target
                }
        }
        return lastCNAME
}

What is the best way to mock the dns query to the nameserver 8.8.8.8?

Here is the full code in case anyone's curious.

Aucun commentaire:

Enregistrer un commentaire