Skip to content

Instantly share code, notes, and snippets.

@robwithhair
Created June 28, 2016 14:00
Show Gist options
  • Save robwithhair/b702bd7a43344af08080ecc1fa57f7e9 to your computer and use it in GitHub Desktop.
Save robwithhair/b702bd7a43344af08080ecc1fa57f7e9 to your computer and use it in GitHub Desktop.
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