Skip to content

Commit acae9a1

Browse files
committed
Fix unmarshalling DNS test result resolve output
1 parent 29371df commit acae9a1

3 files changed

Lines changed: 50 additions & 3 deletions

File tree

perfops/client.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,16 @@ func (c *Client) do(req *http.Request, v interface{}) error {
147147
}
148148
return &clientError{Code: resp.StatusCode, Text: string(b)}
149149
}
150+
if false {
151+
b, err := ioutil.ReadAll(resp.Body)
152+
if err != nil {
153+
return err
154+
}
155+
fmt.Println(string(b))
156+
r := bytes.NewReader(b)
157+
d := json.NewDecoder(r)
158+
return d.Decode(&v)
159+
}
150160
d := json.NewDecoder(resp.Body)
151161
return d.Decode(&v)
152162
}

perfops/run.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -381,11 +381,15 @@ func (r *DNSTestResult) PerfOutput() string {
381381

382382
// ResolveOutput returns the unmarshalled output for DNS resolve requests.
383383
func (r *DNSTestResult) ResolveOutput() []string {
384-
var o string
385-
if err := json.Unmarshal(r.Output, &o); err != nil {
384+
var o []string
385+
if err := json.Unmarshal(r.Output, &o); err == nil {
386+
return o
387+
}
388+
var o2 string
389+
if err := json.Unmarshal(r.Output, &o2); err != nil {
386390
return []string{"-"}
387391
}
388-
return strings.Split(o, "\n")
392+
return strings.Split(o2, "\n")
389393
}
390394

391395
// isValidTarget checks if a string is a valid target, i.e., a public

perfops/run_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,39 @@ func TestDNSResolveOutput(t *testing.T) {
386386
}
387387
}
388388

389+
func TestDNSTestResult_ResolveOutput(t *testing.T) {
390+
testCases := map[string]struct {
391+
data []byte
392+
output []string
393+
}{
394+
"String": {[]byte(`{"dnsServer": "8.8.8.8","output": "204.79.197.200\n13.107.21.200","node": {"id": 5,"latitude": 50.110781326572834,"longitude": 8.68984222412098,"country": {"id": 116,"name": "Germany","continent": {"id": 3,"name": "Europe","iso": "EU"},"iso": "DE","iso_numeric": "276"},"city": "Frankfurt","sub_region": "Western Europe"}}`), []string{"204.79.197.200", "13.107.21.200"}},
395+
"Array": {[]byte(`{"dnsServer":"8.8.8.8","output":["104.16.85.20","104.16.87.20","104.16.88.20","104.16.86.20","104.16.89.20"],"node":{"id":127,"latitude":34.021790882354999,"longitude":-118.20224761963,"country":{"id":168,"name":"United States","continent":{"id":4,"name":"North America","iso":"NA"},"iso":"US","iso_numeric":"840","is_eu":false},"city":"Los Angeles","sub_region":"Northern America"},"finished":true,"time":1519397739.7899001}`), []string{"104.16.85.20", "104.16.87.20", "104.16.88.20", "104.16.86.20", "104.16.89.20"}},
396+
}
397+
eq := func(a, b []string) bool {
398+
if len(a) != len(b) {
399+
return false
400+
}
401+
for i := range a {
402+
if a[i] != b[i] {
403+
return false
404+
}
405+
}
406+
return true
407+
}
408+
for name, tc := range testCases {
409+
t.Run(name, func(t *testing.T) {
410+
var r *DNSTestResult
411+
if err := json.Unmarshal(tc.data, &r); err != nil {
412+
t.Fatalf("unexpected error %v", err)
413+
}
414+
exp, got := tc.output, r.ResolveOutput()
415+
if !eq(exp, got) {
416+
t.Fatalf("expected %v; got %v", exp, got)
417+
}
418+
})
419+
}
420+
}
421+
389422
func TestCurl(t *testing.T) {
390423
reqTestCases := map[string]struct {
391424
curlReq CurlRequest

0 commit comments

Comments
 (0)