2019-02-27 16:21:37 +08:00
|
|
|
package s3api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/service/s3"
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
|
2022-03-23 16:05:14 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-02-27 16:21:37 +08:00
|
|
|
"testing"
|
2020-09-12 05:53:50 +08:00
|
|
|
"time"
|
2019-02-27 16:21:37 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestInitiateMultipartUploadResult(t *testing.T) {
|
|
|
|
|
|
|
|
expected := `<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<InitiateMultipartUploadResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Bucket>example-bucket</Bucket><Key>example-object</Key><UploadId>VXBsb2FkIElEIGZvciA2aWWpbmcncyBteS1tb3ZpZS5tMnRzIHVwbG9hZA</UploadId></InitiateMultipartUploadResult>`
|
|
|
|
response := &InitiateMultipartUploadResult{
|
|
|
|
CreateMultipartUploadOutput: s3.CreateMultipartUploadOutput{
|
|
|
|
Bucket: aws.String("example-bucket"),
|
|
|
|
Key: aws.String("example-object"),
|
|
|
|
UploadId: aws.String("VXBsb2FkIElEIGZvciA2aWWpbmcncyBteS1tb3ZpZS5tMnRzIHVwbG9hZA"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-06-11 12:50:21 +08:00
|
|
|
encoded := string(s3err.EncodeXMLResponse(response))
|
2019-02-27 16:21:37 +08:00
|
|
|
if encoded != expected {
|
|
|
|
t.Errorf("unexpected output: %s\nexpecting:%s", encoded, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-09-12 05:53:50 +08:00
|
|
|
|
|
|
|
func TestListPartsResult(t *testing.T) {
|
|
|
|
|
|
|
|
expected := `<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<ListPartsResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Part><ETag>"12345678"</ETag><LastModified>1970-01-01T00:00:00Z</LastModified><PartNumber>1</PartNumber><Size>123</Size></Part></ListPartsResult>`
|
|
|
|
response := &ListPartsResult{
|
|
|
|
Part: []*s3.Part{
|
|
|
|
{
|
|
|
|
PartNumber: aws.Int64(int64(1)),
|
|
|
|
LastModified: aws.Time(time.Unix(0, 0).UTC()),
|
|
|
|
Size: aws.Int64(int64(123)),
|
|
|
|
ETag: aws.String("\"12345678\""),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-06-11 12:50:21 +08:00
|
|
|
encoded := string(s3err.EncodeXMLResponse(response))
|
2020-09-12 05:53:50 +08:00
|
|
|
if encoded != expected {
|
|
|
|
t.Errorf("unexpected output: %s\nexpecting:%s", encoded, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-03-23 16:05:14 +08:00
|
|
|
|
2024-04-07 01:56:39 +08:00
|
|
|
func Test_parsePartNumber(t *testing.T) {
|
2022-03-23 16:05:14 +08:00
|
|
|
tests := []struct {
|
2024-04-07 01:56:39 +08:00
|
|
|
name string
|
|
|
|
fileName string
|
|
|
|
partNum int
|
2022-03-23 16:05:14 +08:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
"first",
|
2024-04-07 01:56:39 +08:00
|
|
|
"0001_uuid.part",
|
|
|
|
1,
|
2022-03-23 16:05:14 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"second",
|
2024-04-07 01:56:39 +08:00
|
|
|
"0002.part",
|
|
|
|
2,
|
2022-03-23 16:05:14 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2024-04-07 01:56:39 +08:00
|
|
|
partNumber, _ := parsePartNumber(tt.fileName)
|
|
|
|
assert.Equalf(t, tt.partNum, partNumber, "parsePartNumber(%v)", tt.fileName)
|
2022-03-23 16:05:14 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|