forked from cloudflare/cloudflare-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
virtualdns_test.go
84 lines (73 loc) · 2.04 KB
/
virtualdns_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package cloudflare
import (
"fmt"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func float64Ptr(v float64) *float64 {
return &v
}
func int64Ptr(v int64) *int64 {
return &v
}
func TestVirtualDNSUserAnalytics(t *testing.T) {
setup()
defer teardown()
since := time.Now().Add(-1 * time.Hour)
until := time.Now()
handler := func(w http.ResponseWriter, r *http.Request) {
expectedMetrics := "queryCount,uncachedCount,staleCount,responseTimeAvg,responseTimeMedia,responseTime90th,responseTime99th"
assert.Equal(t, r.Method, "GET", "Expected method 'GET'")
assert.Equal(t, expectedMetrics, r.URL.Query().Get("metrics"), "Expected many metrics in URL parameter")
assert.Equal(t, since.Format(time.RFC3339), r.URL.Query().Get("since"), "Expected since parameter in URL")
assert.Equal(t, until.Format(time.RFC3339), r.URL.Query().Get("until"), "Expected until parameter in URL")
w.Header().Set("content-type", "application/json")
fmt.Fprint(w, `{
"result": {
"totals":{
"queryCount": 5,
"uncachedCount":6,
"staleCount":7,
"responseTimeAvg":1.0,
"responseTimeMedian":2.0,
"responseTime90th":3.0,
"responseTime99th":4.0
}
},
"success": true,
"errors": null,
"messages": null
}`)
}
mux.HandleFunc("/user/virtual_dns/12345/dns_analytics/report", handler)
want := VirtualDNSAnalytics{
Totals: VirtualDNSAnalyticsMetrics{
QueryCount: int64Ptr(5),
UncachedCount: int64Ptr(6),
StaleCount: int64Ptr(7),
ResponseTimeAvg: float64Ptr(1.0),
ResponseTimeMedian: float64Ptr(2.0),
ResponseTime90th: float64Ptr(3.0),
ResponseTime99th: float64Ptr(4.0),
},
}
params := VirtualDNSUserAnalyticsOptions{
Metrics: []string{
"queryCount",
"uncachedCount",
"staleCount",
"responseTimeAvg",
"responseTimeMedia",
"responseTime90th",
"responseTime99th",
},
Since: &since,
Until: &until,
}
actual, err := client.VirtualDNSUserAnalytics("12345", params)
if assert.NoError(t, err) {
assert.Equal(t, want, actual)
}
}