I'm building a micro service in Go to handle user image uploads. Currently when I send an Image via the following curl everything works as expected, and at the end of the upload {"POST":"Successful"} is returned as JSON.
Identifier = username as string
uploadfile = path to image being uploaded
curl -F identifier=test -F uploadfile=@1.jpg localhost:9090/upload
My problem stems from trying to write a test for this; I want my test to to fail if {"POST":"Sucsessful"} isn't returned. I'm currently running into an error multipart: NextPart: EOF and therefore nothing is being returned to rr.Body.String()
payload := strings.NewReader("identifier=test&uploadfile=@1.jpg")
req, err := http.NewRequest("POST", "/upload", payload)
if err != nil {
t.Fatal(err)
}
req.Header.Add("content-type", "multipart/form-data; boundary=&")
rr := httptest.NewRecorder()
// webUpload.Upload <--- reference to the upload function handling POST
handler := http.HandlerFunc(webUpload.Upload)
handler.ServeHTTP(rr, req)
expected := `{"POST":"Sucsessful"}`
if rr.Body.String() != expected {
t.Errorf("handler returned unexpected body: got %v want %v",
rr.Body.String(), expected)
}
I've had problems with the boundary param in multipart and I'm not 100% if I've fixed this correctly. I've also been looking here: https://golang.org/pkg/mime/multipart/#NewReader although I've had no luck. I'm brand new to Go so any advise is welcome
Aucun commentaire:
Enregistrer un commentaire