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.

115 lines
2.8 KiB

  1. package cos
  2. import (
  3. "context"
  4. "encoding/xml"
  5. "fmt"
  6. "net/http"
  7. "reflect"
  8. "testing"
  9. )
  10. func TestBucketService_PutReplication(t *testing.T) {
  11. setup()
  12. defer teardown()
  13. opt := &PutBucketReplicationOptions{
  14. Role: "qcs::cam::uin/100000000001:uin/100000000001",
  15. Rule: []BucketReplicationRule{
  16. {
  17. Status: "Disabled",
  18. Prefix: "prefix",
  19. Destination: &ReplicationDestination{
  20. Bucket: "qcs::cos:ap-beijing-1::examplebucket-1250000000",
  21. },
  22. },
  23. },
  24. }
  25. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  26. testMethod(t, r, http.MethodPut)
  27. vs := values{
  28. "replication": "",
  29. }
  30. testFormValues(t, r, vs)
  31. body := &PutBucketReplicationOptions{}
  32. xml.NewDecoder(r.Body).Decode(body)
  33. want := opt
  34. want.XMLName = xml.Name{Local: "ReplicationConfiguration"}
  35. if !reflect.DeepEqual(want, body) {
  36. t.Fatalf("Bucket.PutReplication request\n body: %+v\n, want %+v\n", body, want)
  37. }
  38. })
  39. _, err := client.Bucket.PutBucketReplication(context.Background(), opt)
  40. if err != nil {
  41. t.Fatalf("Bucket.PutLogging failed, error: %v", err)
  42. }
  43. }
  44. func TestBucketService_GetReplication(t *testing.T) {
  45. setup()
  46. defer teardown()
  47. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  48. testMethod(t, r, http.MethodGet)
  49. vs := values{
  50. "replication": "",
  51. }
  52. testFormValues(t, r, vs)
  53. fmt.Fprint(w, `<ReplicationConfiguration>
  54. <Role>qcs::cam::uin/100000000001:uin/100000000001</Role>
  55. <Rule>
  56. <Status>Disabled</Status>
  57. <ID></ID>
  58. <Prefix>prefix</Prefix>
  59. <Destination>
  60. <Bucket>qcs::cos:ap-beijing-1::examplebucket-1250000000</Bucket>
  61. </Destination>
  62. </Rule>
  63. </ReplicationConfiguration>`)
  64. })
  65. res, _, err := client.Bucket.GetBucketReplication(context.Background())
  66. if err != nil {
  67. t.Fatalf("Bucket.GetReplication failed, error: %v", err)
  68. }
  69. want := &GetBucketReplicationResult{
  70. XMLName: xml.Name{Local: "ReplicationConfiguration"},
  71. Role: "qcs::cam::uin/100000000001:uin/100000000001",
  72. Rule: []BucketReplicationRule{
  73. {
  74. Status: "Disabled",
  75. Prefix: "prefix",
  76. Destination: &ReplicationDestination{
  77. Bucket: "qcs::cos:ap-beijing-1::examplebucket-1250000000",
  78. },
  79. },
  80. },
  81. }
  82. if !reflect.DeepEqual(res, want) {
  83. t.Errorf("Bucket.GetBucketReplication\nres %+v\nwant %+v", res.Rule[0].Destination, want.Rule[0].Destination)
  84. t.Errorf("Bucket.GetBucketReplication\nres %+v\nwant %+v", res, want)
  85. }
  86. }
  87. func TestBucketService_DeleteReplication(t *testing.T) {
  88. setup()
  89. defer teardown()
  90. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  91. testMethod(t, r, http.MethodDelete)
  92. vs := values{
  93. "replication": "",
  94. }
  95. testFormValues(t, r, vs)
  96. w.WriteHeader(http.StatusNoContent)
  97. })
  98. _, err := client.Bucket.DeleteBucketReplication(context.Background())
  99. if err != nil {
  100. t.Fatalf("Bucket.DeleteBucketReplication returned error: %v", err)
  101. }
  102. }