s3manager-web/delete-bucket_test.go

47 lines
994 B
Go
Raw Normal View History

2017-04-03 14:08:01 +02:00
package main
2017-04-02 17:10:36 +02:00
import (
"errors"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
2017-04-03 14:08:01 +02:00
func TestDeleteBucketHandler(t *testing.T) {
2017-04-02 17:10:36 +02:00
assert := assert.New(t)
2017-04-03 14:08:01 +02:00
tests := map[string]struct {
2017-04-03 22:03:45 +02:00
s3 S3Client
2017-04-02 17:10:36 +02:00
expectedStatusCode int
expectedBody string
}{
2017-04-03 14:08:01 +02:00
"success": {
2017-04-03 22:03:45 +02:00
s3: &S3ClientMock{},
2017-04-02 17:10:36 +02:00
expectedStatusCode: http.StatusNoContent,
expectedBody: "",
},
2017-04-03 14:08:01 +02:00
"s3 error": {
2017-04-03 22:03:45 +02:00
s3: &S3ClientMock{
2017-04-03 23:52:41 +02:00
Err: errors.New("mocked S3 error"),
2017-04-02 17:10:36 +02:00
},
expectedStatusCode: http.StatusInternalServerError,
expectedBody: "error removing bucket\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.MethodDelete, "/api/buckets/bucketName", nil)
2017-04-07 12:51:23 +02:00
assert.NoError(err, tcID)
2017-04-02 17:10:36 +02:00
rr := httptest.NewRecorder()
2017-04-03 22:03:45 +02:00
handler := DeleteBucketHandler(tc.s3)
2017-04-02 17:10:36 +02:00
handler.ServeHTTP(rr, req)
2017-04-07 12:51:23 +02:00
assert.Equal(tc.expectedStatusCode, rr.Code, tcID)
assert.Equal(tc.expectedBody, rr.Body.String(), tcID)
2017-04-02 17:10:36 +02:00
}
}