MCPcopy Index your code
hub / github.com/supabase/auth / TestTimeoutMiddleware

Method TestTimeoutMiddleware

internal/api/middleware_test.go:376–402  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

374}
375
376func (ts *MiddlewareTestSuite) TestTimeoutMiddleware() {
377 ts.Config.API.MaxRequestDuration = 5 * time.Microsecond
378 req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
379 w := httptest.NewRecorder()
380
381 timeoutHandler := timeoutMiddleware(ts.Config.API.MaxRequestDuration)
382
383 slowHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
384 // Simulate a slow handler which should trigger the timeout.
385 // Use context-aware wait so the goroutine exits when the timeout fires,
386 // avoiding a data race with test cleanup.
387 select {
388 case <-time.After(1 * time.Second):
389 ts.API.handler.ServeHTTP(w, r)
390 case <-r.Context().Done():
391 return
392 }
393 })
394 timeoutHandler(slowHandler).ServeHTTP(w, req)
395 assert.Equal(ts.T(), http.StatusGatewayTimeout, w.Code)
396
397 var data map[string]interface{}
398 require.NoError(ts.T(), json.NewDecoder(w.Body).Decode(&data))
399 require.Equal(ts.T(), apierrors.ErrorCodeRequestTimeout, data["error_code"])
400 require.Equal(ts.T(), float64(504), data["code"])
401 require.NotNil(ts.T(), data["msg"])
402}
403
404func TestTimeoutResponseWriter(t *testing.T) {
405 // timeoutResponseWriter should exhitbit a similar behavior as http.ResponseWriter

Callers

nothing calls this directly

Calls 4

timeoutMiddlewareFunction · 0.85
EqualMethod · 0.80
ServeHTTPMethod · 0.45
DecodeMethod · 0.45

Tested by

no test coverage detected