-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathrate_limit_test.go
120 lines (107 loc) · 2.35 KB
/
rate_limit_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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package backlog
import (
"fmt"
"net/http"
"reflect"
"testing"
"time"
"github.com/kylelemons/godebug/pretty"
"github.com/pkg/errors"
)
func TestResetAsTime(t *testing.T) {
ls := &LimitStatus{
Reset: Int(1603881873),
}
expected := ls.ResetAsTime().UTC()
want := time.Date(2020, time.October, 28, 10, 44, 33, 0, time.UTC)
if !reflect.DeepEqual(want, expected) {
t.Fatal(errors.New(pretty.Compare(want, expected)))
}
}
func TestResetAsTimeWithResetNull(t *testing.T) {
ls := &LimitStatus{}
expected := ls.ResetAsTime()
want := time.Time{}
if !reflect.DeepEqual(want, expected) {
t.Fatal(errors.New(pretty.Compare(want, expected)))
}
}
const testJSONRateLimit string = `{
"rateLimit": {
"read": {
"limit": 600,
"remaining": 600,
"reset": 1603881873
},
"update": {
"limit": 150,
"remaining": 150,
"reset": 1603881873
},
"search": {
"limit": 150,
"remaining": 150,
"reset": 1603881873
},
"icon": {
"limit": 60,
"remaining": 60,
"reset": 1603881873
}
}
}`
func getTestRateLimit() *ResponseRateLimit {
return &ResponseRateLimit{
RateLimit: &RateLimit{
Read: &LimitStatus{
Limit: Int(600),
Remaining: Int(600),
Reset: Int(1603881873),
},
Update: &LimitStatus{
Limit: Int(150),
Remaining: Int(150),
Reset: Int(1603881873),
},
Search: &LimitStatus{
Limit: Int(150),
Remaining: Int(150),
Reset: Int(1603881873),
},
Icon: &LimitStatus{
Limit: Int(60),
Remaining: Int(60),
Reset: Int(1603881873),
},
},
}
}
func TestGetRateLimit(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/rateLimit", func(w http.ResponseWriter, r *http.Request) {
if _, err := fmt.Fprint(w, testJSONRateLimit); err != nil {
t.Fatal(err)
}
})
expected, err := client.GetRateLimit()
if err != nil {
t.Errorf("Unexpected error: %s", err)
return
}
r := getTestRateLimit()
want := r.RateLimit
if !reflect.DeepEqual(want, expected) {
t.Fatal(ErrIncorrectResponse)
}
}
func TestGetRateLimitFailed(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/rateLimit", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
})
if _, err := client.GetRateLimit(); err == nil {
t.Fatal("expected an error but got none")
}
}