mardi 7 mai 2019

How to test http.Redirect target URL in golang

I have set up a http redirect in the go handler, and I'm trying to verify if the target URL is composed as expected. I'm currently using httptest for relevant unit test.

In the handler:

url := fmt.Sprintf("%s%s?token=%s", domain, someURL, token)
http.Redirect(w, r, url, 302)

The current unit test can only verify the respond code but not the target URL:

resp := httptest.NewRecorder()
router.ServeHTTP(resp, req)
assert.Equal(t, 302, resp.Code)

I'm expecting something like

assert.Equal(t, expectedURL, resp.redirectURL)

Aucun commentaire:

Enregistrer un commentaire