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.1 KiB

  1. package cos
  2. import (
  3. "context"
  4. "encoding/xml"
  5. "fmt"
  6. "net/http"
  7. "reflect"
  8. "testing"
  9. )
  10. func TestBucketService_GetTagging(t *testing.T) {
  11. setup()
  12. defer teardown()
  13. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  14. testMethod(t, r, "GET")
  15. vs := values{
  16. "tagging": "",
  17. }
  18. testFormValues(t, r, vs)
  19. fmt.Fprint(w, `<Tagging>
  20. <TagSet>
  21. <Tag>
  22. <Key>test_k2</Key>
  23. <Value>test_v2</Value>
  24. </Tag>
  25. <Tag>
  26. <Key>test_k3</Key>
  27. <Value>test_vv</Value>
  28. </Tag>
  29. </TagSet>
  30. </Tagging>`)
  31. })
  32. ref, _, err := client.Bucket.GetTagging(context.Background())
  33. if err != nil {
  34. t.Fatalf("Bucket.GetTagging returned error: %v", err)
  35. }
  36. want := &BucketGetTaggingResult{
  37. XMLName: xml.Name{Local: "Tagging"},
  38. TagSet: []BucketTaggingTag{
  39. {"test_k2", "test_v2"},
  40. {"test_k3", "test_vv"},
  41. },
  42. }
  43. if !reflect.DeepEqual(ref, want) {
  44. t.Errorf("Bucket.GetTagging returned %+v, want %+v", ref, want)
  45. }
  46. }
  47. func TestBucketService_PutTagging(t *testing.T) {
  48. setup()
  49. defer teardown()
  50. opt := &BucketPutTaggingOptions{
  51. TagSet: []BucketTaggingTag{
  52. {
  53. Key: "test_k2",
  54. Value: "test_v2",
  55. },
  56. {
  57. Key: "test_k3",
  58. Value: "test_v3",
  59. },
  60. },
  61. }
  62. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  63. v := new(BucketPutTaggingOptions)
  64. xml.NewDecoder(r.Body).Decode(v)
  65. testMethod(t, r, "PUT")
  66. vs := values{
  67. "tagging": "",
  68. }
  69. testFormValues(t, r, vs)
  70. want := opt
  71. want.XMLName = xml.Name{Local: "Tagging"}
  72. if !reflect.DeepEqual(v, want) {
  73. t.Errorf("Bucket.PutTagging request body: %+v, want %+v", v, want)
  74. }
  75. })
  76. _, err := client.Bucket.PutTagging(context.Background(), opt)
  77. if err != nil {
  78. t.Fatalf("Bucket.PutTagging returned error: %v", err)
  79. }
  80. }
  81. func TestBucketService_DeleteTagging(t *testing.T) {
  82. setup()
  83. defer teardown()
  84. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  85. testMethod(t, r, http.MethodDelete)
  86. vs := values{
  87. "tagging": "",
  88. }
  89. testFormValues(t, r, vs)
  90. w.WriteHeader(http.StatusNoContent)
  91. })
  92. _, err := client.Bucket.DeleteTagging(context.Background())
  93. if err != nil {
  94. t.Fatalf("Bucket.DeleteTagging returned error: %v", err)
  95. }
  96. }