Skip to content

Commit 599ffd4

Browse files
Added unit test cases
1 parent 0702c86 commit 599ffd4

1 file changed

Lines changed: 39 additions & 3 deletions

File tree

internal/commands/util/usercount/github_test.go

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package usercount
22

33
import (
4-
"strings"
5-
"testing"
6-
74
"github.com/checkmarx/ast-cli/internal/commands/util/printer"
85
"github.com/checkmarx/ast-cli/internal/params"
6+
"github.com/checkmarx/ast-cli/internal/wrappers"
97
"github.com/checkmarx/ast-cli/internal/wrappers/mock"
8+
asserts "github.com/stretchr/testify/assert"
109
"gotest.tools/assert"
10+
"net/http"
11+
"strconv"
12+
"strings"
13+
"testing"
14+
"time"
1115
)
1216

1317
func TestGitHubUserCountOrgs(t *testing.T) {
@@ -100,3 +104,35 @@ func TestGitHubUserCountManyOrgs(t *testing.T) {
100104
err := cmd.Execute()
101105
assert.Error(t, err, tooManyOrgs)
102106
}
107+
108+
func TestHandleRateLimit_WaitsAndRetries(t *testing.T) {
109+
resp := &http.Response{
110+
StatusCode: http.StatusForbidden,
111+
Header: http.Header{},
112+
}
113+
resp.Header.Set("X-RateLimit-Remaining", "0")
114+
resetTime := time.Now().Add(50 * time.Second).Unix()
115+
resp.Header.Set("X-RateLimit-Reset", strconv.FormatInt(resetTime, 10))
116+
117+
client := &http.Client{}
118+
req, _ := http.NewRequest(http.MethodGet, "http://example.com", nil)
119+
120+
start := time.Now()
121+
_, err := wrappers.HandleRateLimit(resp, client, req, "http://example.com", "token", map[string]string{})
122+
elapsed := time.Since(start)
123+
124+
asserts.NoError(t, err)
125+
asserts.GreaterOrEqual(t, elapsed, 20*time.Second)
126+
}
127+
128+
func TestHandleRateLimit_NoRateLimit(t *testing.T) {
129+
resp := &http.Response{
130+
StatusCode: http.StatusForbidden,
131+
Header: http.Header{},
132+
}
133+
client := &http.Client{}
134+
req, _ := http.NewRequest(http.MethodGet, "http://example.com", nil)
135+
outResp, err := wrappers.HandleRateLimit(resp, client, req, "http://example.com", "token", map[string]string{})
136+
asserts.NoError(t, err)
137+
assert.Equal(t, resp, outResp)
138+
}

0 commit comments

Comments
 (0)