| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | package forward
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"testing"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin/pkg/dnstest"
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin/test"
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/request"
 | 
					
						
							| 
									
										
										
										
											2018-02-15 10:21:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestLookup(t *testing.T) {
 | 
					
						
							|  |  |  | 	s := dnstest.NewServer(func(w dns.ResponseWriter, r *dns.Msg) {
 | 
					
						
							|  |  |  | 		ret := new(dns.Msg)
 | 
					
						
							|  |  |  | 		ret.SetReply(r)
 | 
					
						
							|  |  |  | 		ret.Answer = append(ret.Answer, test.A("example.org. IN A 127.0.0.1"))
 | 
					
						
							|  |  |  | 		w.WriteMsg(ret)
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | 	defer s.Close()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-09 15:14:55 +01:00
										 |  |  | 	p := NewProxy(s.Addr, DNS)
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 	f := New()
 | 
					
						
							|  |  |  | 	f.SetProxy(p)
 | 
					
						
							|  |  |  | 	defer f.Close()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	state := request.Request{W: &test.ResponseWriter{}, Req: new(dns.Msg)}
 | 
					
						
							|  |  |  | 	resp, err := f.Lookup(state, "example.org.", dns.TypeA)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		t.Fatal("Expected to receive reply, but didn't")
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	// expect answer section with A record in it
 | 
					
						
							|  |  |  | 	if len(resp.Answer) == 0 {
 | 
					
						
							|  |  |  | 		t.Fatalf("Expected to at least one RR in the answer section, got none: %s", resp)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	if resp.Answer[0].Header().Rrtype != dns.TypeA {
 | 
					
						
							|  |  |  | 		t.Errorf("Expected RR to A, got: %d", resp.Answer[0].Header().Rrtype)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	if resp.Answer[0].(*dns.A).A.String() != "127.0.0.1" {
 | 
					
						
							|  |  |  | 		t.Errorf("Expected 127.0.0.1, got: %s", resp.Answer[0].(*dns.A).A.String())
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 |