-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
162 lines (155 loc) · 3.7 KB
/
client_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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//
// client_test.go
// Copyright (C) 2017 weirdgiraffe <[email protected]>
//
// Distributed under terms of the MIT license.
//
package github
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"testing"
"time"
"github.com/weirdgiraffe/github/mock"
)
func TestRequestDefaultHeaders(t *testing.T) {
c := NewClient(nil)
verb := []string{"GET", "PUT", "POST", "DELETE"}
url := "https://api.github.com"
for i := range verb {
req, err := c.NewRequest(verb[i], url)
if err != nil {
t.Fatal("Failed NewRequest(): %v", err)
}
if req.Header.Get("User-Agent") != defaultUserAgent {
t.Errorf(
"Unexpected User-Agent header: %s != %s",
defaultUserAgent,
req.Header.Get("User-Agent"),
)
}
if req.Header.Get("Accept") != defaultAccept {
t.Errorf(
"Unexpected Accept header: %s != %s",
defaultAccept,
req.Header.Get("Accept"),
)
}
}
}
func TestBasicAuth(t *testing.T) {
mockHTTP := &github_mock.HttpClient{
ActualDo: func(r *http.Request) (*http.Response, error) {
if r.Header.Get("Authorization") == "" {
t.Error("BasicAuth not set")
}
return &http.Response{
Header: make(http.Header),
Body: ioutil.NopCloser(bytes.NewBufferString("Hello World")),
}, nil
},
}
client := &BasicAuthClient{
client: mockHTTP,
user: "hello",
pass: "world",
}
c := NewClient(client)
verb := []string{"GET", "PUT", "POST", "DELETE"}
url := "https://api.github.com"
for i := range verb {
req, err := c.NewRequest(verb[i], url)
if err != nil {
t.Fatal("Failed NewRequest(): %v", err)
}
_, _ = c.Do(req)
}
}
func TestRateLimit(t *testing.T) {
res := &http.Response{
Header: make(http.Header),
}
var tc = []struct {
restLimit int
reset time.Time
checkError bool
}{
{100, time.Now().Add(1 * time.Hour), false},
{0, time.Now().Add(1 * time.Hour), true},
}
for i := range tc {
r := RateLimit{}
err := r.Check()
if err != nil {
t.Fatalf("testcase[%2d] New Ratelimit check: %v", i, err)
}
res.Header.Set(
"X-RateLimit-Remaining",
fmt.Sprintf("%d", tc[i].restLimit),
)
res.Header.Set(
"X-RateLimit-Reset",
fmt.Sprintf("%d", tc[i].reset.Unix()),
)
err = r.Update(res)
if err != nil {
t.Fatalf("testcase[%2d] Ratelimit update: %v", i, err)
}
err = r.Check()
if tc[i].checkError {
if err == nil {
t.Errorf("testcase[%2d] No check error, but expected", i)
} else {
if e := err.(*RatelimitError); e == nil {
t.Errorf("testcase[%2d] Unexpected error: %v", i, err)
}
if e := err.(*RatelimitError); e != nil {
if time.Now().Add(e.Timeout).Before(tc[i].reset) {
t.Errorf("testcase[%2d] Unexpected timeout: %v", i, e.Timeout)
}
}
}
} else {
if err != nil {
t.Errorf("testcase[%2d] Check error, but unexpected: %v", i, err)
}
}
if r.RestLimit != tc[i].restLimit {
t.Errorf(
"testcase[%2d] Ratelimit.RestLimit: %d != %d",
i, tc[i].restLimit, r.RestLimit,
)
}
if r.Reset.Unix() != tc[i].reset.Unix() {
t.Errorf(
"testcase[%2d] Ratelimit.Reset %v != %v",
i, tc[i].reset, r.Reset,
)
}
}
}
func TestClientDo(t *testing.T) {
mockHTTP := &github_mock.HttpClient{
ActualDo: func(req *http.Request) (*http.Response, error) {
res := &http.Response{
Header: make(http.Header),
Body: ioutil.NopCloser(bytes.NewBufferString("Hello World")),
}
res.Header.Set("X-RateLimit-Remaining", "1")
res.Header.Set("X-RateLimit-Reset", fmt.Sprintf("%d", time.Now().Add(1*time.Hour).Unix()))
return res, nil
},
}
c := NewClient(mockHTTP)
req, err := c.NewRequest("GET", "https://api.github.com")
if err != nil {
t.Fatalf("NewRequest: %v", err)
}
res, err := c.Do(req)
if err != nil {
t.Fatalf("Do: %v", err)
}
res.Body.Close()
}