|
| 1 | +package cos |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/xml" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "reflect" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +func TestBucketService_GetOrigin(t *testing.T) { |
| 13 | + setup() |
| 14 | + defer teardown() |
| 15 | + |
| 16 | + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 17 | + testMethod(t, r, "GET") |
| 18 | + vs := values{ |
| 19 | + "origin": "", |
| 20 | + } |
| 21 | + testFormValues(t, r, vs) |
| 22 | + fmt.Fprint(w, `<OriginConfiguration> |
| 23 | + <OriginRule> |
| 24 | + <RulePriority>1</RulePriority> |
| 25 | + <OriginType>Mirror</OriginType> |
| 26 | + <OriginCondition> |
| 27 | + <HTTPStatusCode>404</HTTPStatusCode> |
| 28 | + <Prefix></Prefix> |
| 29 | + </OriginCondition> |
| 30 | + <OriginParameter> |
| 31 | + <Protocol>HTTP</Protocol> |
| 32 | + <FollowQueryString>true</FollowQueryString> |
| 33 | + <HttpHeader> |
| 34 | + <NewHttpHeaders> |
| 35 | + <Header> |
| 36 | + <Key>x-cos</Key> |
| 37 | + <Value>exampleHeader</Value> |
| 38 | + </Header> |
| 39 | + </NewHttpHeaders> |
| 40 | + <FollowHttpHeaders> |
| 41 | + <Header> |
| 42 | + <Key>exampleHeaderKey</Key> |
| 43 | + </Header> |
| 44 | + </FollowHttpHeaders> |
| 45 | + </HttpHeader> |
| 46 | + <FollowRedirection>true</FollowRedirection> |
| 47 | + <HttpRedirectCode>302</HttpRedirectCode> |
| 48 | + </OriginParameter> |
| 49 | + <OriginInfo> |
| 50 | + <HostInfo> |
| 51 | + <HostName>examplebucket-1250000000.cos.ap-shanghai.myqcloud.com</HostName> |
| 52 | + </HostInfo> |
| 53 | + </OriginInfo> |
| 54 | + </OriginRule> |
| 55 | + </OriginConfiguration> |
| 56 | + `) |
| 57 | + }) |
| 58 | + |
| 59 | + res, _, err := client.Bucket.GetOrigin(context.Background()) |
| 60 | + if err != nil { |
| 61 | + t.Fatalf("Bucket.GetOrigin returned error %v", err) |
| 62 | + } |
| 63 | + |
| 64 | + want := &BucketGetOriginResult{ |
| 65 | + XMLName: xml.Name{Local: "OriginConfiguration"}, |
| 66 | + Rule: []BucketOriginRule{ |
| 67 | + { |
| 68 | + OriginType: "Mirror", |
| 69 | + RulePriority: 1, |
| 70 | + OriginCondition: &BucketOriginCondition{ |
| 71 | + HTTPStatusCode: "404", |
| 72 | + }, |
| 73 | + OriginParameter: &BucketOriginParameter{ |
| 74 | + Protocol: "HTTP", |
| 75 | + FollowQueryString: true, |
| 76 | + HttpHeader: &BucketOriginHttpHeader{ |
| 77 | + FollowHttpHeaders: []OriginHttpHeader{ |
| 78 | + { |
| 79 | + Key: "exampleHeaderKey", |
| 80 | + }, |
| 81 | + }, |
| 82 | + NewHttpHeaders: []OriginHttpHeader{ |
| 83 | + { |
| 84 | + Key: "x-cos", |
| 85 | + Value: "exampleHeader", |
| 86 | + }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + FollowRedirection: true, |
| 90 | + HttpRedirectCode: "302", |
| 91 | + }, |
| 92 | + OriginInfo: &BucketOriginInfo{ |
| 93 | + HostInfo: "examplebucket-1250000000.cos.ap-shanghai.myqcloud.com", |
| 94 | + }, |
| 95 | + }, |
| 96 | + }, |
| 97 | + } |
| 98 | + |
| 99 | + if !reflect.DeepEqual(res, want) { |
| 100 | + t.Errorf("Bucket.GetOrigin returned %+v, want %+v", res, want) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func TestBucketService_PutOrigin(t *testing.T) { |
| 105 | + setup() |
| 106 | + defer teardown() |
| 107 | + |
| 108 | + opt := &BucketPutOriginOptions{ |
| 109 | + XMLName: xml.Name{Local: "OriginConfiguration"}, |
| 110 | + Rule: []BucketOriginRule{ |
| 111 | + { |
| 112 | + OriginType: "Mirror", |
| 113 | + RulePriority: 1, |
| 114 | + OriginCondition: &BucketOriginCondition{ |
| 115 | + HTTPStatusCode: "404", |
| 116 | + }, |
| 117 | + OriginParameter: &BucketOriginParameter{ |
| 118 | + Protocol: "HTTP", |
| 119 | + FollowQueryString: true, |
| 120 | + HttpHeader: &BucketOriginHttpHeader{ |
| 121 | + FollowHttpHeaders: []OriginHttpHeader{ |
| 122 | + { |
| 123 | + Key: "exampleHeaderKey", |
| 124 | + }, |
| 125 | + }, |
| 126 | + NewHttpHeaders: []OriginHttpHeader{ |
| 127 | + { |
| 128 | + Key: "x-cos", |
| 129 | + Value: "exampleHeader", |
| 130 | + }, |
| 131 | + }, |
| 132 | + }, |
| 133 | + FollowRedirection: true, |
| 134 | + HttpRedirectCode: "302", |
| 135 | + }, |
| 136 | + OriginInfo: &BucketOriginInfo{ |
| 137 | + HostInfo: "examplebucket-1250000000.cos.ap-shanghai.myqcloud.com", |
| 138 | + }, |
| 139 | + }, |
| 140 | + }, |
| 141 | + } |
| 142 | + |
| 143 | + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 144 | + testMethod(t, r, "PUT") |
| 145 | + vs := values{ |
| 146 | + "origin": "", |
| 147 | + } |
| 148 | + testFormValues(t, r, vs) |
| 149 | + |
| 150 | + body := new(BucketPutOriginOptions) |
| 151 | + xml.NewDecoder(r.Body).Decode(body) |
| 152 | + want := opt |
| 153 | + want.XMLName = xml.Name{Local: "OriginConfiguration"} |
| 154 | + if !reflect.DeepEqual(body, want) { |
| 155 | + t.Errorf("Bucket.PutOrigin request\n body: %+v\n, want %+v\n", body, want) |
| 156 | + } |
| 157 | + }) |
| 158 | + |
| 159 | + _, err := client.Bucket.PutOrigin(context.Background(), opt) |
| 160 | + if err != nil { |
| 161 | + t.Fatalf("Bucket.PutOrigin returned error: %v", err) |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +func TestBucketService_DeleteOrigin(t *testing.T) { |
| 166 | + setup() |
| 167 | + defer teardown() |
| 168 | + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 169 | + testMethod(t, r, http.MethodDelete) |
| 170 | + vs := values{ |
| 171 | + "origin": "", |
| 172 | + } |
| 173 | + testFormValues(t, r, vs) |
| 174 | + w.WriteHeader(http.StatusNoContent) |
| 175 | + }) |
| 176 | + _, err := client.Bucket.DeleteOrigin(context.Background()) |
| 177 | + if err != nil { |
| 178 | + t.Fatalf("Bucket.DeleteOrigin returned error: %v", err) |
| 179 | + } |
| 180 | +} |
0 commit comments