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.

129 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_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. Status: "ENABLED",
  40. Name: "www.abc.com",
  41. Type: "REST",
  42. ForcedReplacement: "CNAME",
  43. }
  44. if !reflect.DeepEqual(res, want) {
  45. t.Errorf("Bucket.GetDomain returned %+v, want %+v", res, want)
  46. }
  47. }
  48. func TestBucketService_PutDomain(t *testing.T) {
  49. setup()
  50. defer teardown()
  51. opt := &BucketPutDomainOptions{
  52. XMLName: xml.Name{Local: "DomainConfiguration"},
  53. Status: "ENABLED",
  54. Name: "www.abc.com",
  55. Type: "REST",
  56. ForcedReplacement: "CNAME",
  57. }
  58. rt := 0
  59. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  60. testMethod(t, r, "PUT")
  61. vs := values{
  62. "domain": "",
  63. }
  64. testFormValues(t, r, vs)
  65. rt++
  66. if rt < 3 {
  67. w.WriteHeader(http.StatusGatewayTimeout)
  68. }
  69. body := new(BucketPutDomainOptions)
  70. xml.NewDecoder(r.Body).Decode(body)
  71. want := opt
  72. want.XMLName = xml.Name{Local: "DomainConfiguration"}
  73. if !reflect.DeepEqual(body, want) {
  74. t.Errorf("Bucket.PutDomain request\n body: %+v\n, want %+v\n", body, want)
  75. }
  76. })
  77. _, err := client.Bucket.PutDomain(context.Background(), opt)
  78. if err != nil {
  79. t.Fatalf("Bucket.PutDomain returned error: %v", err)
  80. }
  81. }
  82. func TestBucketService_DeleteDomain(t *testing.T) {
  83. setup()
  84. defer teardown()
  85. opt := &BucketPutDomainOptions{}
  86. rt := 0
  87. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  88. testMethod(t, r, http.MethodPut)
  89. vs := values{
  90. "domain": "",
  91. }
  92. testFormValues(t, r, vs)
  93. rt++
  94. if rt < 3 {
  95. w.WriteHeader(http.StatusGatewayTimeout)
  96. return
  97. }
  98. body := new(BucketPutDomainOptions)
  99. xml.NewDecoder(r.Body).Decode(body)
  100. want := opt
  101. want.XMLName = xml.Name{Local: "DomainConfiguration"}
  102. if !reflect.DeepEqual(body, want) {
  103. t.Errorf("Bucket.PutDomain request\n body: %+v\n, want %+v\n", body, want)
  104. }
  105. w.WriteHeader(http.StatusNoContent)
  106. })
  107. _, err := client.Bucket.PutDomain(context.Background(), opt)
  108. if err != nil {
  109. t.Fatalf("Bucket.PutDomain returned error: %v", err)
  110. }
  111. }