I am trying to add form variables to a Go http request.
Here's how my Go test looks:
func sample_test(t *testing.T) {
handler := &my_listener_class{}
reader := strings.NewReader("number=2")
req, _ := http.NewRequest("POST", "/my_url", reader)
w := httptest.NewRecorder()
handler.function_to_test(w, req)
if w.Code != http.StatusOK {
t.Errorf("Home page didn't return %v", http.StatusOK)
}
}
The issue is that the form data never gets passed on to the function I need to test.
The other relevant function is:
func (listener *my_listener_class) function_to_test(writer http.ResponseWriter, request *http.Request) {
...
}
I am using Go version go1.3.3 darwin/amd64.
You need to add a Content-Type
header to the request so the handler will know how to treat the POST body data:
req, _ := http.NewRequest("POST", "/my_url", reader) //BTW check for error
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With