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.

172 lines
3.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_GetWebsite(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. "website": "",
  17. }
  18. testFormValues(t, r, vs)
  19. fmt.Fprint(w, `<WebsiteConfiguration>
  20. <IndexDocument>
  21. <Suffix>index.html</Suffix>
  22. </IndexDocument>
  23. <RedirectAllRequestsTo>
  24. <Protocol>https</Protocol>
  25. </RedirectAllRequestsTo>
  26. <RoutingRules>
  27. <RoutingRule>
  28. <Condition>
  29. <HttpErrorCodeReturnedEquals>404</HttpErrorCodeReturnedEquals>
  30. </Condition>
  31. <Redirect>
  32. <Protocol>https</Protocol>
  33. <ReplaceKeyWith>404.html</ReplaceKeyWith>
  34. </Redirect>
  35. </RoutingRule>
  36. <RoutingRule>
  37. <Condition>
  38. <KeyPrefixEquals>docs/</KeyPrefixEquals>
  39. </Condition>
  40. <Redirect>
  41. <Protocol>https</Protocol>
  42. <ReplaceKeyPrefixWith>documents/</ReplaceKeyPrefixWith>
  43. </Redirect>
  44. </RoutingRule>
  45. <RoutingRule>
  46. <Condition>
  47. <KeyPrefixEquals>img/</KeyPrefixEquals>
  48. </Condition>
  49. <Redirect>
  50. <Protocol>https</Protocol>
  51. <ReplaceKeyWith>demo.jpg</ReplaceKeyWith>
  52. </Redirect>
  53. </RoutingRule>
  54. </RoutingRules>
  55. </WebsiteConfiguration>`)
  56. })
  57. res, _, err := client.Bucket.GetWebsite(context.Background())
  58. if err != nil {
  59. t.Fatalf("Bucket.GetWebsite returned error %v", err)
  60. }
  61. want := &BucketGetWebsiteResult{
  62. XMLName: xml.Name{Local: "WebsiteConfiguration"},
  63. Index: "index.html",
  64. RedirectProtocol: &RedirectRequestsProtocol{
  65. "https",
  66. },
  67. RoutingRules: &WebsiteRoutingRules{
  68. Rules: []WebsiteRoutingRule{
  69. {
  70. ConditionErrorCode: "404",
  71. RedirectProtocol: "https",
  72. RedirectReplaceKey: "404.html",
  73. },
  74. {
  75. ConditionPrefix: "docs/",
  76. RedirectProtocol: "https",
  77. RedirectReplaceKeyPrefix: "documents/",
  78. },
  79. {
  80. ConditionPrefix: "img/",
  81. RedirectProtocol: "https",
  82. RedirectReplaceKey: "demo.jpg",
  83. },
  84. },
  85. },
  86. }
  87. if !reflect.DeepEqual(res, want) {
  88. t.Errorf("Bucket.GetWebsite returned %+v, want %+v", res, want)
  89. }
  90. }
  91. func TestBucketService_PutWebsite(t *testing.T) {
  92. setup()
  93. defer teardown()
  94. opt := &BucketPutWebsiteOptions{
  95. Index: "index.html",
  96. RedirectProtocol: &RedirectRequestsProtocol{
  97. "https",
  98. },
  99. Error: &ErrorDocument{
  100. "Error.html",
  101. },
  102. RoutingRules: &WebsiteRoutingRules{
  103. []WebsiteRoutingRule{
  104. {
  105. ConditionErrorCode: "404",
  106. RedirectProtocol: "https",
  107. RedirectReplaceKey: "404.html",
  108. },
  109. {
  110. ConditionPrefix: "docs/",
  111. RedirectProtocol: "https",
  112. RedirectReplaceKeyPrefix: "documents/",
  113. },
  114. {
  115. ConditionPrefix: "img/",
  116. RedirectProtocol: "https",
  117. RedirectReplaceKey: "demo.jpg",
  118. },
  119. },
  120. },
  121. }
  122. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  123. testMethod(t, r, "PUT")
  124. vs := values{
  125. "website": "",
  126. }
  127. testFormValues(t, r, vs)
  128. body := new(BucketPutWebsiteOptions)
  129. xml.NewDecoder(r.Body).Decode(body)
  130. want := opt
  131. want.XMLName = xml.Name{Local: "WebsiteConfiguration"}
  132. if !reflect.DeepEqual(body, want) {
  133. t.Errorf("Bucket.PutWebsite request\n body: %+v\n, want %+v\n", body, want)
  134. }
  135. })
  136. _, err := client.Bucket.PutWebsite(context.Background(), opt)
  137. if err != nil {
  138. t.Fatalf("Bucket.PutWebsite returned error: %v", err)
  139. }
  140. }
  141. func TestBucketService_DeleteWebsite(t *testing.T) {
  142. setup()
  143. defer teardown()
  144. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  145. testMethod(t, r, http.MethodDelete)
  146. vs := values{
  147. "website": "",
  148. }
  149. testFormValues(t, r, vs)
  150. w.WriteHeader(http.StatusNoContent)
  151. })
  152. _, err := client.Bucket.DeleteWebsite(context.Background())
  153. if err != nil {
  154. t.Fatalf("Bucket.DeleteWebsite returned error: %v", err)
  155. }
  156. }