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.

138 lines
3.3 KiB

  1. package cos
  2. import (
  3. "context"
  4. "encoding/xml"
  5. "fmt"
  6. "net/http"
  7. "reflect"
  8. "testing"
  9. )
  10. func TestBucketService_GetCORS(t *testing.T) {
  11. setup()
  12. defer teardown()
  13. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  14. testMethod(t, r, http.MethodGet)
  15. vs := values{
  16. "cors": "",
  17. }
  18. testFormValues(t, r, vs)
  19. fmt.Fprint(w, `<?xml version='1.0' encoding='utf-8' ?>
  20. <CORSConfiguration>
  21. <CORSRule>
  22. <AllowedOrigin>http://www.qq.com</AllowedOrigin>
  23. <AllowedMethod>PUT</AllowedMethod>
  24. <AllowedMethod>GET</AllowedMethod>
  25. <AllowedHeader>x-cos-meta-test</AllowedHeader>
  26. <AllowedHeader>x-cos-xx</AllowedHeader>
  27. <ExposeHeader>x-cos-meta-test1</ExposeHeader>
  28. <MaxAgeSeconds>500</MaxAgeSeconds>
  29. </CORSRule>
  30. <CORSRule>
  31. <ID>1234</ID>
  32. <AllowedOrigin>http://www.baidu.com</AllowedOrigin>
  33. <AllowedOrigin>twitter.com</AllowedOrigin>
  34. <AllowedMethod>PUT</AllowedMethod>
  35. <AllowedMethod>GET</AllowedMethod>
  36. <MaxAgeSeconds>500</MaxAgeSeconds>
  37. </CORSRule>
  38. </CORSConfiguration>`)
  39. })
  40. ref, _, err := client.Bucket.GetCORS(context.Background())
  41. if err != nil {
  42. t.Fatalf("Bucket.GetCORS returned error: %v", err)
  43. }
  44. want := &BucketGetCORSResult{
  45. XMLName: xml.Name{Local: "CORSConfiguration"},
  46. Rules: []BucketCORSRule{
  47. {
  48. AllowedOrigins: []string{"http://www.qq.com"},
  49. AllowedMethods: []string{"PUT", "GET"},
  50. AllowedHeaders: []string{"x-cos-meta-test", "x-cos-xx"},
  51. MaxAgeSeconds: 500,
  52. ExposeHeaders: []string{"x-cos-meta-test1"},
  53. },
  54. {
  55. ID: "1234",
  56. AllowedOrigins: []string{"http://www.baidu.com", "twitter.com"},
  57. AllowedMethods: []string{"PUT", "GET"},
  58. MaxAgeSeconds: 500,
  59. },
  60. },
  61. }
  62. if !reflect.DeepEqual(ref, want) {
  63. t.Errorf("Bucket.GetLifecycle returned %+v, want %+v", ref, want)
  64. }
  65. }
  66. func TestBucketService_PutCORS(t *testing.T) {
  67. setup()
  68. defer teardown()
  69. opt := &BucketPutCORSOptions{
  70. Rules: []BucketCORSRule{
  71. {
  72. AllowedOrigins: []string{"http://www.qq.com"},
  73. AllowedMethods: []string{"PUT", "GET"},
  74. AllowedHeaders: []string{"x-cos-meta-test", "x-cos-xx"},
  75. MaxAgeSeconds: 500,
  76. ExposeHeaders: []string{"x-cos-meta-test1"},
  77. },
  78. {
  79. ID: "1234",
  80. AllowedOrigins: []string{"http://www.baidu.com", "twitter.com"},
  81. AllowedMethods: []string{"PUT", "GET"},
  82. MaxAgeSeconds: 500,
  83. },
  84. },
  85. }
  86. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  87. v := new(BucketPutCORSOptions)
  88. xml.NewDecoder(r.Body).Decode(v)
  89. testMethod(t, r, http.MethodPut)
  90. vs := values{
  91. "cors": "",
  92. }
  93. testFormValues(t, r, vs)
  94. want := opt
  95. want.XMLName = xml.Name{Local: "CORSConfiguration"}
  96. if !reflect.DeepEqual(v, want) {
  97. t.Errorf("Bucket.PutCORS request body: %+v, want %+v", v, want)
  98. }
  99. })
  100. _, err := client.Bucket.PutCORS(context.Background(), opt)
  101. if err != nil {
  102. t.Fatalf("Bucket.PutCORS returned error: %v", err)
  103. }
  104. }
  105. func TestBucketService_DeleteCORS(t *testing.T) {
  106. setup()
  107. defer teardown()
  108. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  109. testMethod(t, r, http.MethodDelete)
  110. vs := values{
  111. "cors": "",
  112. }
  113. testFormValues(t, r, vs)
  114. w.WriteHeader(http.StatusNoContent)
  115. })
  116. _, err := client.Bucket.DeleteCORS(context.Background())
  117. if err != nil {
  118. t.Fatalf("Bucket.DeleteCORS returned error: %v", err)
  119. }
  120. }