Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
package main
import (
. ""
func TestGettingSuccessfulResponseToPostData(t *testing.T) {
Convey("Make a post request and get a response", t, func() {
var b bytes.Buffer
w := multipart.NewWriter(&b)
f, err := os.Open("testFiles/testImage.jpg")
if err != nil {
t.Error("Failed as coudln't open file", err)
defer f.Close()
fw, err := w.CreateFormFile("file", "testImage.jpg")
if err != nil {
t.Error("Error creating form element", err)
if _, err = io.Copy(fw, f); err != nil {
t.Error("Error copying file to buffer", err)
req, err := http.NewRequest("POST", "", &b)
if err != nil {
req.Header.Set("Content-Type", w.FormDataContentType())
recorder := httptest.NewRecorder()
PhotoUploaderHandler(recorder, req)
Convey("The code should be 201", func() {
body := PhotoSuccessResponse{}
json.Unmarshal(recorder.Body.Bytes(), &body)
So(body.Filename, ShouldEqual, "testImage.jpg")
So(body.URL, ShouldEqual, "")
So(recorder.Code, ShouldEqual, 201)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.