~ubuntu-branches/debian/stretch/golang-github-aws-aws-sdk-go/stretch

« back to all changes in this revision

Viewing changes to service/s3/statusok_error_test.go

  • Committer: Package Import Robot
  • Author(s): Dmitry Smirnov
  • Date: 2015-09-29 12:34:07 UTC
  • Revision ID: package-import@ubuntu.com-20150929123407-7xmll3gdhvb9zh2l
Tags: upstream-0.9.9+dfsg
ImportĀ upstreamĀ versionĀ 0.9.9+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package s3_test
 
2
 
 
3
import (
 
4
        "net/http"
 
5
        "net/http/httptest"
 
6
        "testing"
 
7
 
 
8
        "github.com/stretchr/testify/assert"
 
9
        "github.com/stretchr/testify/require"
 
10
 
 
11
        "fmt"
 
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"
 
15
        "time"
 
16
)
 
17
 
 
18
const errMsg = `<Error><Code>ErrorCode</Code><Message>message body</Message><RequestId>requestID</RequestId><HostId>hostID=</HostId></Error>`
 
19
 
 
20
var lastModifiedTime = time.Date(2009, 11, 23, 0, 0, 0, 0, time.UTC)
 
21
 
 
22
func TestCopyObjectNoError(t *testing.T) {
 
23
        const successMsg = `
 
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>&quot;1da64c7f13d1e8dbeaea40b905fd586c&quot;</ETag></CopyObjectResult>`
 
26
 
 
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"),
 
31
        })
 
32
 
 
33
        require.NoError(t, err)
 
34
 
 
35
        assert.Equal(t, fmt.Sprintf(`%q`, "1da64c7f13d1e8dbeaea40b905fd586c"), *res.CopyObjectResult.ETag)
 
36
        assert.Equal(t, lastModifiedTime, *res.CopyObjectResult.LastModified)
 
37
}
 
38
 
 
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"),
 
44
        })
 
45
 
 
46
        require.Error(t, err)
 
47
        e := err.(awserr.Error)
 
48
 
 
49
        assert.Equal(t, "ErrorCode", e.Code())
 
50
        assert.Equal(t, "message body", e.Message())
 
51
}
 
52
 
 
53
func TestUploadPartCopySuccess(t *testing.T) {
 
54
        const successMsg = `
 
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>&quot;1da64c7f13d1e8dbeaea40b905fd586c&quot;</ETag></CopyObjectResult>`
 
57
 
 
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"),
 
64
        })
 
65
 
 
66
        require.NoError(t, err)
 
67
 
 
68
        assert.Equal(t, fmt.Sprintf(`%q`, "1da64c7f13d1e8dbeaea40b905fd586c"), *res.CopyPartResult.ETag)
 
69
        assert.Equal(t, lastModifiedTime, *res.CopyPartResult.LastModified)
 
70
}
 
71
 
 
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"),
 
79
        })
 
80
 
 
81
        require.Error(t, err)
 
82
        e := err.(awserr.Error)
 
83
 
 
84
        assert.Equal(t, "ErrorCode", e.Code())
 
85
        assert.Equal(t, "message body", e.Message())
 
86
}
 
87
 
 
88
func TestCompleteMultipartUploadSuccess(t *testing.T) {
 
89
        const successMsg = `
 
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"),
 
96
        })
 
97
 
 
98
        require.NoError(t, err)
 
99
 
 
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)
 
104
}
 
105
 
 
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"),
 
111
        })
 
112
 
 
113
        require.Error(t, err)
 
114
        e := err.(awserr.Error)
 
115
 
 
116
        assert.Equal(t, "ErrorCode", e.Code())
 
117
        assert.Equal(t, "message body", e.Message())
 
118
}
 
119
 
 
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)
 
123
        }))
 
124
        return s3.New(aws.NewConfig().
 
125
                WithEndpoint(server.URL).
 
126
                WithDisableSSL(true).
 
127
                WithMaxRetries(0).
 
128
                WithS3ForcePathStyle(true))
 
129
}