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.

120 lines
2.5 KiB

  1. package cos
  2. import (
  3. "context"
  4. "encoding/xml"
  5. "fmt"
  6. "net/http"
  7. "reflect"
  8. "testing"
  9. )
  10. func TestBucketService_GetDomain(t *testing.T) {
  11. setup()
  12. defer teardown()
  13. rt := 0
  14. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  15. testMethod(t, r, "GET")
  16. vs := values{
  17. "domain": "",
  18. }
  19. testFormValues(t, r, vs)
  20. rt++
  21. if rt < 3 {
  22. w.WriteHeader(http.StatusGatewayTimeout)
  23. }
  24. fmt.Fprint(w, `<DomainConfiguration>
  25. <DomainRule>
  26. <Status>ENABLED</Status>
  27. <Name>www.abc.com</Name>
  28. <Type>REST</Type>
  29. <ForcedReplacement>CNAME</ForcedReplacement>
  30. </DomainRule>
  31. </DomainConfiguration>`)
  32. })
  33. res, _, err := client.Bucket.GetDomain(context.Background())
  34. if err != nil {
  35. t.Fatalf("Bucket.GetDomain returned error %v", err)
  36. }
  37. want := &BucketGetDomainResult{
  38. XMLName: xml.Name{Local: "DomainConfiguration"},
  39. Rules: []BucketDomainRule{
  40. {
  41. Status: "ENABLED",
  42. Name: "www.abc.com",
  43. Type: "REST",
  44. ForcedReplacement: "CNAME",
  45. },
  46. },
  47. }
  48. if !reflect.DeepEqual(res, want) {
  49. t.Errorf("Bucket.GetDomain returned %+v, want %+v", res, want)
  50. }
  51. }
  52. func TestBucketService_PutDomain(t *testing.T) {
  53. setup()
  54. defer teardown()
  55. opt := &BucketPutDomainOptions{
  56. XMLName: xml.Name{Local: "DomainConfiguration"},
  57. Rules: []BucketDomainRule{
  58. {
  59. Status: "ENABLED",
  60. Name: "www.abc.com",
  61. Type: "REST",
  62. ForcedReplacement: "CNAME",
  63. },
  64. },
  65. }
  66. rt := 0
  67. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  68. testMethod(t, r, "PUT")
  69. vs := values{
  70. "domain": "",
  71. }
  72. testFormValues(t, r, vs)
  73. rt++
  74. if rt < 3 {
  75. w.WriteHeader(http.StatusGatewayTimeout)
  76. }
  77. body := new(BucketPutDomainOptions)
  78. xml.NewDecoder(r.Body).Decode(body)
  79. want := opt
  80. want.XMLName = xml.Name{Local: "DomainConfiguration"}
  81. if !reflect.DeepEqual(body, want) {
  82. t.Errorf("Bucket.PutDomain request\n body: %+v\n, want %+v\n", body, want)
  83. }
  84. })
  85. _, err := client.Bucket.PutDomain(context.Background(), opt)
  86. if err != nil {
  87. t.Fatalf("Bucket.PutDomain returned error: %v", err)
  88. }
  89. }
  90. func TestBucketService_DeleteDomain(t *testing.T) {
  91. setup()
  92. defer teardown()
  93. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  94. testMethod(t, r, http.MethodDelete)
  95. vs := values{
  96. "domain": "",
  97. }
  98. testFormValues(t, r, vs)
  99. w.WriteHeader(http.StatusNoContent)
  100. })
  101. _, err := client.Bucket.DeleteDomain(context.Background())
  102. if err != nil {
  103. t.Fatalf("Bucket.DeleteDomain returned error: %v", err)
  104. }
  105. }