package cos
import (
"context"
"encoding/xml"
"fmt"
"net/http"
"reflect"
"testing"
)
func TestBucketService_Get(t *testing.T) {
setup()
defer teardown()
opt := &BucketGetOptions{
Prefix: "test",
MaxKeys: 2,
}
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
vs := values{
"prefix": "test",
"max-keys": "2",
}
testFormValues(t, r, vs)
fmt.Fprint(w, `
test-1253846586
test
2
true
test/delete.txt
test/
2017-06-09T16:32:25.000Z
""
0
1253846586
STANDARD
test/anonymous_get.go
2017-06-17T15:09:26.000Z
"5b7236085f08b3818bfa40b03c946dcc"
8
1253846586
STANDARD
`)
})
ref, _, err := client.Bucket.Get(context.Background(), opt)
if err != nil {
t.Fatalf("Bucket.Get returned error: %v", err)
}
want := &BucketGetResult{
XMLName: xml.Name{Local: "ListBucketResult"},
Name: "test-1253846586",
Prefix: "test",
MaxKeys: 2,
IsTruncated: true,
NextMarker: "test/delete.txt",
Contents: []Object{
{
Key: "test/",
LastModified: "2017-06-09T16:32:25.000Z",
ETag: "\"\"",
Size: 0,
Owner: &Owner{
ID: "1253846586",
},
StorageClass: "STANDARD",
},
{
Key: "test/anonymous_get.go",
LastModified: "2017-06-17T15:09:26.000Z",
ETag: "\"5b7236085f08b3818bfa40b03c946dcc\"",
Size: 8,
Owner: &Owner{
ID: "1253846586",
},
StorageClass: "STANDARD",
},
},
}
if !reflect.DeepEqual(ref, want) {
t.Errorf("Bucket.Get returned %+v, want %+v", ref, want)
}
}
func TestBucketService_Put(t *testing.T) {
setup()
defer teardown()
opt := &BucketPutOptions{
XCosACL: "public-read",
}
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
v := new(BucketPutTaggingOptions)
xml.NewDecoder(r.Body).Decode(v)
testMethod(t, r, "PUT")
testHeader(t, r, "x-cos-acl", "public-read")
})
_, err := client.Bucket.Put(context.Background(), opt)
if err != nil {
t.Fatalf("Bucket.Put returned error: %v", err)
}
}
func TestBucketService_Delete(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodDelete)
w.WriteHeader(http.StatusNoContent)
})
_, err := client.Bucket.Delete(context.Background())
if err != nil {
t.Fatalf("Bucket.Delete returned error: %v", err)
}
}
func TestBucketService_Head(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodHead)
w.WriteHeader(http.StatusOK)
})
_, err := client.Bucket.Head(context.Background())
if err != nil {
t.Fatalf("Bucket.Head returned error: %v", err)
}
}
func TestBucketService_GetObjectVersions(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
w.WriteHeader(http.StatusOK)
vs := values{
"versions": "",
"delimiter": "/",
}
testFormValues(t, r, vs)
fmt.Fprint(w, `
examplebucket-1250000000
1000
false
/
example-folder-1/
example-folder-2/
example-object-1.jpg
MTg0NDUxNzgxMjEzNTU3NTk1Mjg
true
2019-08-16T10:45:53.000Z
"5d1143df07a17b23320d0da161e2819e"
30
STANDARD
1250000000
1250000000
example-object-1.jpg
MTg0NDUxNzgxMjEzNjE1OTcxMzM
false
2019-08-16T10:45:47.000Z
1250000000
1250000000
`)
})
want := &BucketGetObjectVersionsResult{
XMLName: xml.Name{Local: "ListVersionsResult"},
Name: "examplebucket-1250000000",
MaxKeys: 1000,
IsTruncated: false,
Delimiter: "/",
CommonPrefixes: []string{
"example-folder-1/",
"example-folder-2/",
},
Version: []ListVersionsResultVersion{
{
Key: "example-object-1.jpg",
VersionId: "MTg0NDUxNzgxMjEzNTU3NTk1Mjg",
IsLatest: true,
LastModified: "2019-08-16T10:45:53.000Z",
ETag: "\"5d1143df07a17b23320d0da161e2819e\"",
Size: 30,
StorageClass: "STANDARD",
Owner: &Owner{
ID: "1250000000",
DisplayName: "1250000000",
},
},
},
DeleteMarker: []ListVersionsResultDeleteMarker{
{
Key: "example-object-1.jpg",
VersionId: "MTg0NDUxNzgxMjEzNjE1OTcxMzM",
IsLatest: false,
LastModified: "2019-08-16T10:45:47.000Z",
Owner: &Owner{
ID: "1250000000",
DisplayName: "1250000000",
},
},
},
}
opt := &BucketGetObjectVersionsOptions{
Delimiter: "/",
}
res, _, err := client.Bucket.GetObjectVersions(context.Background(), opt)
if err != nil {
t.Fatalf("Bucket.GetObjectVersions returned error: %v", err)
}
if !reflect.DeepEqual(res, want) {
t.Errorf("Bucket.GetObjectVersions returned\n%+v\nwant\n%+v", res, want)
}
}