s3manager-web/get_object_test.go

61 lines
1.4 KiB
Go
Raw Normal View History

2017-05-08 23:07:07 +02:00
package s3manager_test
2017-04-03 23:52:41 +02:00
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/gorilla/mux"
2017-05-25 18:33:44 +02:00
. "github.com/mastertinner/s3manager"
2017-04-03 23:52:41 +02:00
"github.com/stretchr/testify/assert"
)
func TestGetObjectHandler(t *testing.T) {
assert := assert.New(t)
2017-05-08 23:07:07 +02:00
cases := map[string]struct {
2017-05-25 18:33:44 +02:00
s3 S3
2017-04-18 15:43:13 +02:00
bucketName string
objectName string
expectedStatusCode int
expectedBodyContains string
2017-04-03 23:52:41 +02:00
}{
"s3 error": {
2017-05-08 23:07:07 +02:00
s3: &s3Mock{
2017-04-03 23:52:41 +02:00
Err: errors.New("mocked S3 error"),
},
2017-04-18 15:43:13 +02:00
bucketName: "testBucket",
objectName: "testObject",
expectedStatusCode: http.StatusInternalServerError,
expectedBodyContains: http.StatusText(http.StatusInternalServerError),
2017-04-03 23:52:41 +02:00
},
}
2017-05-08 23:07:07 +02:00
for tcID, tc := range cases {
2017-07-31 10:57:22 +02:00
t.Run(tcID, func(t *testing.T) {
r := mux.NewRouter()
r.
Methods(http.MethodGet).
Path("/buckets/{bucketName}/objects/{objectName}").
Handler(GetObjectHandler(tc.s3))
ts := httptest.NewServer(r)
defer ts.Close()
url := fmt.Sprintf("%s/buckets/%s/objects/%s", ts.URL, tc.bucketName, tc.objectName)
resp, err := http.Get(url)
2017-04-19 14:18:58 +02:00
assert.NoError(err, tcID)
2017-07-31 10:57:22 +02:00
defer resp.Body.Close()
2017-04-03 23:52:41 +02:00
2017-07-31 10:57:22 +02:00
body, err := ioutil.ReadAll(resp.Body)
assert.NoError(err, tcID)
2017-04-03 23:52:41 +02:00
2017-07-31 10:57:22 +02:00
assert.Equal(tc.expectedStatusCode, resp.StatusCode, tcID)
assert.Contains(string(body), tc.expectedBodyContains, tcID)
})
2017-04-03 23:52:41 +02:00
}
}