2019-02-27 08:21:37 +00:00
|
|
|
package s3api
|
|
|
|
|
|
|
|
import (
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
|
2022-08-15 07:30:19 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-02-27 08:21:37 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestListObjectsHandler(t *testing.T) {
|
|
|
|
|
|
|
|
// https://docs.aws.amazon.com/AmazonS3/latest/API/v2-RESTBucketGET.html
|
|
|
|
|
|
|
|
expected := `<?xml version="1.0" encoding="UTF-8"?>
|
2019-04-02 06:59:31 +00:00
|
|
|
<ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Name>test_container</Name><Prefix></Prefix><Marker></Marker><MaxKeys>1000</MaxKeys><IsTruncated>false</IsTruncated><Contents><Key>1.zip</Key><ETag>"4397da7a7649e8085de9916c240e8166"</ETag><Size>1234567</Size><Owner><ID>65a011niqo39cdf8ec533ec3d1ccaafsa932</ID></Owner><StorageClass>STANDARD</StorageClass><LastModified>2011-04-09T12:34:49Z</LastModified></Contents></ListBucketResult>`
|
2019-02-27 08:21:37 +00:00
|
|
|
|
|
|
|
response := ListBucketResult{
|
|
|
|
Name: "test_container",
|
|
|
|
Prefix: "",
|
|
|
|
Marker: "",
|
|
|
|
NextMarker: "",
|
|
|
|
MaxKeys: 1000,
|
|
|
|
IsTruncated: false,
|
|
|
|
Contents: []ListEntry{{
|
|
|
|
Key: "1.zip",
|
|
|
|
LastModified: time.Date(2011, 4, 9, 12, 34, 49, 0, time.UTC),
|
|
|
|
ETag: "\"4397da7a7649e8085de9916c240e8166\"",
|
|
|
|
Size: 1234567,
|
|
|
|
Owner: CanonicalUser{
|
|
|
|
ID: "65a011niqo39cdf8ec533ec3d1ccaafsa932",
|
|
|
|
},
|
|
|
|
StorageClass: "STANDARD",
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
2021-06-11 04:50:21 +00:00
|
|
|
encoded := string(s3err.EncodeXMLResponse(response))
|
2019-02-27 08:21:37 +00:00
|
|
|
if encoded != expected {
|
|
|
|
t.Errorf("unexpected output: %s\nexpecting:%s", encoded, expected)
|
|
|
|
}
|
|
|
|
}
|
2022-08-15 07:30:19 +00:00
|
|
|
|
|
|
|
func Test_normalizePrefixMarker(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
prefix string
|
|
|
|
marker string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
wantAlignedDir string
|
|
|
|
wantAlignedPrefix string
|
|
|
|
wantAlignedMarker string
|
|
|
|
}{
|
|
|
|
{"prefix is a directory",
|
|
|
|
args{"/parentDir/data/",
|
|
|
|
""},
|
2023-04-12 07:08:13 +00:00
|
|
|
"parentDir",
|
|
|
|
"data",
|
2022-08-15 07:30:19 +00:00
|
|
|
"",
|
|
|
|
},
|
|
|
|
{"normal case",
|
|
|
|
args{"/parentDir/data/0",
|
|
|
|
"parentDir/data/0e/0e149049a2137b0cc12e"},
|
|
|
|
"parentDir/data",
|
|
|
|
"0",
|
|
|
|
"0e/0e149049a2137b0cc12e",
|
|
|
|
},
|
|
|
|
{"empty prefix",
|
|
|
|
args{"",
|
|
|
|
"parentDir/data/0e/0e149049a2137b0cc12e"},
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"parentDir/data/0e/0e149049a2137b0cc12e",
|
|
|
|
},
|
|
|
|
{"empty directory",
|
|
|
|
args{"parent",
|
|
|
|
"parentDir/data/0e/0e149049a2137b0cc12e"},
|
|
|
|
"",
|
|
|
|
"parent",
|
|
|
|
"parentDir/data/0e/0e149049a2137b0cc12e",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
gotAlignedDir, gotAlignedPrefix, gotAlignedMarker := normalizePrefixMarker(tt.args.prefix, tt.args.marker)
|
|
|
|
assert.Equalf(t, tt.wantAlignedDir, gotAlignedDir, "normalizePrefixMarker(%v, %v)", tt.args.prefix, tt.args.marker)
|
|
|
|
assert.Equalf(t, tt.wantAlignedPrefix, gotAlignedPrefix, "normalizePrefixMarker(%v, %v)", tt.args.prefix, tt.args.marker)
|
|
|
|
assert.Equalf(t, tt.wantAlignedMarker, gotAlignedMarker, "normalizePrefixMarker(%v, %v)", tt.args.prefix, tt.args.marker)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|