Skip to content
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

Open
davidsonff opened this issue Jul 19, 2024 · 2 comments
Open

Implement TimeoutHandler #153

davidsonff opened this issue Jul 19, 2024 · 2 comments

Comments

@davidsonff
Copy link

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.

@steinfletcher
Copy link
Owner

steinfletcher commented Sep 3, 2024

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.

@davidsonff
Copy link
Author

Hi! Thanks for responding. I am using the SL? https://pkg.go.dev/net/http@go1.23.0#TimeoutHandler
I could write a wrapper, I suppose, just seemed like a logical addition to the suite?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants