|
1
|
|
|
package main |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"encoding/json" |
|
5
|
|
|
"go-github-token-limit/internal/githubapi" |
|
6
|
|
|
"net/http" |
|
7
|
|
|
"net/http/httptest" |
|
8
|
|
|
"testing" |
|
9
|
|
|
"time" |
|
10
|
|
|
) |
|
11
|
|
|
|
|
12
|
|
|
func TestFetchRateLimit(t *testing.T) { |
|
13
|
|
|
// Mock server to simulate GitHub API responses |
|
14
|
|
|
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
|
15
|
|
|
response := githubapi.RateLimitResponse{ |
|
16
|
|
|
Resources: githubapi.RateLimit{ |
|
17
|
|
|
Core: githubapi.Rate{ |
|
18
|
|
|
Limit: 5000, |
|
19
|
|
|
Remaining: 4999, |
|
20
|
|
|
Reset: githubapi.Timestamp{Time: time.Now().Add(30 * time.Minute)}, |
|
21
|
|
|
}, |
|
22
|
|
|
}, |
|
23
|
|
|
} |
|
24
|
|
|
w.Header().Set("Content-Type", "application/json") |
|
25
|
|
|
err := json.NewEncoder(w).Encode(response) |
|
26
|
|
|
if err != nil { |
|
27
|
|
|
t.Fatalf("Failed to encode JSON response: %v", err) |
|
28
|
|
|
} |
|
29
|
|
|
})) |
|
30
|
|
|
defer mockServer.Close() |
|
31
|
|
|
|
|
32
|
|
|
originalAPIURL := githubapi.APIURL |
|
33
|
|
|
githubapi.APIURL = mockServer.URL |
|
34
|
|
|
defer func() { githubapi.APIURL = originalAPIURL }() |
|
35
|
|
|
|
|
36
|
|
|
client := &http.Client{} |
|
37
|
|
|
token := "" |
|
38
|
|
|
|
|
39
|
|
|
rateLimitResponse, err := githubapi.FetchRateLimit(client, token) |
|
40
|
|
|
if err != nil { |
|
41
|
|
|
t.Fatalf("Expected no error, got %v", err) |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
core := rateLimitResponse.Resources.Core |
|
45
|
|
|
if core.Limit != 5000 { |
|
46
|
|
|
t.Errorf("Expected limit to be 5000, got %d", core.Limit) |
|
47
|
|
|
} |
|
48
|
|
|
if core.Remaining != 4999 { |
|
49
|
|
|
t.Errorf("Expected remaining to be 4999, got %d", core.Remaining) |
|
50
|
|
|
} |
|
51
|
|
|
if core.Reset.Time.Before(time.Now()) { |
|
52
|
|
|
t.Errorf("Expected reset time to be in the future, got %s", core.Reset.Time) |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
func TestFetchRateLimitReached(t *testing.T) { |
|
57
|
|
|
// Mock server to simulate GitHub API responses |
|
58
|
|
|
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
|
59
|
|
|
response := githubapi.RateLimitResponse{ |
|
60
|
|
|
Resources: githubapi.RateLimit{ |
|
61
|
|
|
Core: githubapi.Rate{ |
|
62
|
|
|
Limit: 5000, |
|
63
|
|
|
Remaining: 0, |
|
64
|
|
|
Reset: githubapi.Timestamp{Time: time.Now().Add(30 * time.Minute)}, |
|
65
|
|
|
}, |
|
66
|
|
|
}, |
|
67
|
|
|
} |
|
68
|
|
|
w.Header().Set("Content-Type", "application/json") |
|
69
|
|
|
err := json.NewEncoder(w).Encode(response) |
|
70
|
|
|
if err != nil { |
|
71
|
|
|
t.Fatalf("Failed to encode JSON response: %v", err) |
|
72
|
|
|
} |
|
73
|
|
|
})) |
|
74
|
|
|
defer mockServer.Close() |
|
75
|
|
|
|
|
76
|
|
|
originalAPIURL := githubapi.APIURL |
|
77
|
|
|
githubapi.APIURL = mockServer.URL |
|
78
|
|
|
defer func() { githubapi.APIURL = originalAPIURL }() |
|
79
|
|
|
|
|
80
|
|
|
client := &http.Client{} |
|
81
|
|
|
token := "" |
|
82
|
|
|
|
|
83
|
|
|
rateLimitResponse, err := githubapi.FetchRateLimit(client, token) |
|
84
|
|
|
if err != nil { |
|
85
|
|
|
t.Fatalf("Expected no error, got %v", err) |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
core := rateLimitResponse.Resources.Core |
|
89
|
|
|
if core.Limit != 5000 { |
|
90
|
|
|
t.Errorf("Expected limit to be 5000, got %d", core.Limit) |
|
91
|
|
|
} |
|
92
|
|
|
if core.Remaining != 0 { |
|
93
|
|
|
t.Errorf("Expected remaining to be 0, got %d", core.Remaining) |
|
94
|
|
|
} |
|
95
|
|
|
if core.Reset.Time.Before(time.Now()) { |
|
96
|
|
|
t.Errorf("Expected reset time to be in the future, got %s", core.Reset.Time) |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
func TestNoApiKeyRateLimit(t *testing.T) { |
|
101
|
|
|
// Mock server to simulate GitHub API responses |
|
102
|
|
|
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
|
103
|
|
|
response := githubapi.RateLimitResponse{ |
|
104
|
|
|
Resources: githubapi.RateLimit{ |
|
105
|
|
|
Core: githubapi.Rate{ |
|
106
|
|
|
Limit: 60, |
|
107
|
|
|
Remaining: 59, |
|
108
|
|
|
Reset: githubapi.Timestamp{Time: time.Now().Add(30 * time.Minute)}, |
|
109
|
|
|
}, |
|
110
|
|
|
}, |
|
111
|
|
|
} |
|
112
|
|
|
w.Header().Set("Content-Type", "application/json") |
|
113
|
|
|
err := json.NewEncoder(w).Encode(response) |
|
114
|
|
|
if err != nil { |
|
115
|
|
|
t.Fatalf("Failed to encode JSON response: %v", err) |
|
116
|
|
|
} |
|
117
|
|
|
})) |
|
118
|
|
|
defer mockServer.Close() |
|
119
|
|
|
|
|
120
|
|
|
originalAPIURL := githubapi.APIURL |
|
121
|
|
|
githubapi.APIURL = mockServer.URL |
|
122
|
|
|
defer func() { githubapi.APIURL = originalAPIURL }() |
|
123
|
|
|
|
|
124
|
|
|
client := &http.Client{} |
|
125
|
|
|
token := "" |
|
126
|
|
|
|
|
127
|
|
|
rateLimitResponse, err := githubapi.FetchRateLimit(client, token) |
|
128
|
|
|
if err != nil { |
|
129
|
|
|
t.Fatalf("Expected no error, got %v", err) |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
core := rateLimitResponse.Resources.Core |
|
133
|
|
|
if core.Limit != 60 { |
|
134
|
|
|
t.Errorf("Expected limit to be 60, got %d", core.Limit) |
|
135
|
|
|
} |
|
136
|
|
|
if core.Remaining != 59 { |
|
137
|
|
|
t.Errorf("Expected remaining to be 59, got %d", core.Remaining) |
|
138
|
|
|
} |
|
139
|
|
|
if core.Reset.Time.Before(time.Now()) { |
|
140
|
|
|
t.Errorf("Expected reset time to be in the future, got %s", core.Reset.Time) |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|