| 1 | package installsource |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | func TestFetchTextAllowsBodyAtLimit(t *testing.T) { |
| 13 | body := strings.Repeat("x", defaultFetchLimit) |
| 14 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 15 | _, _ = w.Write([]byte(body)) |
| 16 | })) |
| 17 | defer srv.Close() |
| 18 | |
| 19 | tool := NewTool(Options{ |
| 20 | ProjectRoot: t.TempDir(), |
| 21 | HomeDir: t.TempDir(), |
| 22 | HTTPClient: srv.Client(), |
| 23 | }).(*installSourceTool) |
| 24 | |
| 25 | got, err := tool.fetchText(context.Background(), srv.URL) |
| 26 | if err != nil { |
| 27 | t.Fatalf("fetchText() error = %v", err) |
| 28 | } |
| 29 | if got != body { |
| 30 | t.Fatalf("fetchText() returned %d bytes, want %d", len(got), len(body)) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | func TestFetchTextRejectsOversizedBody(t *testing.T) { |
| 35 | body := strings.Repeat("x", defaultFetchLimit+1) |
| 36 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 37 | _, _ = w.Write([]byte(body)) |
| 38 | })) |
| 39 | defer srv.Close() |
| 40 | |
| 41 | tool := NewTool(Options{ |
| 42 | ProjectRoot: t.TempDir(), |
| 43 | HomeDir: t.TempDir(), |
| 44 | HTTPClient: srv.Client(), |
| 45 | }).(*installSourceTool) |
| 46 | |
| 47 | got, err := tool.fetchText(context.Background(), srv.URL) |
| 48 | if !errors.Is(err, ErrSourceUnreadable) { |
| 49 | t.Fatalf("fetchText() error = %v, want ErrSourceUnreadable", err) |
| 50 | } |
| 51 | if got != "" { |
| 52 | t.Fatalf("fetchText() returned %d bytes after rejecting oversized body", len(got)) |
| 53 | } |
| 54 | } |
| 55 |