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.

273 lines
7.0 KiB

  1. package cos
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/xml"
  6. "fmt"
  7. "io/ioutil"
  8. "net/http"
  9. "reflect"
  10. "testing"
  11. )
  12. func TestObjectService_AbortMultipartUpload(t *testing.T) {
  13. setup()
  14. defer teardown()
  15. name := "test/hello.txt"
  16. uploadID := "xxxxaabcc"
  17. mux.HandleFunc("/test/hello.txt", func(w http.ResponseWriter, r *http.Request) {
  18. testMethod(t, r, http.MethodDelete)
  19. vs := values{
  20. "uploadId": uploadID,
  21. }
  22. testFormValues(t, r, vs)
  23. w.WriteHeader(http.StatusNoContent)
  24. })
  25. _, err := client.Object.AbortMultipartUpload(context.Background(),
  26. name, uploadID)
  27. if err != nil {
  28. t.Fatalf("Object.AbortMultipartUpload returned error: %v", err)
  29. }
  30. }
  31. func TestObjectService_InitiateMultipartUpload(t *testing.T) {
  32. setup()
  33. defer teardown()
  34. opt := &InitiateMultipartUploadOptions{
  35. ObjectPutHeaderOptions: &ObjectPutHeaderOptions{
  36. ContentType: "text/html",
  37. },
  38. ACLHeaderOptions: &ACLHeaderOptions{
  39. XCosACL: "private",
  40. },
  41. }
  42. name := "test/hello.txt"
  43. mux.HandleFunc("/test/hello.txt", func(w http.ResponseWriter, r *http.Request) {
  44. v := new(BucketPutTaggingOptions)
  45. xml.NewDecoder(r.Body).Decode(v)
  46. testMethod(t, r, http.MethodPost)
  47. testHeader(t, r, "x-cos-acl", "private")
  48. testHeader(t, r, "Content-Type", "text/html")
  49. vs := values{
  50. "uploads": "",
  51. }
  52. testFormValues(t, r, vs)
  53. fmt.Fprint(w, `<InitiateMultipartUploadResult>
  54. <Bucket>test-1253846586</Bucket>
  55. <Key>test/hello.txt</Key>
  56. <UploadId>149795166761115ef06e259b2fceb8ff34bf7dd840883d26a0f90243562dd398efa41718db</UploadId>
  57. </InitiateMultipartUploadResult>`)
  58. })
  59. ref, _, err := client.Object.InitiateMultipartUpload(context.Background(),
  60. name, opt)
  61. if err != nil {
  62. t.Fatalf("Object.InitiateMultipartUpload returned error: %v", err)
  63. }
  64. want := &InitiateMultipartUploadResult{
  65. XMLName: xml.Name{Local: "InitiateMultipartUploadResult"},
  66. Bucket: "test-1253846586",
  67. Key: "test/hello.txt",
  68. UploadID: "149795166761115ef06e259b2fceb8ff34bf7dd840883d26a0f90243562dd398efa41718db",
  69. }
  70. if !reflect.DeepEqual(ref, want) {
  71. t.Errorf("Object.InitiateMultipartUpload returned %+v, want %+v", ref, want)
  72. }
  73. }
  74. func TestObjectService_UploadPart(t *testing.T) {
  75. setup()
  76. defer teardown()
  77. opt := &ObjectUploadPartOptions{}
  78. name := "test/hello.txt"
  79. uploadID := "xxxxx"
  80. partNumber := 1
  81. mux.HandleFunc("/test/hello.txt", func(w http.ResponseWriter, r *http.Request) {
  82. testMethod(t, r, http.MethodPut)
  83. vs := values{
  84. "uploadId": uploadID,
  85. "partNumber": "1",
  86. }
  87. testFormValues(t, r, vs)
  88. b, _ := ioutil.ReadAll(r.Body)
  89. v := string(b)
  90. want := "hello"
  91. if !reflect.DeepEqual(v, want) {
  92. t.Errorf("Object.UploadPart request body: %#v, want %#v", v, want)
  93. }
  94. })
  95. r := bytes.NewReader([]byte("hello"))
  96. _, err := client.Object.UploadPart(context.Background(),
  97. name, uploadID, partNumber, r, opt)
  98. if err != nil {
  99. t.Fatalf("Object.UploadPart returned error: %v", err)
  100. }
  101. }
  102. func TestObjectService_ListParts(t *testing.T) {
  103. setup()
  104. defer teardown()
  105. name := "test/hello.txt"
  106. uploadID := "149795194893578fd83aceef3a88f708f81f00e879fda5ea8a80bf15aba52746d42d512387"
  107. mux.HandleFunc("/test/hello.txt", func(w http.ResponseWriter, r *http.Request) {
  108. v := new(BucketPutTaggingOptions)
  109. xml.NewDecoder(r.Body).Decode(v)
  110. testMethod(t, r, http.MethodGet)
  111. vs := values{
  112. "uploadId": uploadID,
  113. }
  114. testFormValues(t, r, vs)
  115. fmt.Fprint(w, `<ListPartsResult>
  116. <Bucket>test-1253846586</Bucket>
  117. <Encoding-type/>
  118. <Key>test/hello.txt</Key>
  119. <UploadId>149795194893578fd83aceef3a88f708f81f00e879fda5ea8a80bf15aba52746d42d512387</UploadId>
  120. <Owner>
  121. <ID>1253846586</ID>
  122. <DisplayName>1253846586</DisplayName>
  123. </Owner>
  124. <PartNumberMarker>0</PartNumberMarker>
  125. <Initiator>
  126. <ID>qcs::cam::uin/100000760461:uin/100000760461</ID>
  127. <DisplayName>100000760461</DisplayName>
  128. </Initiator>
  129. <Part>
  130. <PartNumber>1</PartNumber>
  131. <LastModified>2017-06-20T09:45:49.000Z</LastModified>
  132. <ETag>&quot;fae3dba15f4d9b2d76cbaed5de3a08e3&quot;</ETag>
  133. <Size>6291456</Size>
  134. </Part>
  135. <Part>
  136. <PartNumber>2</PartNumber>
  137. <LastModified>2017-06-20T09:45:50.000Z</LastModified>
  138. <ETag>&quot;c81982550f2f965118d486176d9541d4&quot;</ETag>
  139. <Size>6391456</Size>
  140. </Part>
  141. <StorageClass>Standard</StorageClass>
  142. <MaxParts>1000</MaxParts>
  143. <IsTruncated>false</IsTruncated>
  144. </ListPartsResult>`)
  145. })
  146. ref, _, err := client.Object.ListParts(context.Background(),
  147. name, uploadID)
  148. if err != nil {
  149. t.Fatalf("Object.ListParts returned error: %v", err)
  150. }
  151. want := &ObjectListPartsResult{
  152. XMLName: xml.Name{Local: "ListPartsResult"},
  153. Bucket: "test-1253846586",
  154. UploadID: uploadID,
  155. Key: name,
  156. Owner: &Owner{
  157. ID: "1253846586",
  158. DisplayName: "1253846586",
  159. },
  160. PartNumberMarker: 0,
  161. Initiator: &Initiator{
  162. ID: "qcs::cam::uin/100000760461:uin/100000760461",
  163. DisplayName: "100000760461",
  164. },
  165. Parts: []Object{
  166. {
  167. PartNumber: 1,
  168. LastModified: "2017-06-20T09:45:49.000Z",
  169. ETag: "\"fae3dba15f4d9b2d76cbaed5de3a08e3\"",
  170. Size: 6291456,
  171. },
  172. {
  173. PartNumber: 2,
  174. LastModified: "2017-06-20T09:45:50.000Z",
  175. ETag: "\"c81982550f2f965118d486176d9541d4\"",
  176. Size: 6391456,
  177. },
  178. },
  179. StorageClass: "Standard",
  180. MaxParts: 1000,
  181. IsTruncated: false,
  182. }
  183. if !reflect.DeepEqual(ref, want) {
  184. t.Errorf("Object.ListParts returned \n%#v, want \n%#v", ref, want)
  185. }
  186. }
  187. func TestObjectService_CompleteMultipartUpload(t *testing.T) {
  188. setup()
  189. defer teardown()
  190. name := "test/hello.txt"
  191. uploadID := "149795194893578fd83aceef3a88f708f81f00e879fda5ea8a80bf15aba52746d42d512387"
  192. opt := &CompleteMultipartUploadOptions{
  193. Parts: []Object{
  194. {
  195. PartNumber: 1,
  196. ETag: "\"fae3dba15f4d9b2d76cbaed5de3a08e3\"",
  197. },
  198. {
  199. PartNumber: 2,
  200. ETag: "\"c81982550f2f965118d486176d9541d4\"",
  201. },
  202. },
  203. }
  204. mux.HandleFunc("/test/hello.txt", func(w http.ResponseWriter, r *http.Request) {
  205. v := new(CompleteMultipartUploadOptions)
  206. xml.NewDecoder(r.Body).Decode(v)
  207. testMethod(t, r, http.MethodPost)
  208. vs := values{
  209. "uploadId": uploadID,
  210. }
  211. testFormValues(t, r, vs)
  212. want := opt
  213. want.XMLName = xml.Name{Local: "CompleteMultipartUpload"}
  214. if !reflect.DeepEqual(v, want) {
  215. t.Errorf("Object.CompleteMultipartUpload request body: %+v, want %+v", v, want)
  216. }
  217. fmt.Fprint(w, `<CompleteMultipartUploadResult>
  218. <Location>test-1253846586.cos.ap-guangzhou.myqcloud.com/test/hello.txt</Location>
  219. <Bucket>test</Bucket>
  220. <Key>test/hello.txt</Key>
  221. <ETag>&quot;594f98b11c6901c0f0683de1085a6d0e-4&quot;</ETag>
  222. </CompleteMultipartUploadResult>`)
  223. })
  224. ref, _, err := client.Object.CompleteMultipartUpload(context.Background(),
  225. name, uploadID, opt)
  226. if err != nil {
  227. t.Fatalf("Object.ListParts returned error: %v", err)
  228. }
  229. want := &CompleteMultipartUploadResult{
  230. XMLName: xml.Name{Local: "CompleteMultipartUploadResult"},
  231. Bucket: "test",
  232. Key: name,
  233. ETag: "\"594f98b11c6901c0f0683de1085a6d0e-4\"",
  234. Location: "test-1253846586.cos.ap-guangzhou.myqcloud.com/test/hello.txt",
  235. }
  236. if !reflect.DeepEqual(ref, want) {
  237. t.Errorf("Object.CompleteMultipartUpload returned \n%#v, want \n%#v", ref, want)
  238. }
  239. }