8
"github.com/stretchr/testify/assert"
9
"github.com/stretchr/testify/require"
12
"github.com/aws/aws-sdk-go/aws"
13
"github.com/aws/aws-sdk-go/aws/awserr"
14
"github.com/aws/aws-sdk-go/service/s3"
18
const errMsg = `<Error><Code>ErrorCode</Code><Message>message body</Message><RequestId>requestID</RequestId><HostId>hostID=</HostId></Error>`
20
var lastModifiedTime = time.Date(2009, 11, 23, 0, 0, 0, 0, time.UTC)
22
func TestCopyObjectNoError(t *testing.T) {
24
<?xml version="1.0" encoding="UTF-8"?>
25
<CopyObjectResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><LastModified>2009-11-23T0:00:00Z</LastModified><ETag>"1da64c7f13d1e8dbeaea40b905fd586c"</ETag></CopyObjectResult>`
27
res, err := newCopyTestSvc(successMsg).CopyObject(&s3.CopyObjectInput{
28
Bucket: aws.String("bucketname"),
29
CopySource: aws.String("bucketname/exists.txt"),
30
Key: aws.String("destination.txt"),
33
require.NoError(t, err)
35
assert.Equal(t, fmt.Sprintf(`%q`, "1da64c7f13d1e8dbeaea40b905fd586c"), *res.CopyObjectResult.ETag)
36
assert.Equal(t, lastModifiedTime, *res.CopyObjectResult.LastModified)
39
func TestCopyObjectError(t *testing.T) {
40
_, err := newCopyTestSvc(errMsg).CopyObject(&s3.CopyObjectInput{
41
Bucket: aws.String("bucketname"),
42
CopySource: aws.String("bucketname/doesnotexist.txt"),
43
Key: aws.String("destination.txt"),
47
e := err.(awserr.Error)
49
assert.Equal(t, "ErrorCode", e.Code())
50
assert.Equal(t, "message body", e.Message())
53
func TestUploadPartCopySuccess(t *testing.T) {
55
<?xml version="1.0" encoding="UTF-8"?>
56
<UploadPartCopyResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><LastModified>2009-11-23T0:00:00Z</LastModified><ETag>"1da64c7f13d1e8dbeaea40b905fd586c"</ETag></CopyObjectResult>`
58
res, err := newCopyTestSvc(successMsg).UploadPartCopy(&s3.UploadPartCopyInput{
59
Bucket: aws.String("bucketname"),
60
CopySource: aws.String("bucketname/doesnotexist.txt"),
61
Key: aws.String("destination.txt"),
62
PartNumber: aws.Int64(0),
63
UploadId: aws.String("uploadID"),
66
require.NoError(t, err)
68
assert.Equal(t, fmt.Sprintf(`%q`, "1da64c7f13d1e8dbeaea40b905fd586c"), *res.CopyPartResult.ETag)
69
assert.Equal(t, lastModifiedTime, *res.CopyPartResult.LastModified)
72
func TestUploadPartCopyError(t *testing.T) {
73
_, err := newCopyTestSvc(errMsg).UploadPartCopy(&s3.UploadPartCopyInput{
74
Bucket: aws.String("bucketname"),
75
CopySource: aws.String("bucketname/doesnotexist.txt"),
76
Key: aws.String("destination.txt"),
77
PartNumber: aws.Int64(0),
78
UploadId: aws.String("uploadID"),
82
e := err.(awserr.Error)
84
assert.Equal(t, "ErrorCode", e.Code())
85
assert.Equal(t, "message body", e.Message())
88
func TestCompleteMultipartUploadSuccess(t *testing.T) {
90
<?xml version="1.0" encoding="UTF-8"?>
91
<CompleteMultipartUploadResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Location>locationName</Location><Bucket>bucketName</Bucket><Key>keyName</Key><ETag>"etagVal"</ETag></CompleteMultipartUploadResult>`
92
res, err := newCopyTestSvc(successMsg).CompleteMultipartUpload(&s3.CompleteMultipartUploadInput{
93
Bucket: aws.String("bucketname"),
94
Key: aws.String("key"),
95
UploadId: aws.String("uploadID"),
98
require.NoError(t, err)
100
assert.Equal(t, `"etagVal"`, *res.ETag)
101
assert.Equal(t, "bucketName", *res.Bucket)
102
assert.Equal(t, "keyName", *res.Key)
103
assert.Equal(t, "locationName", *res.Location)
106
func TestCompleteMultipartUploadError(t *testing.T) {
107
_, err := newCopyTestSvc(errMsg).CompleteMultipartUpload(&s3.CompleteMultipartUploadInput{
108
Bucket: aws.String("bucketname"),
109
Key: aws.String("key"),
110
UploadId: aws.String("uploadID"),
113
require.Error(t, err)
114
e := err.(awserr.Error)
116
assert.Equal(t, "ErrorCode", e.Code())
117
assert.Equal(t, "message body", e.Message())
120
func newCopyTestSvc(errMsg string) *s3.S3 {
121
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
122
http.Error(w, errMsg, http.StatusOK)
124
return s3.New(aws.NewConfig().
125
WithEndpoint(server.URL).
126
WithDisableSSL(true).
128
WithS3ForcePathStyle(true))