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

65 lines
1.7 KiB
Go
Raw Normal View History

2017-05-08 23:07:07 +02:00
package s3manager_test
2017-04-03 23:52:41 +02:00
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
2018-05-31 18:28:55 +02:00
"strings"
2017-04-03 23:52:41 +02:00
"testing"
2018-03-14 21:53:35 +01:00
"github.com/mastertinner/s3manager/internal/app/s3manager"
2018-08-12 15:13:07 +02:00
"github.com/mastertinner/s3manager/internal/app/s3manager/mocks"
2018-05-31 18:28:55 +02:00
"github.com/matryer/is"
2018-05-31 16:10:41 +02:00
"github.com/matryer/way"
2018-04-24 22:35:21 +02:00
minio "github.com/minio/minio-go"
2017-04-03 23:52:41 +02:00
)
2018-05-31 16:10:41 +02:00
func TestHandleGetObject(t *testing.T) {
2017-05-08 23:07:07 +02:00
cases := map[string]struct {
2018-04-24 22:35:21 +02:00
getObjectFunc func(string, string, minio.GetObjectOptions) (*minio.Object, error)
2017-04-18 15:43:13 +02:00
bucketName string
objectName string
expectedStatusCode int
expectedBodyContains string
2017-04-03 23:52:41 +02:00
}{
2017-12-21 07:50:59 +01:00
"returns error if there is an S3 error": {
2018-05-22 22:56:01 +02:00
getObjectFunc: func(string, string, minio.GetObjectOptions) (*minio.Object, error) {
2018-04-24 22:35:21 +02:00
return nil, errors.New("mocked S3 error")
2017-04-03 23:52:41 +02:00
},
2017-04-18 15:43:13 +02:00
bucketName: "testBucket",
objectName: "testObject",
expectedStatusCode: http.StatusInternalServerError,
expectedBodyContains: http.StatusText(http.StatusInternalServerError),
2017-04-03 23:52:41 +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) {
2018-05-31 18:28:55 +02:00
is := is.New(t)
2017-12-21 07:50:59 +01:00
2018-08-12 15:13:07 +02:00
s3 := &mocks.S3Mock{
2018-04-24 22:35:21 +02:00
GetObjectFunc: tc.getObjectFunc,
}
2018-05-31 16:10:41 +02:00
r := way.NewRouter()
r.Handle(http.MethodGet, "/buckets/:bucketName/objects/:objectName", s3manager.HandleGetObject(s3))
2017-07-31 10:57:22 +02:00
ts := httptest.NewServer(r)
defer ts.Close()
url := fmt.Sprintf("%s/buckets/%s/objects/%s", ts.URL, tc.bucketName, tc.objectName)
resp, err := http.Get(url)
2018-05-31 18:28:55 +02:00
is.NoErr(err)
defer resp.Body.Close()
2017-04-03 23:52:41 +02:00
2017-07-31 10:57:22 +02:00
body, err := ioutil.ReadAll(resp.Body)
2018-05-31 18:28:55 +02:00
is.NoErr(err)
2017-04-03 23:52:41 +02:00
2018-05-31 18:28:55 +02:00
is.Equal(tc.expectedStatusCode, resp.StatusCode) // status code
is.True(strings.Contains(string(body), tc.expectedBodyContains)) // body
2017-07-31 10:57:22 +02:00
})
2017-04-03 23:52:41 +02:00
}
}