s3manager-web/create_bucket_test.go

67 lines
1.9 KiB
Go
Raw Normal View History

2017-05-08 23:07:07 +02:00
package s3manager_test
2017-04-02 17:10:36 +02:00
import (
"bytes"
"errors"
"net/http"
"net/http/httptest"
"testing"
2017-05-25 18:33:44 +02:00
. "github.com/mastertinner/s3manager"
2017-04-02 17:10:36 +02:00
"github.com/stretchr/testify/assert"
)
2017-04-03 14:08:01 +02:00
func TestCreateBucketHandler(t *testing.T) {
2017-05-08 23:07:07 +02:00
cases := map[string]struct {
2017-05-25 18:33:44 +02:00
s3 S3
2017-04-09 16:28:57 +02:00
body string
expectedStatusCode int
expectedBodyContains string
2017-04-02 17:10:36 +02:00
}{
2017-12-21 07:50:59 +01:00
"creates a new bucket": {
2017-05-08 23:07:07 +02:00
s3: &s3Mock{},
2017-04-09 16:28:57 +02:00
body: "{\"name\":\"myBucket\"}",
expectedStatusCode: http.StatusCreated,
expectedBodyContains: "{\"name\":\"myBucket\",\"creationDate\":\"0001-01-01T00:00:00Z\"}\n",
2017-04-02 17:10:36 +02:00
},
2017-12-21 07:50:59 +01:00
"returns error for empty request": {
2017-05-08 23:07:07 +02:00
s3: &s3Mock{},
2017-04-09 16:28:57 +02:00
body: "",
expectedStatusCode: http.StatusUnprocessableEntity,
expectedBodyContains: http.StatusText(http.StatusUnprocessableEntity),
2017-04-02 17:10:36 +02:00
},
2017-12-21 07:50:59 +01:00
"returns error for malformed request": {
2017-05-08 23:07:07 +02:00
s3: &s3Mock{},
2017-04-09 16:28:57 +02:00
body: "}",
expectedStatusCode: http.StatusUnprocessableEntity,
expectedBodyContains: http.StatusText(http.StatusUnprocessableEntity),
2017-04-02 17:10:36 +02:00
},
2017-12-21 07:50:59 +01:00
"returns error if there is an S3 error": {
2017-05-08 23:07:07 +02:00
s3: &s3Mock{
2017-04-03 23:52:41 +02:00
Err: errors.New("mocked S3 error"),
2017-04-02 17:10:36 +02:00
},
2017-04-09 16:28:57 +02:00
body: "{\"name\":\"myBucket\"}",
expectedStatusCode: http.StatusInternalServerError,
expectedBodyContains: http.StatusText(http.StatusInternalServerError),
2017-04-02 17:10:36 +02:00
},
}
2017-05-08 23:07:07 +02:00
for tcID, tc := range cases {
2017-07-31 10:57:22 +02:00
t.Run(tcID, func(t *testing.T) {
2017-12-21 07:50:59 +01:00
assert := assert.New(t)
2017-07-31 10:57:22 +02:00
req, err := http.NewRequest(http.MethodPost, "/api/buckets", bytes.NewBufferString(tc.body))
assert.NoError(err, tcID)
2017-04-02 17:10:36 +02:00
2017-07-31 10:57:22 +02:00
rr := httptest.NewRecorder()
handler := CreateBucketHandler(tc.s3)
2017-04-02 17:10:36 +02:00
2017-07-31 10:57:22 +02:00
handler.ServeHTTP(rr, req)
resp := rr.Result()
2017-04-02 17:10:36 +02:00
2017-07-31 10:57:22 +02:00
assert.Equal(tc.expectedStatusCode, resp.StatusCode, tcID)
assert.Contains(rr.Body.String(), tc.expectedBodyContains, tcID)
})
2017-04-02 17:10:36 +02:00
}
}