You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

257 lines
6.1 KiB

4 years ago
4 years ago
4 years ago
4 years ago
  1. package cos
  2. import (
  3. "context"
  4. "encoding/xml"
  5. "fmt"
  6. "net/http"
  7. "reflect"
  8. "testing"
  9. )
  10. func TestBucketService_Get(t *testing.T) {
  11. setup()
  12. defer teardown()
  13. opt := &BucketGetOptions{
  14. Prefix: "test",
  15. MaxKeys: 2,
  16. }
  17. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  18. testMethod(t, r, http.MethodGet)
  19. vs := values{
  20. "prefix": "test",
  21. "max-keys": "2",
  22. }
  23. testFormValues(t, r, vs)
  24. fmt.Fprint(w, `<?xml version='1.0' encoding='utf-8' ?>
  25. <ListBucketResult>
  26. <Name>test-1253846586</Name>
  27. <Prefix>test</Prefix>
  28. <Marker/>
  29. <MaxKeys>2</MaxKeys>
  30. <IsTruncated>true</IsTruncated>
  31. <NextMarker>test/delete.txt</NextMarker>
  32. <Contents>
  33. <Key>test/</Key>
  34. <LastModified>2017-06-09T16:32:25.000Z</LastModified>
  35. <ETag>&quot;&quot;</ETag>
  36. <Size>0</Size>
  37. <Owner>
  38. <ID>1253846586</ID>
  39. </Owner>
  40. <StorageClass>STANDARD</StorageClass>
  41. </Contents>
  42. <Contents>
  43. <Key>test/anonymous_get.go</Key>
  44. <LastModified>2017-06-17T15:09:26.000Z</LastModified>
  45. <ETag>&quot;5b7236085f08b3818bfa40b03c946dcc&quot;</ETag>
  46. <Size>8</Size>
  47. <Owner>
  48. <ID>1253846586</ID>
  49. </Owner>
  50. <StorageClass>STANDARD</StorageClass>
  51. </Contents>
  52. </ListBucketResult>`)
  53. })
  54. ref, _, err := client.Bucket.Get(context.Background(), opt)
  55. if err != nil {
  56. t.Fatalf("Bucket.Get returned error: %v", err)
  57. }
  58. want := &BucketGetResult{
  59. XMLName: xml.Name{Local: "ListBucketResult"},
  60. Name: "test-1253846586",
  61. Prefix: "test",
  62. MaxKeys: 2,
  63. IsTruncated: true,
  64. NextMarker: "test/delete.txt",
  65. Contents: []Object{
  66. {
  67. Key: "test/",
  68. LastModified: "2017-06-09T16:32:25.000Z",
  69. ETag: "\"\"",
  70. Size: 0,
  71. Owner: &Owner{
  72. ID: "1253846586",
  73. },
  74. StorageClass: "STANDARD",
  75. },
  76. {
  77. Key: "test/anonymous_get.go",
  78. LastModified: "2017-06-17T15:09:26.000Z",
  79. ETag: "\"5b7236085f08b3818bfa40b03c946dcc\"",
  80. Size: 8,
  81. Owner: &Owner{
  82. ID: "1253846586",
  83. },
  84. StorageClass: "STANDARD",
  85. },
  86. },
  87. }
  88. if !reflect.DeepEqual(ref, want) {
  89. t.Errorf("Bucket.Get returned %+v, want %+v", ref, want)
  90. }
  91. }
  92. func TestBucketService_Put(t *testing.T) {
  93. setup()
  94. defer teardown()
  95. opt := &BucketPutOptions{
  96. XCosACL: "public-read",
  97. }
  98. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  99. v := new(BucketPutTaggingOptions)
  100. xml.NewDecoder(r.Body).Decode(v)
  101. testMethod(t, r, "PUT")
  102. testHeader(t, r, "x-cos-acl", "public-read")
  103. })
  104. _, err := client.Bucket.Put(context.Background(), opt)
  105. if err != nil {
  106. t.Fatalf("Bucket.Put returned error: %v", err)
  107. }
  108. }
  109. func TestBucketService_Delete(t *testing.T) {
  110. setup()
  111. defer teardown()
  112. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  113. testMethod(t, r, http.MethodDelete)
  114. w.WriteHeader(http.StatusNoContent)
  115. })
  116. _, err := client.Bucket.Delete(context.Background())
  117. if err != nil {
  118. t.Fatalf("Bucket.Delete returned error: %v", err)
  119. }
  120. }
  121. func TestBucketService_Head(t *testing.T) {
  122. setup()
  123. defer teardown()
  124. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  125. testMethod(t, r, http.MethodHead)
  126. w.WriteHeader(http.StatusOK)
  127. })
  128. _, err := client.Bucket.Head(context.Background())
  129. if err != nil {
  130. t.Fatalf("Bucket.Head returned error: %v", err)
  131. }
  132. }
  133. func TestBucketService_GetObjectVersions(t *testing.T) {
  134. setup()
  135. defer teardown()
  136. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  137. testMethod(t, r, http.MethodGet)
  138. w.WriteHeader(http.StatusOK)
  139. vs := values{
  140. "versions": "",
  141. "delimiter": "/",
  142. }
  143. testFormValues(t, r, vs)
  144. fmt.Fprint(w, `<?xml version='1.0' encoding='utf-8' ?>
  145. <ListVersionsResult>
  146. <Name>examplebucket-1250000000</Name>
  147. <Prefix/>
  148. <KeyMarker/>
  149. <VersionIdMarker/>
  150. <MaxKeys>1000</MaxKeys>
  151. <IsTruncated>false</IsTruncated>
  152. <Delimiter>/</Delimiter>
  153. <CommonPrefixes>
  154. <Prefix>example-folder-1/</Prefix>
  155. </CommonPrefixes>
  156. <CommonPrefixes>
  157. <Prefix>example-folder-2/</Prefix>
  158. </CommonPrefixes>
  159. <Version>
  160. <Key>example-object-1.jpg</Key>
  161. <VersionId>MTg0NDUxNzgxMjEzNTU3NTk1Mjg</VersionId>
  162. <IsLatest>true</IsLatest>
  163. <LastModified>2019-08-16T10:45:53.000Z</LastModified>
  164. <ETag>&quot;5d1143df07a17b23320d0da161e2819e&quot;</ETag>
  165. <Size>30</Size>
  166. <StorageClass>STANDARD</StorageClass>
  167. <Owner>
  168. <ID>1250000000</ID>
  169. <DisplayName>1250000000</DisplayName>
  170. </Owner>
  171. </Version>
  172. <DeleteMarker>
  173. <Key>example-object-1.jpg</Key>
  174. <VersionId>MTg0NDUxNzgxMjEzNjE1OTcxMzM</VersionId>
  175. <IsLatest>false</IsLatest>
  176. <LastModified>2019-08-16T10:45:47.000Z</LastModified>
  177. <Owner>
  178. <ID>1250000000</ID>
  179. <DisplayName>1250000000</DisplayName>
  180. </Owner>
  181. </DeleteMarker>
  182. </ListVersionsResult>`)
  183. })
  184. want := &BucketGetObjectVersionsResult{
  185. XMLName: xml.Name{Local: "ListVersionsResult"},
  186. Name: "examplebucket-1250000000",
  187. MaxKeys: 1000,
  188. IsTruncated: false,
  189. Delimiter: "/",
  190. CommonPrefixes: []string{
  191. "example-folder-1/",
  192. "example-folder-2/",
  193. },
  194. Version: []ListVersionsResultVersion{
  195. {
  196. Key: "example-object-1.jpg",
  197. VersionId: "MTg0NDUxNzgxMjEzNTU3NTk1Mjg",
  198. IsLatest: true,
  199. LastModified: "2019-08-16T10:45:53.000Z",
  200. ETag: "\"5d1143df07a17b23320d0da161e2819e\"",
  201. Size: 30,
  202. StorageClass: "STANDARD",
  203. Owner: &Owner{
  204. ID: "1250000000",
  205. DisplayName: "1250000000",
  206. },
  207. },
  208. },
  209. DeleteMarker: []ListVersionsResultDeleteMarker{
  210. {
  211. Key: "example-object-1.jpg",
  212. VersionId: "MTg0NDUxNzgxMjEzNjE1OTcxMzM",
  213. IsLatest: false,
  214. LastModified: "2019-08-16T10:45:47.000Z",
  215. Owner: &Owner{
  216. ID: "1250000000",
  217. DisplayName: "1250000000",
  218. },
  219. },
  220. },
  221. }
  222. opt := &BucketGetObjectVersionsOptions{
  223. Delimiter: "/",
  224. }
  225. res, _, err := client.Bucket.GetObjectVersions(context.Background(), opt)
  226. if err != nil {
  227. t.Fatalf("Bucket.GetObjectVersions returned error: %v", err)
  228. }
  229. if !reflect.DeepEqual(res, want) {
  230. t.Errorf("Bucket.GetObjectVersions returned\n%+v\nwant\n%+v", res, want)
  231. }
  232. }