s3manager-web/create-bucket_test.go

63 lines
1.5 KiB
Go
Raw Normal View History

2017-04-03 14:08:01 +02:00
package main
2017-04-02 17:10:36 +02:00
import (
"bytes"
"errors"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
2017-04-03 14:08:01 +02:00
func TestCreateBucketHandler(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
body string
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
body: "{\"name\":\"myBucket\"}",
expectedStatusCode: http.StatusCreated,
expectedBody: "{\"name\":\"myBucket\",\"creationDate\":\"0001-01-01T00:00:00Z\"}\n",
},
2017-04-03 14:08:01 +02:00
"empty request": {
2017-04-03 22:03:45 +02:00
s3: &S3ClientMock{},
2017-04-02 17:10:36 +02:00
body: "",
expectedStatusCode: http.StatusUnprocessableEntity,
expectedBody: "error decoding json\n",
},
2017-04-03 14:08:01 +02:00
"malformed request": {
2017-04-03 22:03:45 +02:00
s3: &S3ClientMock{},
2017-04-02 17:10:36 +02:00
body: "}",
expectedStatusCode: http.StatusUnprocessableEntity,
expectedBody: "error decoding json\n",
},
2017-04-03 14:08:01 +02:00
"s3 error": {
2017-04-03 22:03:45 +02:00
s3: &S3ClientMock{
2017-04-02 17:10:36 +02:00
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()
2017-04-03 22:03:45 +02:00
handler := CreateBucketHandler(tc.s3)
2017-04-02 17:10:36 +02:00
handler.ServeHTTP(rr, req)
2017-04-03 14:08:01 +02:00
assert.Equal(tc.expectedStatusCode, rr.Code)
assert.Equal(tc.expectedBody, rr.Body.String())
2017-04-02 17:10:36 +02:00
}
}