option_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. func (s *OssOptionSuite) TestParamOptions(c *C) {
  192. for _, testcase := range paramTestCases {
  193. params := make(map[string]optionValue)
  194. err := testcase.option(params)
  195. c.Assert(err, IsNil)
  196. expected, actual := testcase.value, params[testcase.key].Value
  197. c.Assert(expected, Equals, actual)
  198. }
  199. }
  200. func (s *OssOptionSuite) TestHandleOptions(c *C) {
  201. headers := make(map[string]string)
  202. options := []Option{}
  203. for _, testcase := range headerTestcases {
  204. options = append(options, testcase.option)
  205. }
  206. err := handleOptions(headers, options)
  207. c.Assert(err, IsNil)
  208. for _, testcase := range headerTestcases {
  209. expected, actual := testcase.value, headers[testcase.key]
  210. c.Assert(expected, Equals, actual)
  211. }
  212. options = []Option{IfMatch(""), nil}
  213. headers = map[string]string{}
  214. err = handleOptions(headers, options)
  215. c.Assert(err, IsNil)
  216. c.Assert(len(headers), Equals, 1)
  217. }
  218. func (s *OssOptionSuite) TestHandleParams(c *C) {
  219. client, err := New(endpoint, accessID, accessKey)
  220. c.Assert(err, IsNil)
  221. options := []Option{}
  222. for _, testcase := range paramTestCases {
  223. options = append(options, testcase.option)
  224. }
  225. params, err := getRawParams(options)
  226. c.Assert(err, IsNil)
  227. out := client.Conn.getURLParams(params)
  228. c.Assert(len(out), Equals, 120)
  229. options = []Option{KeyMarker(""), nil}
  230. params, err = getRawParams(options)
  231. c.Assert(err, IsNil)
  232. out = client.Conn.getURLParams(params)
  233. c.Assert(out, Equals, "key-marker=")
  234. }
  235. func (s *OssOptionSuite) TestFindOption(c *C) {
  236. options := []Option{}
  237. for _, testcase := range headerTestcases {
  238. options = append(options, testcase.option)
  239. }
  240. str, err := findOption(options, "X-Oss-Acl", "")
  241. c.Assert(err, IsNil)
  242. c.Assert(str, Equals, "private")
  243. str, err = findOption(options, "MyProp", "")
  244. c.Assert(err, IsNil)
  245. c.Assert(str, Equals, "")
  246. }