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.

113 lines
2.6 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. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  14. testMethod(t, r, "GET")
  15. vs := values{
  16. "domain": "",
  17. }
  18. testFormValues(t, r, vs)
  19. fmt.Fprint(w, `<DomainConfiguration>
  20. <DomainRule>
  21. <Status>ENABLED</Status>
  22. <Name>www.abc.com</Name>
  23. <Type>REST</Type>
  24. <ForcedReplacement>CNAME</ForcedReplacement>
  25. </DomainRule>
  26. </DomainConfiguration>`)
  27. })
  28. res, _, err := client.Bucket.GetDomain(context.Background())
  29. if err != nil {
  30. t.Fatalf("Bucket.GetDomain returned error %v", err)
  31. }
  32. want := &BucketGetDomainResult{
  33. XMLName: xml.Name{Local: "DomainConfiguration"},
  34. Status: "ENABLED",
  35. Name: "www.abc.com",
  36. Type: "REST",
  37. ForcedReplacement: "CNAME",
  38. }
  39. if !reflect.DeepEqual(res, want) {
  40. t.Errorf("Bucket.GetDomain returned %+v, want %+v", res, want)
  41. }
  42. }
  43. func TestBucketService_PutDomain(t *testing.T) {
  44. setup()
  45. defer teardown()
  46. opt := &BucketPutDomainOptions{
  47. XMLName: xml.Name{Local: "DomainConfiguration"},
  48. Status: "ENABLED",
  49. Name: "www.abc.com",
  50. Type: "REST",
  51. ForcedReplacement: "CNAME",
  52. }
  53. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  54. testMethod(t, r, "PUT")
  55. vs := values{
  56. "domain": "",
  57. }
  58. testFormValues(t, r, vs)
  59. body := new(BucketPutDomainOptions)
  60. xml.NewDecoder(r.Body).Decode(body)
  61. want := opt
  62. want.XMLName = xml.Name{Local: "DomainConfiguration"}
  63. if !reflect.DeepEqual(body, want) {
  64. t.Errorf("Bucket.PutDomain request\n body: %+v\n, want %+v\n", body, want)
  65. }
  66. })
  67. _, err := client.Bucket.PutDomain(context.Background(), opt)
  68. if err != nil {
  69. t.Fatalf("Bucket.PutDomain returned error: %v", err)
  70. }
  71. }
  72. func TestBucketService_DeleteDomain(t *testing.T) {
  73. setup()
  74. defer teardown()
  75. opt := &BucketPutDomainOptions{}
  76. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  77. testMethod(t, r, http.MethodPut)
  78. vs := values{
  79. "domain": "",
  80. }
  81. testFormValues(t, r, vs)
  82. body := new(BucketPutDomainOptions)
  83. xml.NewDecoder(r.Body).Decode(body)
  84. want := opt
  85. want.XMLName = xml.Name{Local: "DomainConfiguration"}
  86. if !reflect.DeepEqual(body, want) {
  87. t.Errorf("Bucket.PutDomain request\n body: %+v\n, want %+v\n", body, want)
  88. }
  89. w.WriteHeader(http.StatusNoContent)
  90. })
  91. _, err := client.Bucket.PutDomain(context.Background(), opt)
  92. if err != nil {
  93. t.Fatalf("Bucket.PutDomain returned error: %v", err)
  94. }
  95. }