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.

180 lines
4.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_GetOrigin(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. "origin": "",
  17. }
  18. testFormValues(t, r, vs)
  19. fmt.Fprint(w, `<OriginConfiguration>
  20. <OriginRule>
  21. <RulePriority>1</RulePriority>
  22. <OriginType>Mirror</OriginType>
  23. <OriginCondition>
  24. <HTTPStatusCode>404</HTTPStatusCode>
  25. <Prefix></Prefix>
  26. </OriginCondition>
  27. <OriginParameter>
  28. <Protocol>HTTP</Protocol>
  29. <FollowQueryString>true</FollowQueryString>
  30. <HttpHeader>
  31. <NewHttpHeaders>
  32. <Header>
  33. <Key>x-cos</Key>
  34. <Value>exampleHeader</Value>
  35. </Header>
  36. </NewHttpHeaders>
  37. <FollowHttpHeaders>
  38. <Header>
  39. <Key>exampleHeaderKey</Key>
  40. </Header>
  41. </FollowHttpHeaders>
  42. </HttpHeader>
  43. <FollowRedirection>true</FollowRedirection>
  44. <HttpRedirectCode>302</HttpRedirectCode>
  45. </OriginParameter>
  46. <OriginInfo>
  47. <HostInfo>
  48. <HostName>examplebucket-1250000000.cos.ap-shanghai.myqcloud.com</HostName>
  49. </HostInfo>
  50. </OriginInfo>
  51. </OriginRule>
  52. </OriginConfiguration>
  53. `)
  54. })
  55. res, _, err := client.Bucket.GetOrigin(context.Background())
  56. if err != nil {
  57. t.Fatalf("Bucket.GetOrigin returned error %v", err)
  58. }
  59. want := &BucketGetOriginResult{
  60. XMLName: xml.Name{Local: "OriginConfiguration"},
  61. Rule: []BucketOriginRule{
  62. {
  63. OriginType: "Mirror",
  64. RulePriority: 1,
  65. OriginCondition: &BucketOriginCondition{
  66. HTTPStatusCode: "404",
  67. },
  68. OriginParameter: &BucketOriginParameter{
  69. Protocol: "HTTP",
  70. FollowQueryString: true,
  71. HttpHeader: &BucketOriginHttpHeader{
  72. FollowHttpHeaders: []OriginHttpHeader{
  73. {
  74. Key: "exampleHeaderKey",
  75. },
  76. },
  77. NewHttpHeaders: []OriginHttpHeader{
  78. {
  79. Key: "x-cos",
  80. Value: "exampleHeader",
  81. },
  82. },
  83. },
  84. FollowRedirection: true,
  85. HttpRedirectCode: "302",
  86. },
  87. OriginInfo: &BucketOriginInfo{
  88. HostInfo: "examplebucket-1250000000.cos.ap-shanghai.myqcloud.com",
  89. },
  90. },
  91. },
  92. }
  93. if !reflect.DeepEqual(res, want) {
  94. t.Errorf("Bucket.GetOrigin returned %+v, want %+v", res, want)
  95. }
  96. }
  97. func TestBucketService_PutOrigin(t *testing.T) {
  98. setup()
  99. defer teardown()
  100. opt := &BucketPutOriginOptions{
  101. XMLName: xml.Name{Local: "OriginConfiguration"},
  102. Rule: []BucketOriginRule{
  103. {
  104. OriginType: "Mirror",
  105. RulePriority: 1,
  106. OriginCondition: &BucketOriginCondition{
  107. HTTPStatusCode: "404",
  108. },
  109. OriginParameter: &BucketOriginParameter{
  110. Protocol: "HTTP",
  111. FollowQueryString: true,
  112. HttpHeader: &BucketOriginHttpHeader{
  113. FollowHttpHeaders: []OriginHttpHeader{
  114. {
  115. Key: "exampleHeaderKey",
  116. },
  117. },
  118. NewHttpHeaders: []OriginHttpHeader{
  119. {
  120. Key: "x-cos",
  121. Value: "exampleHeader",
  122. },
  123. },
  124. },
  125. FollowRedirection: true,
  126. HttpRedirectCode: "302",
  127. },
  128. OriginInfo: &BucketOriginInfo{
  129. HostInfo: "examplebucket-1250000000.cos.ap-shanghai.myqcloud.com",
  130. },
  131. },
  132. },
  133. }
  134. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  135. testMethod(t, r, "PUT")
  136. vs := values{
  137. "origin": "",
  138. }
  139. testFormValues(t, r, vs)
  140. body := new(BucketPutOriginOptions)
  141. xml.NewDecoder(r.Body).Decode(body)
  142. want := opt
  143. want.XMLName = xml.Name{Local: "OriginConfiguration"}
  144. if !reflect.DeepEqual(body, want) {
  145. t.Errorf("Bucket.PutOrigin request\n body: %+v\n, want %+v\n", body, want)
  146. }
  147. })
  148. _, err := client.Bucket.PutOrigin(context.Background(), opt)
  149. if err != nil {
  150. t.Fatalf("Bucket.PutOrigin returned error: %v", err)
  151. }
  152. }
  153. func TestBucketService_DeleteOrigin(t *testing.T) {
  154. setup()
  155. defer teardown()
  156. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  157. testMethod(t, r, http.MethodDelete)
  158. vs := values{
  159. "origin": "",
  160. }
  161. testFormValues(t, r, vs)
  162. w.WriteHeader(http.StatusNoContent)
  163. })
  164. _, err := client.Bucket.DeleteOrigin(context.Background())
  165. if err != nil {
  166. t.Fatalf("Bucket.DeleteOrigin returned error: %v", err)
  167. }
  168. }