s3manager-web/buckets-view_test.go

57 lines
1.2 KiB
Go
Raw Normal View History

2017-04-03 22:46:01 +02:00
package main
import (
"errors"
"net/http"
"net/http/httptest"
"testing"
minio "github.com/minio/minio-go"
"github.com/stretchr/testify/assert"
)
func TestBucketsViewHandler(t *testing.T) {
assert := assert.New(t)
tests := map[string]struct {
s3 S3Client
expectedStatusCode int
expectedBodyContains string
}{
"success": {
s3: &S3ClientMock{
Buckets: []minio.BucketInfo{
{Name: "testBucket"},
},
},
expectedStatusCode: http.StatusOK,
expectedBodyContains: "testBucket",
},
"success (bo buckets)": {
s3: &S3ClientMock{},
expectedStatusCode: http.StatusOK,
expectedBodyContains: "No buckets yet",
},
"s3 error": {
s3: &S3ClientMock{
2017-04-03 23:52:41 +02:00
Err: errors.New("mocked S3 error"),
2017-04-03 22:46:01 +02:00
},
expectedStatusCode: http.StatusInternalServerError,
expectedBodyContains: "error listing buckets\n",
},
}
2017-04-07 12:51:23 +02:00
for tcID, tc := range tests {
2017-04-07 08:59:24 +02:00
req, err := http.NewRequest(http.MethodGet, "/buckets", nil)
2017-04-07 12:51:23 +02:00
assert.NoError(err, tcID)
2017-04-03 22:46:01 +02:00
rr := httptest.NewRecorder()
handler := BucketsViewHandler(tc.s3)
handler.ServeHTTP(rr, req)
2017-04-07 12:51:23 +02:00
assert.Equal(tc.expectedStatusCode, rr.Code, tcID)
assert.Contains(rr.Body.String(), tc.expectedBodyContains, tcID)
2017-04-03 22:46:01 +02:00
}
}