30 lines
735 B
Go
30 lines
735 B
Go
package app
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestWithRequestTimeoutAddsContextDeadline(t *testing.T) {
|
|
handler := withRequestTimeout(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
deadline, ok := r.Context().Deadline()
|
|
if !ok {
|
|
t.Fatal("request context should have deadline")
|
|
}
|
|
if time.Until(deadline) > 250*time.Millisecond {
|
|
t.Fatalf("deadline too far away: %s", time.Until(deadline))
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}), 200*time.Millisecond)
|
|
|
|
rec := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodGet, "/api/ops/health", nil)
|
|
handler.ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusNoContent {
|
|
t.Fatalf("status = %d", rec.Code)
|
|
}
|
|
}
|