s3manager-web/internal/app/s3manager/delete_object_test.go

56 lines
1.3 KiB
Go
Raw Normal View History

2017-05-08 23:07:07 +02:00
package s3manager_test
import (
"errors"
"net/http"
"net/http/httptest"
"testing"
2018-03-14 21:53:35 +01:00
"github.com/mastertinner/s3manager/internal/app/s3manager"
"github.com/stretchr/testify/assert"
)
2017-04-03 14:08:01 +02:00
func TestDeleteObjectHandler(t *testing.T) {
2017-05-08 23:07:07 +02:00
cases := map[string]struct {
2018-04-24 22:35:21 +02:00
removeObjectFunc func(string, string) error
2017-04-09 16:28:57 +02:00
expectedStatusCode int
expectedBodyContains string
}{
2017-12-21 07:50:59 +01:00
"deletes an existing object": {
2018-05-22 22:56:01 +02:00
removeObjectFunc: func(string, string) error {
2018-04-24 22:35:21 +02:00
return nil
},
2017-04-09 16:28:57 +02:00
expectedStatusCode: http.StatusNoContent,
2017-04-10 14:19:27 +02:00
expectedBodyContains: "",
},
2017-12-21 07:50:59 +01:00
"returns error if there is an S3 error": {
2018-05-22 22:56:01 +02:00
removeObjectFunc: func(string, string) error {
2018-04-24 22:35:21 +02:00
return errors.New("mocked S3 error")
},
2017-04-09 16:28:57 +02:00
expectedStatusCode: http.StatusInternalServerError,
expectedBodyContains: http.StatusText(http.StatusInternalServerError),
},
}
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)
2018-04-24 22:35:21 +02:00
s3 := &S3Mock{
RemoveObjectFunc: tc.removeObjectFunc,
}
2017-07-31 10:57:22 +02:00
req, err := http.NewRequest(http.MethodDelete, "/api/buckets/bucketName/objects/objectName", nil)
2018-05-22 22:56:01 +02:00
assert.NoError(err)
2017-07-31 10:57:22 +02:00
rr := httptest.NewRecorder()
2018-04-24 22:35:21 +02:00
handler := s3manager.DeleteObjectHandler(s3)
2017-07-31 10:57:22 +02:00
handler.ServeHTTP(rr, req)
2018-05-22 22:56:01 +02:00
assert.Equal(tc.expectedStatusCode, rr.Code)
assert.Contains(rr.Body.String(), tc.expectedBodyContains)
2017-07-31 10:57:22 +02:00
})
}
}