Skip to content

Commit

Permalink
add ci guetzli && test
Browse files Browse the repository at this point in the history
  • Loading branch information
jojoliang committed Apr 28, 2021
1 parent 4af950f commit caee386
Show file tree
Hide file tree
Showing 10 changed files with 1,164 additions and 22 deletions.
7 changes: 0 additions & 7 deletions batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,6 @@ type BatchCreateJobResult struct {
JobId string `xml:"JobId,omitempty"`
}

func processETag(opt *BatchCreateJobOptions) *BatchCreateJobOptions {
if opt != nil && opt.Manifest != nil && opt.Manifest.Location != nil {
opt.Manifest.Location.ETag = "<ETag>" + opt.Manifest.Location.ETag + "</ETag>"
}
return opt
}

func (s *BatchService) CreateJob(ctx context.Context, opt *BatchCreateJobOptions, headers *BatchRequestHeaders) (*BatchCreateJobResult, *Response, error) {
var res BatchCreateJobResult
sendOpt := sendOptions{
Expand Down
9 changes: 5 additions & 4 deletions bucket_origin.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ type BucketPutOriginOptions struct {
}

type BucketOriginRule struct {
OriginType string `xml:"OriginType"`
OriginCondition *BucketOriginCondition `xml:"OriginCondition"`
OriginParameter *BucketOriginParameter `xml:"OriginParameter"`
OriginInfo *BucketOriginInfo `xml:"OriginInfo"`
RulePriority int `xml:"RulePriority,omitempty"`
OriginType string `xml:"OriginType,omitempty"`
OriginCondition *BucketOriginCondition `xml:"OriginCondition,omitempty"`
OriginParameter *BucketOriginParameter `xml:"OriginParameter,omitempty"`
OriginInfo *BucketOriginInfo `xml:"OriginInfo,omitempty"`
}

type BucketOriginCondition struct {
Expand Down
180 changes: 180 additions & 0 deletions bucket_origin_test.go
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)
}
}
121 changes: 121 additions & 0 deletions bucket_policy_test.go
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)
}
}
40 changes: 40 additions & 0 deletions ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,3 +446,43 @@ func (s *CIService) GenerateQRcodeToFile(ctx context.Context, filePath string, o

return res, resp, err
}

// 开通 Guetzli 压缩 https://cloud.tencent.com/document/product/460/30112
func (s *CIService) PutGuetzli(ctx context.Context) (*Response, error) {
sendOpt := &sendOptions{
baseURL: s.client.BaseURL.CIURL,
uri: "/?guetzli",
method: http.MethodPut,
}
resp, err := s.client.send(ctx, sendOpt)
return resp, err
}

type GetGuetzliResult struct {
XMLName xml.Name `xml:"GuetzliStatus"`
GuetzliStatus string `xml:",chardata"`
}

// 查询 Guetzli 状态 https://cloud.tencent.com/document/product/460/30111
func (s *CIService) GetGuetzli(ctx context.Context) (*GetGuetzliResult, *Response, error) {
var res GetGuetzliResult
sendOpt := &sendOptions{
baseURL: s.client.BaseURL.CIURL,
uri: "/?guetzli",
method: http.MethodGet,
result: &res,
}
resp, err := s.client.send(ctx, sendOpt)
return &res, resp, err
}

// 关闭 Guetzli 压缩 https://cloud.tencent.com/document/product/460/30113
func (s *CIService) DeleteGuetzli(ctx context.Context) (*Response, error) {
sendOpt := &sendOptions{
baseURL: s.client.BaseURL.CIURL,
uri: "/?guetzli",
method: http.MethodDelete,
}
resp, err := s.client.send(ctx, sendOpt)
return resp, err
}

0 comments on commit caee386

Please sign in to comment.