From 2ecaaa0bde869f951d6f1d1c0674408cfa9f6c13 Mon Sep 17 00:00:00 2001 From: Brandur Date: Thu, 27 Jun 2024 19:18:04 -0700 Subject: [PATCH] Add test case for timeout in API endpoint infrastructure Follows up #63 with one more test case that checks to make sure the right thing happens if a timeout occurs. (The API endpoint executor adds a five seconds timeout to every run by default.) --- internal/apiendpoint/api_endpoint_test.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/internal/apiendpoint/api_endpoint_test.go b/internal/apiendpoint/api_endpoint_test.go index 168d4c20..287bd180 100644 --- a/internal/apiendpoint/api_endpoint_test.go +++ b/internal/apiendpoint/api_endpoint_test.go @@ -8,6 +8,7 @@ import ( "net/http" "net/http/httptest" "testing" + "time" "github.com/stretchr/testify/require" @@ -18,6 +19,8 @@ import ( func TestMountAndServe(t *testing.T) { t.Parallel() + ctx := context.Background() + type testBundle struct { recorder *httptest.ResponseRecorder } @@ -96,6 +99,22 @@ func TestMountAndServe(t *testing.T) { requireStatusAndJSONResponse(t, http.StatusBadRequest, &apierror.APIError{Message: "Missing message value."}, bundle.recorder) }) + t.Run("Timeout", func(t *testing.T) { + t.Parallel() + + mux, bundle := setup(t) + + ctx, cancel := context.WithDeadline(ctx, time.Now()) + t.Cleanup(cancel) + + req, err := http.NewRequestWithContext(ctx, http.MethodPost, "/api/post-endpoint", + bytes.NewBuffer(mustMarshalJSON(t, &postRequest{MakeInternalError: true}))) + require.NoError(t, err) + mux.ServeHTTP(bundle.recorder, req) + + requireStatusAndJSONResponse(t, http.StatusInternalServerError, &apierror.APIError{Message: "Internal server error. Check logs for more information."}, bundle.recorder) + }) + t.Run("InternalServerError", func(t *testing.T) { t.Parallel()