| 1 | package appupdate |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "io" |
| 9 | "net/http" |
| 10 | "strconv" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | "time" |
| 14 | ) |
| 15 | |
| 16 | func TestUpdateSourceRetryInstructions(t *testing.T) { |
| 17 | now := time.Now().UTC().Truncate(time.Second) |
| 18 | for _, tc := range []struct { |
| 19 | name string |
| 20 | status int |
| 21 | headers http.Header |
| 22 | wait time.Duration |
| 23 | }{ |
| 24 | {"seconds", 429, http.Header{"Retry-After": {"3600"}}, time.Hour}, |
| 25 | {"date", 429, http.Header{"Retry-After": {now.Add(2 * time.Hour).Format(http.TimeFormat)}}, 2 * time.Hour}, |
| 26 | {"primary", 403, http.Header{"X-Ratelimit-Remaining": {"0"}, "X-Ratelimit-Reset": {strconv.FormatInt(now.Add(time.Hour).Unix(), 10)}}, time.Hour}, |
| 27 | {"both", 429, http.Header{"Retry-After": {"60"}, "X-Ratelimit-Remaining": {"0"}, "X-Ratelimit-Reset": {strconv.FormatInt(now.Add(time.Hour).Unix(), 10)}}, time.Hour}, |
| 28 | {"malformed", 429, http.Header{"Retry-After": {"9999999999999999999999"}}, 5 * time.Minute}, |
| 29 | {"past", 429, http.Header{"Retry-After": {now.Add(-time.Hour).Format(http.TimeFormat)}}, 5 * time.Minute}, |
| 30 | {"forbidden", 403, nil, 0}, |
| 31 | {"remaining", 403, http.Header{"X-Ratelimit-Remaining": {"10"}, "X-Ratelimit-Reset": {strconv.FormatInt(now.Add(time.Hour).Unix(), 10)}}, 0}, |
| 32 | } { |
| 33 | t.Run(tc.name, func(t *testing.T) { |
| 34 | client := &http.Client{Transport: roundTrip(func(r *http.Request) (*http.Response, error) { |
| 35 | return &http.Response{StatusCode: tc.status, Header: tc.headers, Body: io.NopCloser(strings.NewReader("private response must stay out of errors")), Request: r}, nil |
| 36 | })} |
| 37 | _, err := findRelease(context.Background(), client, "0.1.0") |
| 38 | if err == nil || strings.Contains(err.Error(), "private response") { |
| 39 | t.Fatalf("unsafe error: %v", err) |
| 40 | } |
| 41 | var delayed interface{ RetryAt() time.Time } |
| 42 | if tc.wait == 0 { |
| 43 | if errors.As(err, &delayed) || !strings.Contains(err.Error(), "403") { |
| 44 | t.Fatalf("ordinary forbidden misclassified: %v", err) |
| 45 | } |
| 46 | return |
| 47 | } |
| 48 | if !errors.As(err, &delayed) { |
| 49 | t.Fatalf("retry deadline lost: %v", err) |
| 50 | } |
| 51 | if delta := delayed.RetryAt().Sub(now.Add(tc.wait)); delta < -time.Second || delta > 5*time.Second { |
| 52 | t.Fatalf("wrong retry deadline: %s", delayed.RetryAt()) |
| 53 | } |
| 54 | state := State{} |
| 55 | state.fail(fmt.Errorf("wrapped: %w", err)) |
| 56 | data, _ := json.Marshal(state) |
| 57 | var wire map[string]any |
| 58 | _ = json.Unmarshal(data, &wire) |
| 59 | if wire["retryAt"] != delayed.RetryAt().UTC().Format(time.RFC3339) { |
| 60 | t.Fatalf("native protocol lost deadline: %s", data) |
| 61 | } |
| 62 | state.fail(errors.New("unrelated failure")) |
| 63 | data, _ = json.Marshal(state) |
| 64 | if !strings.Contains(string(data), `"retryAt":""`) { |
| 65 | t.Fatalf("stale deadline retained by merging native bridge: %s", data) |
| 66 | } |
| 67 | }) |
| 68 | } |
| 69 | } |