-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement TimeoutHandler #153
Comments
Hey @davidsonff, thanks for the request. Just trying to understand what you would like to test on your side... An example might help. Are you trying to force a timeout of a handler? Wondering if you could just wrap a handler in a custom middleware which sleeps, which could be incorporated into apitest, e.g. func TestApiTest_ForcedTimeout(t *testing.T) {
handler := http.NewServeMux()
handler.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
wrappedHandler := timeoutSimulatorMiddleware(handler)
timeoutHandler := http.TimeoutHandler(wrappedHandler, 1*time.Second, "timeout")
request := httptest.NewRequest(http.MethodGet, "/test", nil)
apitest.Handler(timeoutHandler).
HttpRequest(request).
Expect(t).
Status(http.StatusServiceUnavailable).
End()
}
func timeoutSimulatorMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Simulate a delay to trigger the timeout
time.Sleep(2 * time.Second)
next.ServeHTTP(w, r)
})
} Thanks. |
Hi! Thanks for responding. I am using the SL? https://pkg.go.dev/net/http@go1.23.0#TimeoutHandler |
Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. E.g. I'm always frustrated when ...
I'm having a problem testing the timeout feature of my request handlers
Describe the solution you'd like
A clear and concise description of what you want to happen.
An ability to test the fact that a TimeoutHandler actually times out.
Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.
Tried to see if intercept functionality could help but doesn't appear to be able to wrap a handler with a timeouthandler
Additional context
Add any other context or screenshots about the feature request here.
The text was updated successfully, but these errors were encountered: