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.

152 lines
3.2 KiB

  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. }