option_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. func (s *OssOptionSuite) TestHeaderOptions(c *C) {
  141. for _, testcase := range headerTestcases {
  142. headers := make(map[string]optionValue)
  143. err := testcase.option(headers)
  144. c.Assert(err, IsNil)
  145. expected, actual := testcase.value, headers[testcase.key].Value
  146. c.Assert(expected, Equals, actual)
  147. }
  148. }
  149. var paramTestCases = []optionTestCase{
  150. {
  151. option: Delimiter("/"),
  152. key: "delimiter",
  153. value: "/",
  154. },
  155. {
  156. option: Marker("abc"),
  157. key: "marker",
  158. value: "abc",
  159. },
  160. {
  161. option: MaxKeys(150),
  162. key: "max-keys",
  163. value: "150",
  164. },
  165. {
  166. option: Prefix("fun"),
  167. key: "prefix",
  168. value: "fun",
  169. },
  170. {
  171. option: EncodingType("ascii"),
  172. key: "encoding-type",
  173. value: "ascii",
  174. },
  175. {
  176. option: MaxUploads(100),
  177. key: "max-uploads",
  178. value: "100",
  179. },
  180. {
  181. option: KeyMarker("abc"),
  182. key: "key-marker",
  183. value: "abc",
  184. },
  185. {
  186. option: UploadIDMarker("xyz"),
  187. key: "upload-id-marker",
  188. value: "xyz",
  189. },
  190. {
  191. option: MaxParts(1000),
  192. key: "max-parts",
  193. value: "1000",
  194. },
  195. {
  196. option: PartNumberMarker(1),
  197. key: "part-number-marker",
  198. value: "1",
  199. },
  200. {
  201. option: Process("image/format,png"),
  202. key: "x-oss-process",
  203. value: "image/format,png",
  204. },
  205. }
  206. func (s *OssOptionSuite) TestParamOptions(c *C) {
  207. for _, testcase := range paramTestCases {
  208. params := make(map[string]optionValue)
  209. err := testcase.option(params)
  210. c.Assert(err, IsNil)
  211. expected, actual := testcase.value, params[testcase.key].Value
  212. c.Assert(expected, Equals, actual)
  213. }
  214. }
  215. func (s *OssOptionSuite) TestHandleOptions(c *C) {
  216. headers := make(map[string]string)
  217. options := []Option{}
  218. for _, testcase := range headerTestcases {
  219. options = append(options, testcase.option)
  220. }
  221. err := handleOptions(headers, options)
  222. c.Assert(err, IsNil)
  223. for _, testcase := range headerTestcases {
  224. expected, actual := testcase.value, headers[testcase.key]
  225. c.Assert(expected, Equals, actual)
  226. }
  227. options = []Option{IfMatch(""), nil}
  228. headers = map[string]string{}
  229. err = handleOptions(headers, options)
  230. c.Assert(err, IsNil)
  231. c.Assert(len(headers), Equals, 1)
  232. }
  233. func (s *OssOptionSuite) TestHandleParams(c *C) {
  234. client, err := New(endpoint, accessID, accessKey)
  235. c.Assert(err, IsNil)
  236. options := []Option{}
  237. for _, testcase := range paramTestCases {
  238. options = append(options, testcase.option)
  239. }
  240. params, err := getRawParams(options)
  241. c.Assert(err, IsNil)
  242. out := client.Conn.getURLParams(params)
  243. c.Assert(len(out), Equals, 191)
  244. options = []Option{KeyMarker(""), nil}
  245. params, err = getRawParams(options)
  246. c.Assert(err, IsNil)
  247. out = client.Conn.getURLParams(params)
  248. c.Assert(out, Equals, "key-marker=")
  249. }
  250. func (s *OssOptionSuite) TestFindOption(c *C) {
  251. options := []Option{}
  252. for _, testcase := range headerTestcases {
  253. options = append(options, testcase.option)
  254. }
  255. str, err := findOption(options, "X-Oss-Acl", "")
  256. c.Assert(err, IsNil)
  257. c.Assert(str, Equals, "private")
  258. str, err = findOption(options, "MyProp", "")
  259. c.Assert(err, IsNil)
  260. c.Assert(str, Equals, "")
  261. }