2017-05-08 23:07:07 +02:00
|
|
|
package s3manager_test
|
2017-04-03 22:03:45 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2017-05-08 23:07:07 +02:00
|
|
|
"github.com/mastertinner/s3manager"
|
2017-04-03 22:19:07 +02:00
|
|
|
minio "github.com/minio/minio-go"
|
2017-04-03 22:03:45 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBucketViewHandler(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2017-05-08 23:07:07 +02:00
|
|
|
cases := map[string]struct {
|
|
|
|
s3 s3manager.S3
|
2017-04-18 15:43:13 +02:00
|
|
|
bucketName string
|
|
|
|
expectedStatusCode int
|
|
|
|
expectedBodyContains string
|
2017-04-03 22:03:45 +02:00
|
|
|
}{
|
2017-04-03 22:46:01 +02:00
|
|
|
"success (empty bucket)": {
|
2017-05-08 23:07:07 +02:00
|
|
|
s3: &s3Mock{
|
2017-04-03 22:46:01 +02:00
|
|
|
Buckets: []minio.BucketInfo{
|
|
|
|
{Name: "testBucket"},
|
|
|
|
},
|
|
|
|
},
|
2017-04-18 15:43:13 +02:00
|
|
|
bucketName: "testBucket",
|
|
|
|
expectedStatusCode: http.StatusOK,
|
|
|
|
expectedBodyContains: "No objects in",
|
2017-04-03 22:46:01 +02:00
|
|
|
},
|
|
|
|
"success (with file)": {
|
2017-05-08 23:07:07 +02:00
|
|
|
s3: &s3Mock{
|
2017-04-03 22:19:07 +02:00
|
|
|
Buckets: []minio.BucketInfo{
|
|
|
|
{Name: "testBucket"},
|
|
|
|
},
|
2017-04-03 23:52:41 +02:00
|
|
|
Objects: []minio.ObjectInfo{
|
2017-04-03 22:31:14 +02:00
|
|
|
{Key: "testFile"},
|
|
|
|
},
|
2017-04-03 22:19:07 +02:00
|
|
|
},
|
2017-04-18 15:43:13 +02:00
|
|
|
bucketName: "testBucket",
|
|
|
|
expectedStatusCode: http.StatusOK,
|
|
|
|
expectedBodyContains: "testBucket",
|
2017-04-03 22:19:07 +02:00
|
|
|
},
|
2017-04-03 22:31:14 +02:00
|
|
|
"success (archive)": {
|
2017-05-08 23:07:07 +02:00
|
|
|
s3: &s3Mock{
|
2017-04-03 22:31:14 +02:00
|
|
|
Buckets: []minio.BucketInfo{
|
|
|
|
{Name: "testBucket"},
|
|
|
|
},
|
2017-04-03 23:52:41 +02:00
|
|
|
Objects: []minio.ObjectInfo{
|
2017-04-03 22:31:14 +02:00
|
|
|
{Key: "archive.tar.gz"},
|
|
|
|
},
|
|
|
|
},
|
2017-04-18 15:43:13 +02:00
|
|
|
bucketName: "testBucket",
|
|
|
|
expectedStatusCode: http.StatusOK,
|
|
|
|
expectedBodyContains: "archive",
|
2017-04-03 22:31:14 +02:00
|
|
|
},
|
|
|
|
"success (image)": {
|
2017-05-08 23:07:07 +02:00
|
|
|
s3: &s3Mock{
|
2017-04-03 22:31:14 +02:00
|
|
|
Buckets: []minio.BucketInfo{
|
|
|
|
{Name: "testBucket"},
|
|
|
|
},
|
2017-04-03 23:52:41 +02:00
|
|
|
Objects: []minio.ObjectInfo{
|
2017-04-03 22:31:14 +02:00
|
|
|
{Key: "testImage.png"},
|
|
|
|
},
|
|
|
|
},
|
2017-04-18 15:43:13 +02:00
|
|
|
bucketName: "testBucket",
|
|
|
|
expectedStatusCode: http.StatusOK,
|
|
|
|
expectedBodyContains: "photo",
|
2017-04-03 22:31:14 +02:00
|
|
|
},
|
|
|
|
"success (sound)": {
|
2017-05-08 23:07:07 +02:00
|
|
|
s3: &s3Mock{
|
2017-04-03 22:31:14 +02:00
|
|
|
Buckets: []minio.BucketInfo{
|
|
|
|
{Name: "testBucket"},
|
|
|
|
},
|
2017-04-03 23:52:41 +02:00
|
|
|
Objects: []minio.ObjectInfo{
|
2017-04-03 22:31:14 +02:00
|
|
|
{Key: "testSound.mp3"},
|
|
|
|
},
|
|
|
|
},
|
2017-04-18 15:43:13 +02:00
|
|
|
bucketName: "testBucket",
|
|
|
|
expectedStatusCode: http.StatusOK,
|
|
|
|
expectedBodyContains: "music_note",
|
2017-04-03 22:31:14 +02:00
|
|
|
},
|
2017-04-03 22:03:45 +02:00
|
|
|
"bucket doesn't exist": {
|
2017-05-08 23:07:07 +02:00
|
|
|
s3: &s3Mock{},
|
2017-04-18 15:43:13 +02:00
|
|
|
bucketName: "testBucket",
|
|
|
|
expectedStatusCode: http.StatusNotFound,
|
|
|
|
expectedBodyContains: http.StatusText(http.StatusNotFound),
|
2017-04-03 22:03:45 +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-03 22:03:45 +02:00
|
|
|
},
|
2017-04-18 15:43:13 +02:00
|
|
|
bucketName: "testBucket",
|
|
|
|
expectedStatusCode: http.StatusInternalServerError,
|
|
|
|
expectedBodyContains: http.StatusText(http.StatusInternalServerError),
|
2017-04-03 22:03:45 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-05-08 23:07:07 +02:00
|
|
|
for tcID, tc := range cases {
|
2017-04-03 22:03:45 +02:00
|
|
|
r := mux.NewRouter()
|
|
|
|
r.
|
2017-04-07 08:59:24 +02:00
|
|
|
Methods(http.MethodGet).
|
2017-04-03 22:03:45 +02:00
|
|
|
Path("/buckets/{bucketName}").
|
2017-05-08 23:07:07 +02:00
|
|
|
Handler(s3manager.BucketViewHandler(tc.s3))
|
2017-04-03 22:03:45 +02:00
|
|
|
|
|
|
|
ts := httptest.NewServer(r)
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
url := fmt.Sprintf("%s/buckets/%s", ts.URL, tc.bucketName)
|
|
|
|
resp, err := http.Get(url)
|
2017-04-07 12:51:23 +02:00
|
|
|
assert.NoError(err, tcID)
|
2017-04-19 14:18:58 +02:00
|
|
|
defer func() {
|
|
|
|
err = resp.Body.Close()
|
|
|
|
assert.NoError(err, tcID)
|
|
|
|
}()
|
2017-04-03 22:03:45 +02:00
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
2017-04-07 12:51:23 +02:00
|
|
|
assert.NoError(err, tcID)
|
2017-04-03 22:03:45 +02:00
|
|
|
|
2017-04-07 12:51:23 +02:00
|
|
|
assert.Equal(tc.expectedStatusCode, resp.StatusCode, tcID)
|
2017-04-18 15:43:13 +02:00
|
|
|
assert.Contains(string(body), tc.expectedBodyContains, tcID)
|
2017-04-03 22:03:45 +02:00
|
|
|
}
|
|
|
|
}
|