s3manager-web/create-bucket_test.go
2017-04-03 14:08:01 +02:00

63 lines
1.6 KiB
Go

package main
import (
"bytes"
"errors"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestCreateBucketHandler(t *testing.T) {
assert := assert.New(t)
tests := map[string]struct {
s3Client S3Client
body string
expectedStatusCode int
expectedBody string
}{
"success": {
s3Client: &S3ClientMock{},
body: "{\"name\":\"myBucket\"}",
expectedStatusCode: http.StatusCreated,
expectedBody: "{\"name\":\"myBucket\",\"creationDate\":\"0001-01-01T00:00:00Z\"}\n",
},
"empty request": {
s3Client: &S3ClientMock{},
body: "",
expectedStatusCode: http.StatusUnprocessableEntity,
expectedBody: "error decoding json\n",
},
"malformed request": {
s3Client: &S3ClientMock{},
body: "}",
expectedStatusCode: http.StatusUnprocessableEntity,
expectedBody: "error decoding json\n",
},
"s3 error": {
s3Client: &S3ClientMock{
Err: errors.New("internal S3 error"),
},
body: "{\"name\":\"myBucket\"}",
expectedStatusCode: http.StatusInternalServerError,
expectedBody: "error making bucket\n",
},
}
for _, tc := range tests {
req, err := http.NewRequest("POST", "/api/buckets", bytes.NewBufferString(tc.body))
assert.NoError(err)
rr := httptest.NewRecorder()
handler := CreateBucketHandler(tc.s3Client)
handler.ServeHTTP(rr, req)
assert.Equal(tc.expectedStatusCode, rr.Code)
assert.Equal(tc.expectedBody, rr.Body.String())
}
}