-
Notifications
You must be signed in to change notification settings - Fork 10
/
request_test.go
114 lines (96 loc) · 2.78 KB
/
request_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
package onedrive
import (
"io/ioutil"
"net/http"
"testing"
"time"
)
func TestCalculateThrottle(t *testing.T) {
time1, _ := time.Parse("02 Jan 06 15:04 MST", "01 Jan 15 15:04 GMT")
time1e, _ := time.Parse("02 Jan 06 15:04 MST", "01 Jan 15 16:04 GMT")
time2, _ := time.Parse("02 Jan 06 15:04 MST", "01 Jan 15 15:04 GMT")
time2e, _ := time.Parse("02 Jan 06 15:04 MST", "01 Jan 15 15:05 GMT")
tt := []struct {
currentTime time.Time
expectedTime time.Time
retryAfter string
}{
{time1, time1e, "3600"},
{time2, time2e, "60"},
}
for i, tst := range tt {
tm, err := calculateThrottle(tst.currentTime, tst.retryAfter)
if err != nil {
t.Fatalf("[%d] Couldn't calculate retry after: %s", i, err.Error())
}
if got, want := tm, tst.expectedTime; !got.Equal(want) {
t.Fatalf("[%d] Got %s Expected %s", i, got, want)
}
}
}
func TestNewRequest(t *testing.T) {
setup()
defer teardown()
tt := []struct {
method string
uri string
requestHeaders map[string]string
debug bool
body interface{}
}{
{"GET", "/foo", map[string]string{"Content-Type": "text/plain"}, false, nil},
{"POST", "/foo/two", nil, true, ""},
{"DELETE", "/foo/two", nil, true, Item{ID: "hello world"}},
}
for i, tst := range tt {
oneDrive.Debug = tst.debug
req, err := oneDrive.newRequest(tst.method, tst.uri, tst.requestHeaders, tst.body)
if err != nil {
t.Fatal(err)
}
if tst.debug {
if got, want := req.Header.Get("Accept"), "application/json;format=pretty"; got != want {
t.Fatalf("[%d] Got %q Expected %q", i, got, want)
}
} else {
if got, want := req.Header.Get("Accept"), "application/json"; got != want {
t.Fatalf("[%d] Got %q Expected %q", i, got, want)
}
}
if err != nil {
t.Fatalf("[%d] %q", i, err.Error())
}
if got, want := req.Method, tst.method; got != want {
t.Fatalf("[%d] Got %q Expected %q", i, got, want)
}
if got, want := req.URL.String(), oneDrive.BaseURL+tst.uri; got != want {
t.Fatalf("[%d] Got %q Expected %q", i, got, want)
}
for k, v := range tst.requestHeaders {
if got, want := req.Header.Get(k), v; got != want {
t.Fatalf("[%d] Got %q Expected %q", i, got, want)
}
}
}
}
func TestThrottledRequest(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/drive", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Retry-After", "3600")
w.WriteHeader(statusTooManyRequests)
b, err := ioutil.ReadFile("fixtures/request.invalid.tooManyRequests.json")
if err != nil {
panic(err)
}
w.Write(b)
})
_, _, err := oneDrive.Drives.GetDefault()
if err == nil {
t.Fatal("Expected tooManyRequests error but none occured")
}
drive, _, err := oneDrive.Drives.GetDefault()
if drive != nil {
t.Fatalf("Expected no drive to be returned, got %v", *drive)
}
}