|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "net/url" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +func newTestClient(t *testing.T, serverURL string) Client { |
| 14 | + t.Helper() |
| 15 | + endpointURL, err := url.Parse(serverURL) |
| 16 | + if err != nil { |
| 17 | + t.Fatal(err) |
| 18 | + } |
| 19 | + return NewClient(ClientOpts{EndpointURL: endpointURL, Out: io.Discard}) |
| 20 | +} |
| 21 | + |
| 22 | +func TestUnresponsiveServerTimesOut(t *testing.T) { |
| 23 | + t.Setenv("SRC_RESPONSE_HEADER_TIMEOUT", "100ms") |
| 24 | + |
| 25 | + block := make(chan struct{}) |
| 26 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 27 | + <-block |
| 28 | + })) |
| 29 | + defer server.Close() |
| 30 | + defer close(block) |
| 31 | + |
| 32 | + client := newTestClient(t, server.URL) |
| 33 | + req, err := client.NewHTTPRequest(context.Background(), http.MethodGet, "", nil) |
| 34 | + if err != nil { |
| 35 | + t.Fatal(err) |
| 36 | + } |
| 37 | + |
| 38 | + start := time.Now() |
| 39 | + resp, err := client.Do(req) |
| 40 | + if err == nil { |
| 41 | + resp.Body.Close() |
| 42 | + t.Fatal("expected a timeout error, got nil") |
| 43 | + } |
| 44 | + if elapsed := time.Since(start); elapsed > 5*time.Second { |
| 45 | + t.Fatalf("request took %s, expected it to fail within the response header timeout", elapsed) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func TestSlowStreamingDownloadSucceeds(t *testing.T) { |
| 50 | + t.Setenv("SRC_RESPONSE_HEADER_TIMEOUT", "200ms") |
| 51 | + |
| 52 | + const chunks = 5 |
| 53 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 54 | + flusher := w.(http.Flusher) |
| 55 | + w.WriteHeader(http.StatusOK) |
| 56 | + flusher.Flush() |
| 57 | + // Stream the body for much longer than the response header timeout. |
| 58 | + for range chunks { |
| 59 | + time.Sleep(100 * time.Millisecond) |
| 60 | + _, _ = w.Write([]byte("chunk")) |
| 61 | + flusher.Flush() |
| 62 | + } |
| 63 | + })) |
| 64 | + defer server.Close() |
| 65 | + |
| 66 | + client := newTestClient(t, server.URL) |
| 67 | + req, err := client.NewHTTPRequest(context.Background(), http.MethodGet, "", nil) |
| 68 | + if err != nil { |
| 69 | + t.Fatal(err) |
| 70 | + } |
| 71 | + |
| 72 | + resp, err := client.Do(req) |
| 73 | + if err != nil { |
| 74 | + t.Fatal(err) |
| 75 | + } |
| 76 | + defer resp.Body.Close() |
| 77 | + |
| 78 | + body, err := io.ReadAll(resp.Body) |
| 79 | + if err != nil { |
| 80 | + t.Fatalf("reading a slow streaming body failed: %v", err) |
| 81 | + } |
| 82 | + if want := len("chunk") * chunks; len(body) != want { |
| 83 | + t.Fatalf("got %d body bytes, want %d", len(body), want) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func TestResponseHeaderTimeoutEnv(t *testing.T) { |
| 88 | + tests := []struct { |
| 89 | + value string |
| 90 | + want time.Duration |
| 91 | + }{ |
| 92 | + {value: "", want: defaultResponseHeaderTimeout}, |
| 93 | + {value: "30s", want: 30 * time.Second}, |
| 94 | + {value: "0", want: 0}, |
| 95 | + {value: "garbage", want: defaultResponseHeaderTimeout}, |
| 96 | + {value: "-5s", want: defaultResponseHeaderTimeout}, |
| 97 | + } |
| 98 | + |
| 99 | + for _, test := range tests { |
| 100 | + t.Run(test.value, func(t *testing.T) { |
| 101 | + t.Setenv("SRC_RESPONSE_HEADER_TIMEOUT", test.value) |
| 102 | + if got := responseHeaderTimeout(); got != test.want { |
| 103 | + t.Fatalf("got %s, want %s", got, test.want) |
| 104 | + } |
| 105 | + }) |
| 106 | + } |
| 107 | +} |
0 commit comments