Increase the code coverage

This commit is contained in:
cemturker 2020-11-18 23:07:38 +01:00
parent cef94299e5
commit 1901dfcd81

View File

@ -11,12 +11,13 @@ func TestIntegrationQuery(t *testing.T) {
inputURL string inputURL string
expectedDNSCode string expectedDNSCode string
expectedBody string expectedBody string
isErrExpected bool
}{ }{
{ {
name: "test DNS with type A", name: "test DNS with type A",
inputDNS: DNS{ inputDNS: DNS{
QueryType: "A", QueryType: "A",
QueryName: "example.com", QueryName: "example.com.",
}, },
inputURL: "8.8.8.8", inputURL: "8.8.8.8",
expectedDNSCode: "NOERROR", expectedDNSCode: "NOERROR",
@ -26,7 +27,7 @@ func TestIntegrationQuery(t *testing.T) {
name: "test DNS with type AAAA", name: "test DNS with type AAAA",
inputDNS: DNS{ inputDNS: DNS{
QueryType: "AAAA", QueryType: "AAAA",
QueryName: "example.com", QueryName: "example.com.",
}, },
inputURL: "8.8.8.8", inputURL: "8.8.8.8",
expectedDNSCode: "NOERROR", expectedDNSCode: "NOERROR",
@ -36,17 +37,17 @@ func TestIntegrationQuery(t *testing.T) {
name: "test DNS with type CNAME", name: "test DNS with type CNAME",
inputDNS: DNS{ inputDNS: DNS{
QueryType: "CNAME", QueryType: "CNAME",
QueryName: "example.com", QueryName: "doc.google.com.",
}, },
inputURL: "8.8.8.8", inputURL: "8.8.8.8",
expectedDNSCode: "NOERROR", expectedDNSCode: "NOERROR",
expectedBody: "", expectedBody: "writely.l.google.com.",
}, },
{ {
name: "test DNS with type MX", name: "test DNS with type MX",
inputDNS: DNS{ inputDNS: DNS{
QueryType: "MX", QueryType: "MX",
QueryName: "example.com", QueryName: "example.com.",
}, },
inputURL: "8.8.8.8", inputURL: "8.8.8.8",
expectedDNSCode: "NOERROR", expectedDNSCode: "NOERROR",
@ -56,12 +57,21 @@ func TestIntegrationQuery(t *testing.T) {
name: "test DNS with type NS", name: "test DNS with type NS",
inputDNS: DNS{ inputDNS: DNS{
QueryType: "NS", QueryType: "NS",
QueryName: "example.com", QueryName: "example.com.",
}, },
inputURL: "8.8.8.8", inputURL: "8.8.8.8",
expectedDNSCode: "NOERROR", expectedDNSCode: "NOERROR",
expectedBody: "b.iana-servers.net.", expectedBody: "b.iana-servers.net.",
}, },
{
name: "test DNS with fake type and retrieve error",
inputDNS: DNS{
QueryType: "B",
QueryName: "google",
},
inputURL: "8.8.8.8",
isErrExpected: true,
},
} }
for _, test := range tests { for _, test := range tests {
@ -69,10 +79,9 @@ func TestIntegrationQuery(t *testing.T) {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
dns := test.inputDNS dns := test.inputDNS
result := &Result{} result := &Result{}
dns.validateAndSetDefault()
dns.query(test.inputURL, result) dns.query(test.inputURL, result)
if len(result.Errors) != 0 { if test.isErrExpected && len(result.Errors) == 0 {
t.Errorf("there should be no error Errors:%v", result.Errors) t.Errorf("there should be errors")
} }
if result.DNSRCode != test.expectedDNSCode { if result.DNSRCode != test.expectedDNSCode {
t.Errorf("DNSRCodePlaceHolder '%s' should have been %s", result.DNSRCode, test.expectedDNSCode) t.Errorf("DNSRCodePlaceHolder '%s' should have been %s", result.DNSRCode, test.expectedDNSCode)