option_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. func (s *OssOptionSuite) TestHeaderOptions(c *C) {
  131. for _, testcase := range headerTestcases {
  132. headers := make(map[string]optionValue)
  133. err := testcase.option(headers)
  134. c.Assert(err, IsNil)
  135. expected, actual := testcase.value, headers[testcase.key].Value
  136. c.Assert(expected, Equals, actual)
  137. }
  138. }
  139. var paramTestCases = []optionTestCase{
  140. {
  141. option: Delimiter("/"),
  142. key: "delimiter",
  143. value: "/",
  144. },
  145. {
  146. option: Marker("abc"),
  147. key: "marker",
  148. value: "abc",
  149. },
  150. {
  151. option: MaxKeys(150),
  152. key: "max-keys",
  153. value: "150",
  154. },
  155. {
  156. option: Prefix("fun"),
  157. key: "prefix",
  158. value: "fun",
  159. },
  160. {
  161. option: EncodingType("ascii"),
  162. key: "encoding-type",
  163. value: "ascii",
  164. },
  165. {
  166. option: MaxUploads(100),
  167. key: "max-uploads",
  168. value: "100",
  169. },
  170. {
  171. option: KeyMarker("abc"),
  172. key: "key-marker",
  173. value: "abc",
  174. },
  175. {
  176. option: UploadIDMarker("xyz"),
  177. key: "upload-id-marker",
  178. value: "xyz",
  179. },
  180. }
  181. func (s *OssOptionSuite) TestParamOptions(c *C) {
  182. for _, testcase := range paramTestCases {
  183. params := make(map[string]optionValue)
  184. err := testcase.option(params)
  185. c.Assert(err, IsNil)
  186. expected, actual := testcase.value, params[testcase.key].Value
  187. c.Assert(expected, Equals, actual)
  188. }
  189. }
  190. func (s *OssOptionSuite) TestHandleOptions(c *C) {
  191. headers := make(map[string]string)
  192. options := []Option{}
  193. for _, testcase := range headerTestcases {
  194. options = append(options, testcase.option)
  195. }
  196. err := handleOptions(headers, options)
  197. c.Assert(err, IsNil)
  198. for _, testcase := range headerTestcases {
  199. expected, actual := testcase.value, headers[testcase.key]
  200. c.Assert(expected, Equals, actual)
  201. }
  202. options = []Option{IfMatch(""), nil}
  203. headers = map[string]string{}
  204. err = handleOptions(headers, options)
  205. c.Assert(err, IsNil)
  206. c.Assert(len(headers), Equals, 1)
  207. }
  208. func (s *OssOptionSuite) TestHandleParams(c *C) {
  209. client, err := New(endpoint, accessID, accessKey)
  210. c.Assert(err, IsNil)
  211. options := []Option{}
  212. for _, testcase := range paramTestCases {
  213. options = append(options, testcase.option)
  214. }
  215. params, err := getRawParams(options)
  216. c.Assert(err, IsNil)
  217. out := client.Conn.getURLParams(params)
  218. c.Assert(len(out), Equals, 120)
  219. options = []Option{KeyMarker(""), nil}
  220. params, err = getRawParams(options)
  221. c.Assert(err, IsNil)
  222. out = client.Conn.getURLParams(params)
  223. c.Assert(out, Equals, "key-marker=")
  224. }
  225. func (s *OssOptionSuite) TestFindOption(c *C) {
  226. options := []Option{}
  227. for _, testcase := range headerTestcases {
  228. options = append(options, testcase.option)
  229. }
  230. str, err := findOption(options, "X-Oss-Acl", "")
  231. c.Assert(err, IsNil)
  232. c.Assert(str, Equals, "private")
  233. str, err = findOption(options, "MyProp", "")
  234. c.Assert(err, IsNil)
  235. c.Assert(str, Equals, "")
  236. }