Add test cases

This commit is contained in:
Lena Fuhrimann 2017-04-03 22:46:01 +02:00
parent 3cdb649365
commit d000984c4d
2 changed files with 67 additions and 1 deletions

View file

@ -22,7 +22,17 @@ func TestBucketViewHandler(t *testing.T) {
expectedStatusCode int
expectedBodyCountains string
}{
"success": {
"success (empty bucket)": {
s3: &S3ClientMock{
Buckets: []minio.BucketInfo{
{Name: "testBucket"},
},
},
bucketName: "testBucket",
expectedStatusCode: http.StatusOK,
expectedBodyCountains: "No objects in",
},
"success (with file)": {
s3: &S3ClientMock{
Buckets: []minio.BucketInfo{
{Name: "testBucket"},

56
buckets-view_test.go Normal file
View file

@ -0,0 +1,56 @@
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{
Err: errors.New("internal S3 error"),
},
expectedStatusCode: http.StatusInternalServerError,
expectedBodyContains: "error listing buckets\n",
},
}
for _, tc := range tests {
req, err := http.NewRequest("GET", "/buckets", nil)
assert.NoError(err)
rr := httptest.NewRecorder()
handler := BucketsViewHandler(tc.s3)
handler.ServeHTTP(rr, req)
assert.Equal(tc.expectedStatusCode, rr.Code)
assert.Contains(rr.Body.String(), tc.expectedBodyContains)
}
}