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

request Params not populated when handler invoked directly during testing #46

Closed
aharbis opened this issue Dec 20, 2021 · 3 comments · Fixed by #48
Closed

request Params not populated when handler invoked directly during testing #46

aharbis opened this issue Dec 20, 2021 · 3 comments · Fixed by #48

Comments

@aharbis
Copy link

aharbis commented Dec 20, 2021

Using a test request generator like this, from the docs:

func NewRequest(method, target string, body io.Reader) bunrouter.Request {
	return bunrouter.NewRequest(httptest.NewRequest(method, target, body))
}

then generating a bunrouter.Request with:

testbed.NewRequest("GET", "/users/1", nil)

and expecting it to match based on the following rule:

router.GET("/users/:id", usersHandler.GetUser)

code within my GetUser handler cannot access the params when testing, i.e. passing the bunrouter.Request and an httptest.ResponseRecorder into the handler directly.

I've confirmed the Params work as expected when invoking the server from an actual client, but from what I can tell the Params map and nodes are all empty/nil when the handler is invoked directly using a test request.

For example, I've got this line at the top of my GetUser handler:

fmt.Printf("params: %#v\n", r.Params().Map())

When the handler is invoked via the router from a real client API call, it prints:

params: map[string]string{"id":"1"}

When the handler is invoked from a test suite, using a bunrouter.Request, it prints:

params: map[string]string(nil)
@vmihailenco
Copy link
Member

vmihailenco commented Dec 20, 2021

but from what I can tell the Params map and nodes are all empty/nil when the handler is invoked directly using a test request.

This is expected, because you skip the whole routing process which is responsible for extracting route params. Also, we can't extract params without having any routes.

The simplest test looks like this and uses router.ServeHTTP:

	router := New()
	router.GET("/user/:param", func(w http.ResponseWriter, req Request) error {
		value1 := req.Param("param")
		require.Equal(t, "hello", value1)
		return nil
	})

	w := httptest.NewRecorder()
	req, _ := http.NewRequest(http.MethodGet, "/user/hello", nil)
	router.ServeHTTP(w, req)

Does that work for you?

@aharbis
Copy link
Author

aharbis commented Dec 20, 2021

Yes, after opening this issue I was searching through the pkg and found router.ServeHTTP which I think will work in my test cases. The main difference appears to be that I need to pass in a raw http.Request instead of a bunrouter.Request.

Also, we can't extract params without having any routes.

I'm curious if there's a way to extract the params without going through ServeHTTP which does the internal lookup? In my test suite I've got a fully bootstrapped app with a bunrouter.Router so it should know about all of the routes and handlers.

The downside to using ServeHTTP (imo) is that it's testing the end-to-end flow of the request/response through all middleware and error handlers. So if I want to test the error response from an actual handler function the only way to do that is directly calling it with a bunrouter.Request; however, handler functions which rely on Params don't support this.

@vmihailenco
Copy link
Member

That is a very good point. With the new version you should be able to do this:

w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/user/hello", nil)
err := router.ServeHTTPError(w, req)
// check the err here

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

Successfully merging a pull request may close this issue.

2 participants