option_test.go 5.3 KB

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