option_test.go 6.8 KB

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