Passed
Push — main ( a0a36f...588928 )
by Clive
03:45
created

main.TestFetchRateLimit   B

Complexity

Conditions 7

Size

Total Lines 38
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 26
nop 1
dl 0
loc 38
rs 7.856
c 0
b 0
f 0
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
		json.NewEncoder(w).Encode(response)
26
	}))
27
	defer mockServer.Close()
28
29
	originalAPIURL := githubapi.APIURL
30
	githubapi.APIURL = mockServer.URL
31
	defer func() { githubapi.APIURL = originalAPIURL }()
32
33
	client := &http.Client{}
34
	token := ""
35
36
	rateLimitResponse, err := githubapi.FetchRateLimit(client, token)
37
	if err != nil {
38
		t.Fatalf("Expected no error, got %v", err)
39
	}
40
41
	core := rateLimitResponse.Resources.Core
42
	if core.Limit != 5000 {
43
		t.Errorf("Expected limit to be 5000, got %d", core.Limit)
44
	}
45
	if core.Remaining != 4999 {
46
		t.Errorf("Expected remaining to be 4999, got %d", core.Remaining)
47
	}
48
	if core.Reset.Time.Before(time.Now()) {
49
		t.Errorf("Expected reset time to be in the future, got %s", core.Reset.Time)
50
	}
51
}
52
53
func TestFetchRateLimitReached(t *testing.T) {
54
	// Mock server to simulate GitHub API responses
55
	mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
56
		response := githubapi.RateLimitResponse{
57
			Resources: githubapi.RateLimit{
58
				Core: githubapi.Rate{
59
					Limit:     5000,
60
					Remaining: 0,
61
					Reset:     githubapi.Timestamp{Time: time.Now().Add(30 * time.Minute)},
62
				},
63
			},
64
		}
65
		w.Header().Set("Content-Type", "application/json")
66
		json.NewEncoder(w).Encode(response)
67
	}))
68
	defer mockServer.Close()
69
70
	originalAPIURL := githubapi.APIURL
71
	githubapi.APIURL = mockServer.URL
72
	defer func() { githubapi.APIURL = originalAPIURL }()
73
74
	client := &http.Client{}
75
	token := ""
76
77
	rateLimitResponse, err := githubapi.FetchRateLimit(client, token)
78
	if err != nil {
79
		t.Fatalf("Expected no error, got %v", err)
80
	}
81
82
	core := rateLimitResponse.Resources.Core
83
	if core.Limit != 5000 {
84
		t.Errorf("Expected limit to be 5000, got %d", core.Limit)
85
	}
86
	if core.Remaining != 0 {
87
		t.Errorf("Expected remaining to be 0, got %d", core.Remaining)
88
	}
89
	if core.Reset.Time.Before(time.Now()) {
90
		t.Errorf("Expected reset time to be in the future, got %s", core.Reset.Time)
91
	}
92
}
93