|
| 1 | +package selfupdate |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "strconv" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +func resp(status int, hdr map[string]string) *http.Response { |
| 12 | + h := http.Header{} |
| 13 | + for k, v := range hdr { |
| 14 | + h.Set(k, v) |
| 15 | + } |
| 16 | + return &http.Response{StatusCode: status, Status: strconv.Itoa(status) + " " + http.StatusText(status), Header: h} |
| 17 | +} |
| 18 | + |
| 19 | +// TestRateLimitedForbiddenIsNotReportedAsPermissions is the regression this file |
| 20 | +// exists for. A user on a PUBLIC repo exhausted the anonymous 60/hour quota and |
| 21 | +// got "set GITHUB_TOKEN if the repo is private", which describes a problem that |
| 22 | +// did not exist and hid the one that did. |
| 23 | +func TestRateLimitedForbiddenIsNotReportedAsPermissions(t *testing.T) { |
| 24 | + // Exactly the headers GitHub returned in that session. |
| 25 | + r := resp(http.StatusForbidden, map[string]string{ |
| 26 | + "X-RateLimit-Limit": "60", |
| 27 | + "X-RateLimit-Remaining": "0", |
| 28 | + "X-RateLimit-Reset": strconv.FormatInt(time.Now().Add(10*time.Minute).Unix(), 10), |
| 29 | + }) |
| 30 | + err := apiError("https://api.github.com/repos/o/r/releases", r) |
| 31 | + msg := err.Error() |
| 32 | + |
| 33 | + if !strings.Contains(msg, "rate limit exceeded") { |
| 34 | + t.Errorf("the message must name the real cause, got: %s", msg) |
| 35 | + } |
| 36 | + if strings.Contains(msg, "if the repository is private") || strings.Contains(msg, "if the repo is private") { |
| 37 | + t.Errorf("a rate-limited response must NOT be reported as a permissions problem, got: %s", msg) |
| 38 | + } |
| 39 | + if !strings.Contains(msg, "60 requests/hour") { |
| 40 | + t.Errorf("the message should quote the limit that was hit, got: %s", msg) |
| 41 | + } |
| 42 | + if !strings.Contains(msg, "GITHUB_TOKEN") { |
| 43 | + t.Errorf("the message should still offer the fix, got: %s", msg) |
| 44 | + } |
| 45 | + if !strings.Contains(msg, "resets in") { |
| 46 | + t.Errorf("the message should say when the window rolls over, got: %s", msg) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func TestSecondaryRateLimitViaRetryAfter(t *testing.T) { |
| 51 | + r := resp(http.StatusTooManyRequests, map[string]string{"Retry-After": "45"}) |
| 52 | + msg := apiError("https://api.github.com/x", r).Error() |
| 53 | + if !strings.Contains(msg, "rate limit exceeded") { |
| 54 | + t.Errorf("429 with Retry-After should read as rate limiting, got: %s", msg) |
| 55 | + } |
| 56 | + if !strings.Contains(msg, "retry in 45s") { |
| 57 | + t.Errorf("should surface Retry-After, got: %s", msg) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +// TestForbiddenWithQuotaLeftIsNotRateLimit: a 403 that still has quota is a real |
| 62 | +// permissions failure and must keep the private-repo hint. |
| 63 | +func TestForbiddenWithQuotaLeftIsNotRateLimit(t *testing.T) { |
| 64 | + r := resp(http.StatusForbidden, map[string]string{ |
| 65 | + "X-RateLimit-Limit": "5000", |
| 66 | + "X-RateLimit-Remaining": "4999", |
| 67 | + }) |
| 68 | + msg := apiError("https://api.github.com/x", r).Error() |
| 69 | + if strings.Contains(msg, "rate limit exceeded") { |
| 70 | + t.Errorf("403 with quota remaining is not rate limiting, got: %s", msg) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func TestNotFoundKeepsThePrivateRepoHint(t *testing.T) { |
| 75 | + msg := apiError("https://api.github.com/x", resp(http.StatusNotFound, nil)).Error() |
| 76 | + if !strings.Contains(msg, "private") { |
| 77 | + t.Errorf("404 is where the private-repo hint belongs, got: %s", msg) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func TestUnauthorizedBlamesTheToken(t *testing.T) { |
| 82 | + msg := apiError("https://api.github.com/x", resp(http.StatusUnauthorized, nil)).Error() |
| 83 | + if !strings.Contains(msg, "rejected") { |
| 84 | + t.Errorf("401 means the token is bad, not that the repo is private, got: %s", msg) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +// TestResetHintNeverShowsAStaleOrNegativeDuration: a reset stamp in the past |
| 89 | +// would otherwise render as "resets in -3m0s". |
| 90 | +func TestResetHintNeverShowsAStaleOrNegativeDuration(t *testing.T) { |
| 91 | + r := resp(http.StatusForbidden, map[string]string{ |
| 92 | + "X-RateLimit-Remaining": "0", |
| 93 | + "X-RateLimit-Reset": strconv.FormatInt(time.Now().Add(-3*time.Minute).Unix(), 10), |
| 94 | + }) |
| 95 | + msg := apiError("https://api.github.com/x", r).Error() |
| 96 | + if strings.Contains(msg, "resets in -") || strings.Contains(msg, "resets in 0s") { |
| 97 | + t.Errorf("a past reset stamp must be omitted, got: %s", msg) |
| 98 | + } |
| 99 | + if !strings.Contains(msg, "rate limit exceeded") { |
| 100 | + t.Errorf("still a rate limit, got: %s", msg) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func TestMalformedResetHeaderIsIgnored(t *testing.T) { |
| 105 | + r := resp(http.StatusForbidden, map[string]string{ |
| 106 | + "X-RateLimit-Remaining": "0", |
| 107 | + "X-RateLimit-Reset": "not-a-number", |
| 108 | + }) |
| 109 | + msg := apiError("https://api.github.com/x", r).Error() |
| 110 | + if strings.Contains(msg, "resets in") { |
| 111 | + t.Errorf("an unparseable reset header must be dropped, got: %s", msg) |
| 112 | + } |
| 113 | +} |
0 commit comments