s3manager-web/get-object_test.go

58 lines
1.3 KiB
Go
Raw Normal View History

2017-04-03 23:52:41 +02:00
package main
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
)
func TestGetObjectHandler(t *testing.T) {
assert := assert.New(t)
tests := map[string]struct {
s3 S3Client
bucketName string
objectName string
expectedStatusCode int
expectedBodyCountains string
}{
"s3 error": {
s3: &S3ClientMock{
Err: errors.New("mocked S3 error"),
},
bucketName: "testBucket",
objectName: "testObject",
expectedStatusCode: http.StatusInternalServerError,
2017-04-09 16:28:57 +02:00
expectedBodyCountains: http.StatusText(http.StatusInternalServerError),
2017-04-03 23:52:41 +02:00
},
}
2017-04-07 12:51:23 +02:00
for tcID, tc := range tests {
2017-04-03 23:52:41 +02:00
r := mux.NewRouter()
r.
2017-04-07 08:59:24 +02:00
Methods(http.MethodGet).
2017-04-03 23:52:41 +02:00
Path("/buckets/{bucketName}/objects/{objectName}").
Handler(GetObjectHandler(tc.s3))
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)
2017-04-07 12:51:23 +02:00
assert.NoError(err, tcID)
2017-04-03 23:52:41 +02:00
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
2017-04-07 12:51:23 +02:00
assert.NoError(err, tcID)
2017-04-03 23:52:41 +02:00
2017-04-07 12:51:23 +02:00
assert.Equal(tc.expectedStatusCode, resp.StatusCode, tcID)
assert.Contains(string(body), tc.expectedBodyCountains, tcID)
2017-04-03 23:52:41 +02:00
}
}