-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
jojoliang
committed
Apr 28, 2021
1 parent
4af950f
commit caee386
Showing
10 changed files
with
1,164 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
package cos | ||
|
||
import ( | ||
"context" | ||
"encoding/xml" | ||
"fmt" | ||
"net/http" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestBucketService_GetOrigin(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
|
||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, "GET") | ||
vs := values{ | ||
"origin": "", | ||
} | ||
testFormValues(t, r, vs) | ||
fmt.Fprint(w, `<OriginConfiguration> | ||
<OriginRule> | ||
<RulePriority>1</RulePriority> | ||
<OriginType>Mirror</OriginType> | ||
<OriginCondition> | ||
<HTTPStatusCode>404</HTTPStatusCode> | ||
<Prefix></Prefix> | ||
</OriginCondition> | ||
<OriginParameter> | ||
<Protocol>HTTP</Protocol> | ||
<FollowQueryString>true</FollowQueryString> | ||
<HttpHeader> | ||
<NewHttpHeaders> | ||
<Header> | ||
<Key>x-cos</Key> | ||
<Value>exampleHeader</Value> | ||
</Header> | ||
</NewHttpHeaders> | ||
<FollowHttpHeaders> | ||
<Header> | ||
<Key>exampleHeaderKey</Key> | ||
</Header> | ||
</FollowHttpHeaders> | ||
</HttpHeader> | ||
<FollowRedirection>true</FollowRedirection> | ||
<HttpRedirectCode>302</HttpRedirectCode> | ||
</OriginParameter> | ||
<OriginInfo> | ||
<HostInfo> | ||
<HostName>examplebucket-1250000000.cos.ap-shanghai.myqcloud.com</HostName> | ||
</HostInfo> | ||
</OriginInfo> | ||
</OriginRule> | ||
</OriginConfiguration> | ||
`) | ||
}) | ||
|
||
res, _, err := client.Bucket.GetOrigin(context.Background()) | ||
if err != nil { | ||
t.Fatalf("Bucket.GetOrigin returned error %v", err) | ||
} | ||
|
||
want := &BucketGetOriginResult{ | ||
XMLName: xml.Name{Local: "OriginConfiguration"}, | ||
Rule: []BucketOriginRule{ | ||
{ | ||
OriginType: "Mirror", | ||
RulePriority: 1, | ||
OriginCondition: &BucketOriginCondition{ | ||
HTTPStatusCode: "404", | ||
}, | ||
OriginParameter: &BucketOriginParameter{ | ||
Protocol: "HTTP", | ||
FollowQueryString: true, | ||
HttpHeader: &BucketOriginHttpHeader{ | ||
FollowHttpHeaders: []OriginHttpHeader{ | ||
{ | ||
Key: "exampleHeaderKey", | ||
}, | ||
}, | ||
NewHttpHeaders: []OriginHttpHeader{ | ||
{ | ||
Key: "x-cos", | ||
Value: "exampleHeader", | ||
}, | ||
}, | ||
}, | ||
FollowRedirection: true, | ||
HttpRedirectCode: "302", | ||
}, | ||
OriginInfo: &BucketOriginInfo{ | ||
HostInfo: "examplebucket-1250000000.cos.ap-shanghai.myqcloud.com", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
if !reflect.DeepEqual(res, want) { | ||
t.Errorf("Bucket.GetOrigin returned %+v, want %+v", res, want) | ||
} | ||
} | ||
|
||
func TestBucketService_PutOrigin(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
|
||
opt := &BucketPutOriginOptions{ | ||
XMLName: xml.Name{Local: "OriginConfiguration"}, | ||
Rule: []BucketOriginRule{ | ||
{ | ||
OriginType: "Mirror", | ||
RulePriority: 1, | ||
OriginCondition: &BucketOriginCondition{ | ||
HTTPStatusCode: "404", | ||
}, | ||
OriginParameter: &BucketOriginParameter{ | ||
Protocol: "HTTP", | ||
FollowQueryString: true, | ||
HttpHeader: &BucketOriginHttpHeader{ | ||
FollowHttpHeaders: []OriginHttpHeader{ | ||
{ | ||
Key: "exampleHeaderKey", | ||
}, | ||
}, | ||
NewHttpHeaders: []OriginHttpHeader{ | ||
{ | ||
Key: "x-cos", | ||
Value: "exampleHeader", | ||
}, | ||
}, | ||
}, | ||
FollowRedirection: true, | ||
HttpRedirectCode: "302", | ||
}, | ||
OriginInfo: &BucketOriginInfo{ | ||
HostInfo: "examplebucket-1250000000.cos.ap-shanghai.myqcloud.com", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, "PUT") | ||
vs := values{ | ||
"origin": "", | ||
} | ||
testFormValues(t, r, vs) | ||
|
||
body := new(BucketPutOriginOptions) | ||
xml.NewDecoder(r.Body).Decode(body) | ||
want := opt | ||
want.XMLName = xml.Name{Local: "OriginConfiguration"} | ||
if !reflect.DeepEqual(body, want) { | ||
t.Errorf("Bucket.PutOrigin request\n body: %+v\n, want %+v\n", body, want) | ||
} | ||
}) | ||
|
||
_, err := client.Bucket.PutOrigin(context.Background(), opt) | ||
if err != nil { | ||
t.Fatalf("Bucket.PutOrigin returned error: %v", err) | ||
} | ||
} | ||
|
||
func TestBucketService_DeleteOrigin(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, http.MethodDelete) | ||
vs := values{ | ||
"origin": "", | ||
} | ||
testFormValues(t, r, vs) | ||
w.WriteHeader(http.StatusNoContent) | ||
}) | ||
_, err := client.Bucket.DeleteOrigin(context.Background()) | ||
if err != nil { | ||
t.Fatalf("Bucket.DeleteOrigin returned error: %v", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package cos | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestBucketService_GetPolicy(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
|
||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, "GET") | ||
vs := values{ | ||
"policy": "", | ||
} | ||
testFormValues(t, r, vs) | ||
fmt.Fprint(w, `{ | ||
"Statement": [ | ||
{ | ||
"Principal": { | ||
"qcs": [ | ||
"qcs::cam::uin/100000000001:uin/100000000011" | ||
] | ||
}, | ||
"Effect": "allow", | ||
"Action": [ | ||
"name/cos:GetBucket" | ||
], | ||
"Resource": [ | ||
"qcs::cos:ap-guangzhou:uid/1250000000:examplebucket-1250000000/*" | ||
] | ||
} | ||
], | ||
"version": "2.0" | ||
}`) | ||
}) | ||
|
||
res, _, err := client.Bucket.GetPolicy(context.Background()) | ||
if err != nil { | ||
t.Fatalf("Bucket.GetPolicy returned error %v", err) | ||
} | ||
|
||
want := &BucketGetPolicyResult{ | ||
Statement: []BucketStatement{ | ||
{ | ||
Principal: map[string][]string{ | ||
"qcs": []string{"qcs::cam::uin/100000000001:uin/100000000011"}, | ||
}, | ||
Effect: "allow", | ||
Action: []string{"name/cos:GetBucket"}, | ||
Resource: []string{"qcs::cos:ap-guangzhou:uid/1250000000:examplebucket-1250000000/*"}, | ||
}, | ||
}, | ||
Version: "2.0", | ||
} | ||
|
||
if !reflect.DeepEqual(res, want) { | ||
t.Errorf("Bucket.GetPolicy returned %+v, want %+v", res, want) | ||
} | ||
} | ||
|
||
func TestBucketService_PutPolicy(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
|
||
opt := &BucketPutPolicyOptions{ | ||
Statement: []BucketStatement{ | ||
{ | ||
Principal: map[string][]string{ | ||
"qcs": []string{"qcs::cam::uin/100000000001:uin/100000000011"}, | ||
}, | ||
Effect: "allow", | ||
Action: []string{"name/cos:GetBucket"}, | ||
Resource: []string{"qcs::cos:ap-guangzhou:uid/1250000000:examplebucket-1250000000/*"}, | ||
}, | ||
}, | ||
Version: "2.0", | ||
} | ||
|
||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, "PUT") | ||
vs := values{ | ||
"policy": "", | ||
} | ||
testFormValues(t, r, vs) | ||
|
||
body := new(BucketPutPolicyOptions) | ||
json.NewDecoder(r.Body).Decode(body) | ||
want := opt | ||
if !reflect.DeepEqual(body, want) { | ||
t.Errorf("Bucket.PutPolicy request\n body: %+v\n, want %+v\n", body, want) | ||
} | ||
}) | ||
|
||
_, err := client.Bucket.PutPolicy(context.Background(), opt) | ||
if err != nil { | ||
t.Fatalf("Bucket.PutPolicy returned error: %v", err) | ||
} | ||
} | ||
|
||
func TestBucketService_DeletePolicy(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, http.MethodDelete) | ||
vs := values{ | ||
"policy": "", | ||
} | ||
testFormValues(t, r, vs) | ||
w.WriteHeader(http.StatusNoContent) | ||
}) | ||
_, err := client.Bucket.DeletePolicy(context.Background()) | ||
if err != nil { | ||
t.Fatalf("Bucket.DeletePolicy returned error: %v", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.