| index 29e2e68..6b215c2 100644 |
| --- a/adopt/delivery_test.go |
| +++ b/adopt/delivery_test.go |
| @@ -25,7 +25,7 @@ func (s *site) handler() http.Handler { |
| return |
| } |
| w.Header().Set("X-Carlos-Version", s.header) |
| - _, _ = w.Write([]byte(s.version + "\n")) |
| + _, _ = w.Write([]byte(s.version)) |
| }) |
| mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(s.health) }) |
| mux.HandleFunc("/api/v1/operations", func(w http.ResponseWriter, r *http.Request) { |
| @@ -136,15 +136,55 @@ func TestProbeRefusesNonLoopbackHTTP(t *testing.T) { |
| } |
| |
| func TestProbeErrorsCarryNoBody(t *testing.T) { |
| - s := &site{version: "secret-looking-body", header: "h", health: 200} |
| - srv := httptest.NewServer(s.handler()) |
| - srv.Close() |
| + // The answer promises more bytes than it sends, so the body read fails |
| + // after recognisable content has arrived. |
| + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| + w.Header().Set("Content-Length", "1000") |
| + _, _ = w.Write([]byte("LEAKY-BODY-CONTENT")) |
| + })) |
| + defer srv.Close() |
| _, err := Probe(context.Background(), srv.Client(), srv.URL) |
| if err == nil { |
| - t.Fatal("expected a transport error") |
| + t.Fatal("expected a body read failure") |
| + } |
| + for _, leak := range []string{"LEAKY-BODY-CONTENT", srv.URL, strings.TrimPrefix(srv.URL, "http://")} { |
| + if strings.Contains(err.Error(), leak) { |
| + t.Fatalf("error leaks %q: %v", leak, err) |
| + } |
| } |
| - if strings.Contains(err.Error(), "secret-looking-body") { |
| - t.Fatal("error leaks a body") |
| +} |
| + |
| +func TestProbeKeepsBodyExactly(t *testing.T) { |
| + s := &site{version: "v1\n", header: "v1", health: 200} |
| + a := probeOf(t, s) |
| + s.version = "v1" |
| + b := probeOf(t, s) |
| + if a.Version == b.Version { |
| + t.Fatal("a whitespace-only change was hidden") |
| + } |
| + before := Delivery{Version: a.Version, Header: "v1", Health: 200, Ops: "absent"} |
| + now := Delivery{Version: b.Version, Header: "v1", Health: 200, Ops: "agent-401"} |
| + if err := Delivered(before, now); err == nil { |
| + t.Fatal("Delivered approved a whitespace-only change") |
| + } |
| +} |
| + |
| +func TestProbeRejectsOversizedBodies(t *testing.T) { |
| + prefix := strings.Repeat("a", bodyLimit) |
| + for _, tail := range []string{"x", "y"} { |
| + body := prefix + tail |
| + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { _, _ = w.Write([]byte(body)) })) |
| + _, err := Probe(context.Background(), srv.Client(), srv.URL) |
| + srv.Close() |
| + if err == nil { |
| + t.Fatalf("oversized body (%s) accepted", tail) |
| + } |
| + } |
| + // Exactly the limit is fine. |
| + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { _, _ = w.Write([]byte(prefix)) })) |
| + defer srv.Close() |
| + if _, err := Probe(context.Background(), srv.Client(), srv.URL); err != nil { |
| + t.Fatalf("a body of exactly the limit refused: %v", err) |
| } |
| } |
| |