OSDN Git Service

Create ossClient.go (#574)
[bytom/vapor.git] / vendor / github.com / aliyun / aliyun-oss-go-sdk / oss / option_test.go
1 package oss
2
3 import (
4         "net/http"
5
6         . "gopkg.in/check.v1"
7 )
8
9 type OssOptionSuite struct{}
10
11 var _ = Suite(&OssOptionSuite{})
12
13 type optionTestCase struct {
14         option Option
15         key    string
16         value  string
17 }
18
19 var headerTestcases = []optionTestCase{
20         {
21                 option: Meta("User", "baymax"),
22                 key:    "X-Oss-Meta-User",
23                 value:  "baymax",
24         },
25         {
26                 option: ACL(ACLPrivate),
27                 key:    "X-Oss-Acl",
28                 value:  "private",
29         },
30         {
31                 option: ContentType("plain/text"),
32                 key:    "Content-Type",
33                 value:  "plain/text",
34         },
35         {
36                 option: CacheControl("no-cache"),
37                 key:    "Cache-Control",
38                 value:  "no-cache",
39         },
40         {
41                 option: ContentDisposition("Attachment; filename=example.txt"),
42                 key:    "Content-Disposition",
43                 value:  "Attachment; filename=example.txt",
44         },
45         {
46                 option: ContentEncoding("gzip"),
47                 key:    "Content-Encoding",
48                 value:  "gzip",
49         },
50         {
51                 option: Expires(pastDate),
52                 key:    "Expires",
53                 value:  pastDate.Format(http.TimeFormat),
54         },
55         {
56                 option: Range(0, 9),
57                 key:    "Range",
58                 value:  "bytes=0-9",
59         },
60         {
61                 option: Origin("localhost"),
62                 key:    "Origin",
63                 value:  "localhost",
64         },
65         {
66                 option: CopySourceRange(0, 9),
67                 key:    "X-Oss-Copy-Source-Range",
68                 value:  "bytes=0-8",
69         },
70         {
71                 option: IfModifiedSince(pastDate),
72                 key:    "If-Modified-Since",
73                 value:  pastDate.Format(http.TimeFormat),
74         },
75         {
76                 option: IfUnmodifiedSince(futureDate),
77                 key:    "If-Unmodified-Since",
78                 value:  futureDate.Format(http.TimeFormat),
79         },
80         {
81                 option: IfMatch("xyzzy"),
82                 key:    "If-Match",
83                 value:  "xyzzy",
84         },
85         {
86                 option: IfNoneMatch("xyzzy"),
87                 key:    "If-None-Match",
88                 value:  "xyzzy",
89         },
90         {
91                 option: CopySource("bucket_name", "object_name"),
92                 key:    "X-Oss-Copy-Source",
93                 value:  "/bucket_name/object_name",
94         },
95         {
96                 option: CopySourceIfModifiedSince(pastDate),
97                 key:    "X-Oss-Copy-Source-If-Modified-Since",
98                 value:  pastDate.Format(http.TimeFormat),
99         },
100         {
101                 option: CopySourceIfUnmodifiedSince(futureDate),
102                 key:    "X-Oss-Copy-Source-If-Unmodified-Since",
103                 value:  futureDate.Format(http.TimeFormat),
104         },
105         {
106                 option: CopySourceIfMatch("xyzzy"),
107                 key:    "X-Oss-Copy-Source-If-Match",
108                 value:  "xyzzy",
109         },
110         {
111                 option: CopySourceIfNoneMatch("xyzzy"),
112                 key:    "X-Oss-Copy-Source-If-None-Match",
113                 value:  "xyzzy",
114         },
115         {
116                 option: MetadataDirective(MetaCopy),
117                 key:    "X-Oss-Metadata-Directive",
118                 value:  "COPY",
119         },
120         {
121                 option: ServerSideEncryption("AES256"),
122                 key:    "X-Oss-Server-Side-Encryption",
123                 value:  "AES256",
124         },
125         {
126                 option: ObjectACL(ACLPrivate),
127                 key:    "X-Oss-Object-Acl",
128                 value:  "private",
129         },
130         {
131                 option: ObjectStorageClass(StorageStandard),
132                 key:    "X-Oss-Storage-Class",
133                 value:  "Standard",
134         },
135         {
136                 option: Callback("JTdCJTIyY2FsbGJhY2tVcmwlMjIlM0ElMjJleGFtcGxlLmNvbS9pbmRleC5odG1sJTIyJTdE"),
137                 key:    "X-Oss-Callback",
138                 value:  "JTdCJTIyY2FsbGJhY2tVcmwlMjIlM0ElMjJleGFtcGxlLmNvbS9pbmRleC5odG1sJTIyJTdE",
139         },
140         {
141                 option: CallbackVar("JTdCJTIyeCUzQXZhcjElMjIlM0ElMjJ2YWx1ZTElMjIlMkMlMjJ4JTNBdmFyMiUyMiUzQSUyMnZhbHVlMiUyMiU3RA=="),
142                 key:    "X-Oss-Callback-Var",
143                 value:  "JTdCJTIyeCUzQXZhcjElMjIlM0ElMjJ2YWx1ZTElMjIlMkMlMjJ4JTNBdmFyMiUyMiUzQSUyMnZhbHVlMiUyMiU3RA==",
144         },
145         {
146                 option: ContentLanguage("zh-CN"),
147                 key:    "Content-Language",
148                 value:  "zh-CN",
149         },
150         {
151                 option: ServerSideEncryptionKeyID("xossekid"),
152                 key:    "X-Oss-Server-Side-Encryption-Key-Id",
153                 value:  "xossekid",
154         },
155 }
156
157 func (s *OssOptionSuite) TestHeaderOptions(c *C) {
158         for _, testcase := range headerTestcases {
159                 headers := make(map[string]optionValue)
160                 err := testcase.option(headers)
161                 c.Assert(err, IsNil)
162
163                 expected, actual := testcase.value, headers[testcase.key].Value
164                 c.Assert(expected, Equals, actual)
165         }
166 }
167
168 var paramTestCases = []optionTestCase{
169         {
170                 option: Delimiter("/"),
171                 key:    "delimiter",
172                 value:  "/",
173         },
174         {
175                 option: Marker("abc"),
176                 key:    "marker",
177                 value:  "abc",
178         },
179         {
180                 option: MaxKeys(150),
181                 key:    "max-keys",
182                 value:  "150",
183         },
184         {
185                 option: Prefix("fun"),
186                 key:    "prefix",
187                 value:  "fun",
188         },
189         {
190                 option: EncodingType("ascii"),
191                 key:    "encoding-type",
192                 value:  "ascii",
193         },
194         {
195                 option: MaxUploads(100),
196                 key:    "max-uploads",
197                 value:  "100",
198         },
199         {
200                 option: KeyMarker("abc"),
201                 key:    "key-marker",
202                 value:  "abc",
203         },
204         {
205                 option: UploadIDMarker("xyz"),
206                 key:    "upload-id-marker",
207                 value:  "xyz",
208         },
209         {
210                 option: MaxParts(1000),
211                 key:    "max-parts",
212                 value:  "1000",
213         },
214         {
215                 option: PartNumberMarker(1),
216                 key:    "part-number-marker",
217                 value:  "1",
218         },
219         {
220                 option: Process("image/format,png"),
221                 key:    "x-oss-process",
222                 value:  "image/format,png",
223         },
224 }
225
226 func (s *OssOptionSuite) TestParamOptions(c *C) {
227         for _, testcase := range paramTestCases {
228                 params := make(map[string]optionValue)
229                 err := testcase.option(params)
230                 c.Assert(err, IsNil)
231
232                 expected, actual := testcase.value, params[testcase.key].Value
233                 c.Assert(expected, Equals, actual)
234         }
235 }
236
237 func (s *OssOptionSuite) TestHandleOptions(c *C) {
238         headers := make(map[string]string)
239         options := []Option{}
240
241         for _, testcase := range headerTestcases {
242                 options = append(options, testcase.option)
243         }
244
245         err := handleOptions(headers, options)
246         c.Assert(err, IsNil)
247
248         for _, testcase := range headerTestcases {
249                 expected, actual := testcase.value, headers[testcase.key]
250                 c.Assert(expected, Equals, actual)
251         }
252
253         options = []Option{IfMatch(""), nil}
254         headers = map[string]string{}
255         err = handleOptions(headers, options)
256         c.Assert(err, IsNil)
257         c.Assert(len(headers), Equals, 1)
258 }
259
260 func (s *OssOptionSuite) TestHandleParams(c *C) {
261         client, err := New(endpoint, accessID, accessKey)
262         c.Assert(err, IsNil)
263
264         options := []Option{}
265
266         for _, testcase := range paramTestCases {
267                 options = append(options, testcase.option)
268         }
269
270         params, err := GetRawParams(options)
271         c.Assert(err, IsNil)
272
273         out := client.Conn.getURLParams(params)
274         c.Assert(len(out), Equals, 191)
275
276         options = []Option{KeyMarker(""), nil}
277
278         params, err = GetRawParams(options)
279         c.Assert(err, IsNil)
280
281         out = client.Conn.getURLParams(params)
282         c.Assert(out, Equals, "key-marker=")
283 }
284
285 func (s *OssOptionSuite) TestFindOption(c *C) {
286         options := []Option{}
287
288         for _, testcase := range headerTestcases {
289                 options = append(options, testcase.option)
290         }
291
292         str, err := FindOption(options, "X-Oss-Acl", "")
293         c.Assert(err, IsNil)
294         c.Assert(str, Equals, "private")
295
296         str, err = FindOption(options, "MyProp", "")
297         c.Assert(err, IsNil)
298         c.Assert(str, Equals, "")
299 }
300
301 func (s *OssOptionSuite) TestDeleteOption(c *C) {
302         options := []Option{VersionId("123"), VersionIdMarker("456"), KeyMarker("789")}
303         str, err := FindOption(options, "versionId", "")
304         c.Assert(str, Equals, "123")
305         c.Assert(err, IsNil)
306
307         skipOption := DeleteOption(options, "versionId")
308         str, err = FindOption(skipOption, "versionId", "")
309         c.Assert(str, Equals, "")
310
311         str, err = FindOption(skipOption, "version-id-marker", "")
312         c.Assert(str, Equals, "456")
313
314         str, err = FindOption(skipOption, "key-marker", "")
315         c.Assert(str, Equals, "789")
316
317 }