Skip to content

Commit 92fe455

Browse files
committed
Update dnsproxy_test.go
1 parent b660579 commit 92fe455

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

dnsproxy_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"net/http"
66
"reflect"
77
"testing"
8+
"time"
89

910
"github.com/jarcoal/httpmock"
1011
"github.com/miekg/dns"
@@ -113,3 +114,44 @@ func TestDNSProxy_getResponse(t *testing.T) {
113114
})
114115
}
115116
}
117+
118+
func TestDNSProxy_auditCacheTTL(t *testing.T) {
119+
apiclient := &ApiClient{Client: &http.Client{}, APIURL: agentApiBaseUrl}
120+
121+
httpmock.ActivateNonDefault(apiclient.Client)
122+
123+
httpmock.RegisterResponder("GET", "https://dns.google/resolve?name=domain12345.com.&type=a",
124+
httpmock.ResponderFromMultipleResponses(
125+
[]*http.Response{
126+
httpmock.NewStringResponse(200, `{"Status":0,"TC":false,"RD":true,"RA":true,"AD":false,"CD":false,"Question":[{"name":"domain12345.com.","type":1}],"Answer":[{"name":"domain12345.com.","type":1,"TTL":30,"data":"68.68.68.68"}]}`),
127+
httpmock.NewStringResponse(200, `{"Status":0,"TC":false,"RD":true,"RA":true,"AD":false,"CD":false,"Question":[{"name":"domain12345.com.","type":1}],"Answer":[{"name":"domain12345.com.","type":1,"TTL":30,"data":"68.68.68.68"}]}`),
128+
},
129+
t.Log))
130+
131+
cache := InitCache(EgressPolicyAudit)
132+
133+
proxy := &DNSProxy{
134+
Cache: &cache,
135+
ApiClient: apiclient,
136+
EgressPolicy: EgressPolicyAudit,
137+
}
138+
139+
// should call httpmock
140+
proxy.getResponse(&dns.Msg{Question: []dns.Question{{Name: "domain12345.com.", Qtype: dns.TypeA}}})
141+
142+
time.Sleep(2 * time.Second)
143+
144+
// should get from cache
145+
proxy.getResponse(&dns.Msg{Question: []dns.Question{{Name: "domain12345.com.", Qtype: dns.TypeA}}})
146+
147+
time.Sleep(30 * time.Second)
148+
149+
// should call httpmock
150+
proxy.getResponse(&dns.Msg{Question: []dns.Question{{Name: "domain12345.com.", Qtype: dns.TypeA}}})
151+
152+
info := httpmock.GetCallCountInfo()
153+
count := info["GET https://dns.google/resolve?name=domain12345.com.&type=a"]
154+
if count != 2 {
155+
t.Errorf("incorrect call count %d, expected 2, %v", count, info)
156+
}
157+
}

0 commit comments

Comments
 (0)