Add unit test for bucket view
This commit is contained in:
parent
e16823ade8
commit
3b08aca176
6 changed files with 114 additions and 17 deletions
|
@ -38,11 +38,18 @@ func BucketViewHandler(s3 S3Client) http.Handler {
|
|||
}
|
||||
|
||||
doneCh := make(chan struct{})
|
||||
defer close(doneCh)
|
||||
objectCh := s3.ListObjectsV2(bucketName, "", true, doneCh)
|
||||
for object := range objectCh {
|
||||
if object.Err != nil {
|
||||
msg := "error listing objects"
|
||||
handleHTTPError(w, msg, err, http.StatusInternalServerError)
|
||||
code := http.StatusInternalServerError
|
||||
if object.Err.Error() == "The specified bucket does not exist." {
|
||||
msg = "bucket not found"
|
||||
code = http.StatusNotFound
|
||||
}
|
||||
|
||||
handleHTTPError(w, msg, object.Err, code)
|
||||
return
|
||||
}
|
||||
objectWithIcon := ObjectWithIcon{object, icon(object.Key)}
|
||||
|
|
61
bucket-view_test.go
Normal file
61
bucket-view_test.go
Normal file
|
@ -0,0 +1,61 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestBucketViewHandler(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
tests := map[string]struct {
|
||||
s3 S3Client
|
||||
bucketName string
|
||||
expectedStatusCode int
|
||||
expectedBody string
|
||||
}{
|
||||
"bucket doesn't exist": {
|
||||
s3: &S3ClientMock{},
|
||||
bucketName: "testBucket",
|
||||
expectedStatusCode: http.StatusNotFound,
|
||||
expectedBody: "bucket not found\n",
|
||||
},
|
||||
"s3 error": {
|
||||
s3: &S3ClientMock{
|
||||
Err: errors.New("internal S3 error"),
|
||||
},
|
||||
bucketName: "testBucket",
|
||||
expectedStatusCode: http.StatusInternalServerError,
|
||||
expectedBody: "error listing objects\n",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
r := mux.NewRouter()
|
||||
r.
|
||||
Methods("GET").
|
||||
Path("/buckets/{bucketName}").
|
||||
Handler(BucketViewHandler(tc.s3))
|
||||
|
||||
ts := httptest.NewServer(r)
|
||||
defer ts.Close()
|
||||
|
||||
url := fmt.Sprintf("%s/buckets/%s", ts.URL, tc.bucketName)
|
||||
resp, err := http.Get(url)
|
||||
assert.NoError(err)
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
assert.NoError(err)
|
||||
|
||||
assert.Equal(tc.expectedStatusCode, resp.StatusCode)
|
||||
assert.Equal(tc.expectedBody, string(body))
|
||||
}
|
||||
}
|
|
@ -14,31 +14,31 @@ func TestCreateBucketHandler(t *testing.T) {
|
|||
assert := assert.New(t)
|
||||
|
||||
tests := map[string]struct {
|
||||
s3Client S3Client
|
||||
s3 S3Client
|
||||
body string
|
||||
expectedStatusCode int
|
||||
expectedBody string
|
||||
}{
|
||||
"success": {
|
||||
s3Client: &S3ClientMock{},
|
||||
s3: &S3ClientMock{},
|
||||
body: "{\"name\":\"myBucket\"}",
|
||||
expectedStatusCode: http.StatusCreated,
|
||||
expectedBody: "{\"name\":\"myBucket\",\"creationDate\":\"0001-01-01T00:00:00Z\"}\n",
|
||||
},
|
||||
"empty request": {
|
||||
s3Client: &S3ClientMock{},
|
||||
s3: &S3ClientMock{},
|
||||
body: "",
|
||||
expectedStatusCode: http.StatusUnprocessableEntity,
|
||||
expectedBody: "error decoding json\n",
|
||||
},
|
||||
"malformed request": {
|
||||
s3Client: &S3ClientMock{},
|
||||
s3: &S3ClientMock{},
|
||||
body: "}",
|
||||
expectedStatusCode: http.StatusUnprocessableEntity,
|
||||
expectedBody: "error decoding json\n",
|
||||
},
|
||||
"s3 error": {
|
||||
s3Client: &S3ClientMock{
|
||||
s3: &S3ClientMock{
|
||||
Err: errors.New("internal S3 error"),
|
||||
},
|
||||
body: "{\"name\":\"myBucket\"}",
|
||||
|
@ -52,7 +52,7 @@ func TestCreateBucketHandler(t *testing.T) {
|
|||
assert.NoError(err)
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
handler := CreateBucketHandler(tc.s3Client)
|
||||
handler := CreateBucketHandler(tc.s3)
|
||||
|
||||
handler.ServeHTTP(rr, req)
|
||||
|
||||
|
|
|
@ -13,17 +13,17 @@ func TestDeleteBucketHandler(t *testing.T) {
|
|||
assert := assert.New(t)
|
||||
|
||||
tests := map[string]struct {
|
||||
s3Client S3Client
|
||||
s3 S3Client
|
||||
expectedStatusCode int
|
||||
expectedBody string
|
||||
}{
|
||||
"success": {
|
||||
s3Client: &S3ClientMock{},
|
||||
s3: &S3ClientMock{},
|
||||
expectedStatusCode: http.StatusNoContent,
|
||||
expectedBody: "",
|
||||
},
|
||||
"s3 error": {
|
||||
s3Client: &S3ClientMock{
|
||||
s3: &S3ClientMock{
|
||||
Err: errors.New("internal S3 error"),
|
||||
},
|
||||
expectedStatusCode: http.StatusInternalServerError,
|
||||
|
@ -36,7 +36,7 @@ func TestDeleteBucketHandler(t *testing.T) {
|
|||
assert.NoError(err)
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
handler := DeleteBucketHandler(tc.s3Client)
|
||||
handler := DeleteBucketHandler(tc.s3)
|
||||
|
||||
handler.ServeHTTP(rr, req)
|
||||
|
||||
|
|
|
@ -13,17 +13,17 @@ func TestDeleteObjectHandler(t *testing.T) {
|
|||
assert := assert.New(t)
|
||||
|
||||
tests := map[string]struct {
|
||||
s3Client S3Client
|
||||
s3 S3Client
|
||||
expectedStatusCode int
|
||||
expectedBody string
|
||||
}{
|
||||
"success": {
|
||||
s3Client: &S3ClientMock{},
|
||||
s3: &S3ClientMock{},
|
||||
expectedStatusCode: http.StatusNoContent,
|
||||
expectedBody: "",
|
||||
},
|
||||
"s3 error": {
|
||||
s3Client: &S3ClientMock{
|
||||
s3: &S3ClientMock{
|
||||
Err: errors.New("internal S3 error"),
|
||||
},
|
||||
expectedStatusCode: http.StatusInternalServerError,
|
||||
|
@ -36,7 +36,7 @@ func TestDeleteObjectHandler(t *testing.T) {
|
|||
assert.NoError(err)
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
handler := DeleteObjectHandler(tc.s3Client)
|
||||
handler := DeleteObjectHandler(tc.s3)
|
||||
|
||||
handler.ServeHTTP(rr, req)
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
|
||||
minio "github.com/minio/minio-go"
|
||||
|
@ -26,8 +27,36 @@ func (s S3ClientMock) ListBuckets() ([]minio.BucketInfo, error) {
|
|||
return s.Buckets, s.Err
|
||||
}
|
||||
|
||||
func (s S3ClientMock) ListObjectsV2(string, string, bool, <-chan struct{}) <-chan minio.ObjectInfo {
|
||||
return make(<-chan minio.ObjectInfo)
|
||||
func (s S3ClientMock) ListObjectsV2(bucketName string, p string, r bool, d <-chan struct{}) <-chan minio.ObjectInfo {
|
||||
// Add error if exists
|
||||
if s.Err != nil {
|
||||
s.ObjectInfos = append(s.ObjectInfos, minio.ObjectInfo{
|
||||
Err: s.Err,
|
||||
})
|
||||
}
|
||||
|
||||
// Check if bucket exists
|
||||
found := false
|
||||
for _, b := range s.Buckets {
|
||||
if b.Name == bucketName {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
s.ObjectInfos = append(s.ObjectInfos, minio.ObjectInfo{
|
||||
Err: errors.New("The specified bucket does not exist."),
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
objCh := make(chan minio.ObjectInfo, len(s.ObjectInfos))
|
||||
defer close(objCh)
|
||||
|
||||
for _, obj := range s.ObjectInfos {
|
||||
objCh <- obj
|
||||
}
|
||||
|
||||
return objCh
|
||||
}
|
||||
|
||||
func (s S3ClientMock) MakeBucket(string, string) error {
|
||||
|
|
Loading…
Reference in a new issue