|
1 | 1 | package usercount |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "strings" |
5 | | - "testing" |
6 | | - |
7 | 4 | "github.com/checkmarx/ast-cli/internal/commands/util/printer" |
8 | 5 | "github.com/checkmarx/ast-cli/internal/params" |
| 6 | + "github.com/checkmarx/ast-cli/internal/wrappers" |
9 | 7 | "github.com/checkmarx/ast-cli/internal/wrappers/mock" |
| 8 | + asserts "github.com/stretchr/testify/assert" |
10 | 9 | "gotest.tools/assert" |
| 10 | + "net/http" |
| 11 | + "strconv" |
| 12 | + "strings" |
| 13 | + "testing" |
| 14 | + "time" |
11 | 15 | ) |
12 | 16 |
|
13 | 17 | func TestGitHubUserCountOrgs(t *testing.T) { |
@@ -100,3 +104,35 @@ func TestGitHubUserCountManyOrgs(t *testing.T) { |
100 | 104 | err := cmd.Execute() |
101 | 105 | assert.Error(t, err, tooManyOrgs) |
102 | 106 | } |
| 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