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.

140 lines
3.0 KiB

  1. package cos
  2. import (
  3. "context"
  4. "encoding/xml"
  5. "fmt"
  6. "net/http"
  7. "reflect"
  8. "testing"
  9. )
  10. func TestBucketService_GetLifecycle(t *testing.T) {
  11. setup()
  12. defer teardown()
  13. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  14. testMethod(t, r, http.MethodGet)
  15. vs := values{
  16. "lifecycle": "",
  17. }
  18. testFormValues(t, r, vs)
  19. fmt.Fprint(w, `<LifecycleConfiguration>
  20. <Rule>
  21. <ID>1234</ID>
  22. <Filter>
  23. <Prefix>test</Prefix>
  24. </Filter>
  25. <Status>Enabled</Status>
  26. <Transition>
  27. <Days>10</Days>
  28. <StorageClass>Standard</StorageClass>
  29. </Transition>
  30. </Rule>
  31. <Rule>
  32. <ID>123422</ID>
  33. <Filter>
  34. <Prefix>gg</Prefix>
  35. </Filter>
  36. <Status>Disabled</Status>
  37. <Expiration>
  38. <Days>10</Days>
  39. </Expiration>
  40. </Rule>
  41. </LifecycleConfiguration>`)
  42. })
  43. ref, _, err := client.Bucket.GetLifecycle(context.Background())
  44. if err != nil {
  45. t.Fatalf("Bucket.GetLifecycle returned error: %v", err)
  46. }
  47. want := &BucketGetLifecycleResult{
  48. XMLName: xml.Name{Local: "LifecycleConfiguration"},
  49. Rules: []BucketLifecycleRule{
  50. {
  51. ID: "1234",
  52. Filter: &BucketLifecycleFilter{Prefix: "test"},
  53. Status: "Enabled",
  54. Transition: &BucketLifecycleTransition{Days: 10, StorageClass: "Standard"},
  55. },
  56. {
  57. ID: "123422",
  58. Filter: &BucketLifecycleFilter{Prefix: "gg"},
  59. Status: "Disabled",
  60. Expiration: &BucketLifecycleExpiration{Days: 10},
  61. },
  62. },
  63. }
  64. if !reflect.DeepEqual(ref, want) {
  65. t.Errorf("Bucket.GetLifecycle returned %+v, want %+v", ref, want)
  66. }
  67. }
  68. func TestBucketService_PutLifecycle(t *testing.T) {
  69. setup()
  70. defer teardown()
  71. opt := &BucketPutLifecycleOptions{
  72. Rules: []BucketLifecycleRule{
  73. {
  74. ID: "1234",
  75. Filter: &BucketLifecycleFilter{Prefix: "test"},
  76. Status: "Enabled",
  77. Transition: &BucketLifecycleTransition{Days: 10, StorageClass: "Standard"},
  78. },
  79. {
  80. ID: "123422",
  81. Filter: &BucketLifecycleFilter{Prefix: "gg"},
  82. Status: "Disabled",
  83. Expiration: &BucketLifecycleExpiration{Days: 10},
  84. },
  85. },
  86. }
  87. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  88. v := new(BucketPutLifecycleOptions)
  89. xml.NewDecoder(r.Body).Decode(v)
  90. testMethod(t, r, http.MethodPut)
  91. vs := values{
  92. "lifecycle": "",
  93. }
  94. testFormValues(t, r, vs)
  95. want := opt
  96. want.XMLName = xml.Name{Local: "LifecycleConfiguration"}
  97. if !reflect.DeepEqual(v, want) {
  98. t.Errorf("Bucket.PutLifecycle request body: %+v, want %+v", v, want)
  99. }
  100. })
  101. _, err := client.Bucket.PutLifecycle(context.Background(), opt)
  102. if err != nil {
  103. t.Fatalf("Bucket.PutLifecycle returned error: %v", err)
  104. }
  105. }
  106. func TestBucketService_DeleteLifecycle(t *testing.T) {
  107. setup()
  108. defer teardown()
  109. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  110. testMethod(t, r, http.MethodDelete)
  111. vs := values{
  112. "lifecycle": "",
  113. }
  114. testFormValues(t, r, vs)
  115. w.WriteHeader(http.StatusNoContent)
  116. })
  117. _, err := client.Bucket.DeleteLifecycle(context.Background())
  118. if err != nil {
  119. t.Fatalf("Bucket.DeleteLifecycle returned error: %v", err)
  120. }
  121. }