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.

274 lines
6.0 KiB

6 years ago
  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_Get(t *testing.T) {
  13. setup()
  14. defer teardown()
  15. name := "test/hello.txt"
  16. mux.HandleFunc("/test/hello.txt", func(w http.ResponseWriter, r *http.Request) {
  17. testMethod(t, r, "GET")
  18. vs := values{
  19. "response-content-type": "text/html",
  20. }
  21. testFormValues(t, r, vs)
  22. testHeader(t, r, "Range", "bytes=0-3")
  23. fmt.Fprint(w, `hello`)
  24. })
  25. opt := &ObjectGetOptions{
  26. ResponseContentType: "text/html",
  27. Range: "bytes=0-3",
  28. }
  29. resp, err := client.Object.Get(context.Background(), name, opt)
  30. if err != nil {
  31. t.Fatalf("Object.Get returned error: %v", err)
  32. }
  33. b, _ := ioutil.ReadAll(resp.Body)
  34. ref := string(b)
  35. want := "hello"
  36. if !reflect.DeepEqual(ref, want) {
  37. t.Errorf("Object.Get returned %+v, want %+v", ref, want)
  38. }
  39. }
  40. func TestObjectService_Put(t *testing.T) {
  41. setup()
  42. defer teardown()
  43. opt := &ObjectPutOptions{
  44. ObjectPutHeaderOptions: &ObjectPutHeaderOptions{
  45. ContentType: "text/html",
  46. },
  47. ACLHeaderOptions: &ACLHeaderOptions{
  48. XCosACL: "private",
  49. },
  50. }
  51. name := "test/hello.txt"
  52. mux.HandleFunc("/test/hello.txt", func(w http.ResponseWriter, r *http.Request) {
  53. testMethod(t, r, http.MethodPut)
  54. testHeader(t, r, "x-cos-acl", "private")
  55. testHeader(t, r, "Content-Type", "text/html")
  56. b, _ := ioutil.ReadAll(r.Body)
  57. v := string(b)
  58. want := "hello"
  59. if !reflect.DeepEqual(v, want) {
  60. t.Errorf("Object.Put request body: %#v, want %#v", v, want)
  61. }
  62. })
  63. r := bytes.NewReader([]byte("hello"))
  64. _, err := client.Object.Put(context.Background(), name, r, opt)
  65. if err != nil {
  66. t.Fatalf("Object.Put returned error: %v", err)
  67. }
  68. }
  69. func TestObjectService_Delete(t *testing.T) {
  70. setup()
  71. defer teardown()
  72. name := "test/hello.txt"
  73. mux.HandleFunc("/test/hello.txt", func(w http.ResponseWriter, r *http.Request) {
  74. testMethod(t, r, http.MethodDelete)
  75. w.WriteHeader(http.StatusNoContent)
  76. })
  77. _, err := client.Object.Delete(context.Background(), name)
  78. if err != nil {
  79. t.Fatalf("Object.Delete returned error: %v", err)
  80. }
  81. }
  82. func TestObjectService_Head(t *testing.T) {
  83. setup()
  84. defer teardown()
  85. name := "test/hello.txt"
  86. mux.HandleFunc("/test/hello.txt", func(w http.ResponseWriter, r *http.Request) {
  87. testMethod(t, r, "HEAD")
  88. testHeader(t, r, "If-Modified-Since", "Mon, 12 Jun 2017 05:36:19 GMT")
  89. })
  90. opt := &ObjectHeadOptions{
  91. IfModifiedSince: "Mon, 12 Jun 2017 05:36:19 GMT",
  92. }
  93. _, err := client.Object.Head(context.Background(), name, opt)
  94. if err != nil {
  95. t.Fatalf("Object.Head returned error: %v", err)
  96. }
  97. }
  98. func TestObjectService_Options(t *testing.T) {
  99. setup()
  100. defer teardown()
  101. name := "test/hello.txt"
  102. mux.HandleFunc("/test/hello.txt", func(w http.ResponseWriter, r *http.Request) {
  103. testMethod(t, r, http.MethodOptions)
  104. testHeader(t, r, "Access-Control-Request-Method", "PUT")
  105. testHeader(t, r, "Origin", "www.qq.com")
  106. })
  107. opt := &ObjectOptionsOptions{
  108. Origin: "www.qq.com",
  109. AccessControlRequestMethod: "PUT",
  110. }
  111. _, err := client.Object.Options(context.Background(), name, opt)
  112. if err != nil {
  113. t.Fatalf("Object.Options returned error: %v", err)
  114. }
  115. }
  116. // func TestObjectService_Append(t *testing.T) {
  117. // setup()
  118. // defer teardown()
  119. // opt := &ObjectPutOptions{
  120. // ObjectPutHeaderOptions: &ObjectPutHeaderOptions{
  121. // ContentType: "text/html",
  122. // },
  123. // ACLHeaderOptions: &ACLHeaderOptions{
  124. // XCosACL: "private",
  125. // },
  126. // }
  127. // name := "test/hello.txt"
  128. // position := 0
  129. // mux.HandleFunc("/test/hello.txt", func(w http.ResponseWriter, r *http.Request) {
  130. // vs := values{
  131. // "append": "",
  132. // "position": "0",
  133. // }
  134. // testFormValues(t, r, vs)
  135. // testMethod(t, r, http.MethodPost)
  136. // testHeader(t, r, "x-cos-acl", "private")
  137. // testHeader(t, r, "Content-Type", "text/html")
  138. // b, _ := ioutil.ReadAll(r.Body)
  139. // v := string(b)
  140. // want := "hello"
  141. // if !reflect.DeepEqual(v, want) {
  142. // t.Errorf("Object.Append request body: %#v, want %#v", v, want)
  143. // }
  144. // })
  145. // r := bytes.NewReader([]byte("hello"))
  146. // _, err := client.Object.Append(context.Background(), name, position, r, opt)
  147. // if err != nil {
  148. // t.Fatalf("Object.Append returned error: %v", err)
  149. // }
  150. // }
  151. func TestObjectService_DeleteMulti(t *testing.T) {
  152. setup()
  153. defer teardown()
  154. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  155. testMethod(t, r, http.MethodPost)
  156. vs := values{
  157. "delete": "",
  158. }
  159. testFormValues(t, r, vs)
  160. fmt.Fprint(w, `<DeleteResult>
  161. <Deleted>
  162. <Key>test1</Key>
  163. </Deleted>
  164. <Deleted>
  165. <Key>test3</Key>
  166. </Deleted>
  167. <Deleted>
  168. <Key>test2</Key>
  169. </Deleted>
  170. </DeleteResult>`)
  171. })
  172. opt := &ObjectDeleteMultiOptions{
  173. Objects: []Object{
  174. {
  175. Key: "test1",
  176. },
  177. {
  178. Key: "test3",
  179. },
  180. {
  181. Key: "test2",
  182. },
  183. },
  184. }
  185. ref, _, err := client.Object.DeleteMulti(context.Background(), opt)
  186. if err != nil {
  187. t.Fatalf("Object.DeleteMulti returned error: %v", err)
  188. }
  189. want := &ObjectDeleteMultiResult{
  190. XMLName: xml.Name{Local: "DeleteResult"},
  191. DeletedObjects: []Object{
  192. {
  193. Key: "test1",
  194. },
  195. {
  196. Key: "test3",
  197. },
  198. {
  199. Key: "test2",
  200. },
  201. },
  202. }
  203. if !reflect.DeepEqual(ref, want) {
  204. t.Errorf("Object.DeleteMulti returned %+v, want %+v", ref, want)
  205. }
  206. }
  207. func TestObjectService_Copy(t *testing.T) {
  208. setup()
  209. defer teardown()
  210. mux.HandleFunc("/test.go.copy", func(w http.ResponseWriter, r *http.Request) {
  211. testMethod(t, r, http.MethodPut)
  212. fmt.Fprint(w, `<CopyObjectResult>
  213. <ETag>"098f6bcd4621d373cade4e832627b4f6"</ETag>
  214. <LastModified>2017-12-13T14:53:12</LastModified>
  215. </CopyObjectResult>`)
  216. })
  217. sourceURL := "test-1253846586.cos.ap-guangzhou.myqcloud.com/test.source"
  218. ref, _, err := client.Object.Copy(context.Background(), "test.go.copy", sourceURL, nil)
  219. if err != nil {
  220. t.Fatalf("Object.Copy returned error: %v", err)
  221. }
  222. want := &ObjectCopyResult{
  223. XMLName: xml.Name{Local: "CopyObjectResult"},
  224. ETag: `"098f6bcd4621d373cade4e832627b4f6"`,
  225. LastModified: "2017-12-13T14:53:12",
  226. }
  227. if !reflect.DeepEqual(ref, want) {
  228. t.Errorf("Object.Copy returned %+v, want %+v", ref, want)
  229. }
  230. }