library: an empty GitHub answer is said so, and latestRTK says a cut one (#3045)
Rebase of this issue's patch onto current main, narrowed to what main
still misses: 33ccc6188 (#2794) took ye4241's patch and gave apiGet its
read error ("couldn't read GitHub's answer") and its over-cap error, so
the read-error half of ours is dropped here. What is left of ours:
- apiGet refuses a 200 whose body is empty ("GitHub answered nothing"):
without it an empty answer under cacheBody is kept for a 304, and
every later ask of the same URL is answered nothing forever.
- latestRTK read its answer with `body, _ :=`, so a cut-off one came
back as a parse failure or "no version in"; it now says the read
broke, and its cut-short test rides along.
Run: TestAPIGetEmptyAnswer fails without the empty-body check and
TestLatestRTKCutAnswer fails without the latestRTK wrap, both pass with
them; the whole internal/library package passes; gofmt/vet/builds clean.
Generated-by: GLM-5.3-Flash (ZCode)
This commit is contained in:
@@ -302,7 +302,10 @@ func latestRTK() (string, error) {
|
||||
var r struct {
|
||||
Tag string `json:"tag_name"`
|
||||
}
|
||||
body, _ := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
|
||||
body, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("couldn't read GitHub's answer: %w", err)
|
||||
}
|
||||
if err := json.Unmarshal(body, &r); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
@@ -57,6 +57,27 @@ func asJSON(t *testing.T, v *RTKView) map[string]any {
|
||||
return m
|
||||
}
|
||||
|
||||
// An answer cut off on the way is said so, not shown as a version that
|
||||
// couldn't be read (#3045).
|
||||
func TestLatestRTKCutAnswer(t *testing.T) {
|
||||
sandbox(t)
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Length", "1000")
|
||||
w.Write([]byte(`{"tag_name": "v0.`))
|
||||
w.(http.Flusher).Flush() // the answer's head arrives, its body doesn't
|
||||
panic(http.ErrAbortHandler)
|
||||
}))
|
||||
defer srv.Close()
|
||||
old, oldAPI := rtkReleases, rtkReleasesAPI
|
||||
rtkReleases, rtkReleasesAPI = srv.URL+"/latest", srv.URL
|
||||
t.Cleanup(func() { rtkReleases, rtkReleasesAPI = old, oldAPI })
|
||||
if _, err := latestRTK(); err == nil {
|
||||
t.Error("a cut-short answer wasn't noticed")
|
||||
} else if !strings.Contains(err.Error(), "couldn't read GitHub's answer") {
|
||||
t.Errorf("said: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Homebrew's rtk is upgraded with brew; the days it saved come with the
|
||||
// summary, oldest first; the latest release is read from where GitHub
|
||||
// redirects to.
|
||||
|
||||
@@ -203,6 +203,10 @@ func apiGet(u string, max int64) ([]byte, int, bool, error) {
|
||||
if resp.StatusCode == 200 && int64(len(body)) > max {
|
||||
return nil, resp.StatusCode, token, fmt.Errorf("GitHub's answer was over %s: %w", mb(max), errOverCap)
|
||||
}
|
||||
// an empty 200 kept for a 304 would answer nothing forever (#3045)
|
||||
if resp.StatusCode == 200 && len(body) == 0 {
|
||||
return nil, resp.StatusCode, token, fmt.Errorf("GitHub answered nothing")
|
||||
}
|
||||
if et := resp.Header.Get("ETag"); resp.StatusCode == 200 && et != "" && int64(len(body)) <= cacheBody {
|
||||
etags.Lock()
|
||||
etags.m[u] = etagged{etag: et, body: body}
|
||||
|
||||
@@ -250,3 +250,16 @@ func TestApiGetWholeAnswer(t *testing.T) {
|
||||
t.Fatalf("502 over the cap: code %d, err %v", code, err)
|
||||
}
|
||||
}
|
||||
|
||||
// An empty 200 is said so, not kept for a 304 to answer nothing with
|
||||
// again (#3045).
|
||||
func TestAPIGetEmptyAnswer(t *testing.T) {
|
||||
sandbox(t)
|
||||
empty := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
|
||||
defer empty.Close()
|
||||
if _, _, _, err := apiGet(empty.URL, 8<<20); err == nil {
|
||||
t.Error("an empty answer wasn't noticed")
|
||||
} else if !strings.Contains(err.Error(), "GitHub answered nothing") {
|
||||
t.Errorf("said: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user