Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package main
import (
"bytes"
"encoding/json"
. "github.com/smartystreets/goconvey/convey"
"io"
"mime/multipart"
"net/http"
"net/http/httptest"
"os"
"testing"
)
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)
}
w.Close()
req, err := http.NewRequest("POST", "http://example.com/api-v1/photos", &b)
if err != nil {
t.Fatal(err)
}
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, "https://storage.googleapis.com/test-bucket-tesco-photos/testImage.jpg")
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.