I'm using labstack's Echo framework for building APIs in Golang. Now I have a problem that I can't seem to get around when I want to test an endpoint.
I have a handler function like this
func DoStuff(c echo.Context) error {
businessUnit := strings.ToUpper(c.FormValue("bu"))
week := c.FormValue("wk")
file, _ := c.FormFile("file")
...
}
The handler works just fine. The problem I have now is writing integration tests for this.
This endpoint accepts a Content-Type: multipart/form-data
.
Here's how some of my other handler tests look:
func TestDoStuff(t *testing.T) {
// Not sure about this part tho
body := new(bytes.Buffer)
writer := multipart.NewWriter(body)
writer.WriteField("bu", "HFL")
writer.WriteField("wk", "10")
part, _ := writer.CreateFormFile("file", "file.csv")
part.Write([]byte(`sample`))
e := echo.New()
req := httptest.NewRequest(http.MethodPost, "/endpoint", body)
req.Header.Add("Content-Type", "multipart/form-data")
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
if assert.NotPanics(t, func(){ _ = DoStuff(c)}){
assert.Equal(t, http.StatusOK, rec.Code)
... more assertions ...
}
}
I can't seem to get the form values in the handler. Any help would be greatly appreciated.