mardi 30 juillet 2019

How to test that http.Redirect uses correct redirect uri in Go?

I have a backend in Go that uses the net/http module. I have an endpoint that is meant to redirect using http.Redirect to a provided redirect URI with some generated GET params. I simply want to test using net/http/httptest and testing that my endpoint is redirecting to the proper url.

I have executed the request using ServeHTTP and can verify that it does return the correct status code (303 in this case). I then tried accessing the Request URL to find the most recent URL that was accessed (what I assumed to be the redirect url), but I was unable to access this variable.

w := httptest.NewRecorder()
MyRouterMux().ServeHTTP(w, req)
resp := w.Result()

redirectUrl := resp.Request.URL

In this example, I would expect the redirectUrl variable to contain the url that was redirected to, but instead I receive [signal SIGSEGV: segmentation violation ...]

Any idea what could be going on? I know from manual testing that the endpoint works as intended, but I simply need to prove through the tests that it does.

Aucun commentaire:

Enregistrer un commentaire