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.

100 lines
2.2 KiB

4 years ago
4 years ago
  1. package cos
  2. import (
  3. "context"
  4. "encoding/xml"
  5. "fmt"
  6. "net/http"
  7. "reflect"
  8. "testing"
  9. )
  10. func TestBucketService_GetEncryption(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. "encryption": "",
  17. }
  18. testFormValues(t, r, vs)
  19. fmt.Fprint(w, `<ServerSideEncryptionConfiguration>
  20. <Rule>
  21. <ApplyServerSideEncryptionByDefault>
  22. <SSEAlgorithm>AES256</SSEAlgorithm>
  23. </ApplyServerSideEncryptionByDefault>
  24. </Rule>
  25. </ServerSideEncryptionConfiguration>`)
  26. })
  27. res, _, err := client.Bucket.GetEncryption(context.Background())
  28. if err != nil {
  29. t.Fatalf("Bucket.GetEncryption returned error %v", err)
  30. }
  31. want := &BucketGetEncryptionResult{
  32. XMLName: xml.Name{Local: "ServerSideEncryptionConfiguration"},
  33. Rule: &BucketEncryptionConfiguration{
  34. SSEAlgorithm: "AES256",
  35. },
  36. }
  37. if !reflect.DeepEqual(res, want) {
  38. t.Errorf("Bucket.GetEncryption returned %+v, want %+v", res, want)
  39. }
  40. }
  41. func TestBucketService_PutEncryption(t *testing.T) {
  42. setup()
  43. defer teardown()
  44. opt := &BucketPutEncryptionOptions{
  45. Rule: &BucketEncryptionConfiguration{
  46. SSEAlgorithm: "AES256",
  47. },
  48. }
  49. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  50. testMethod(t, r, "PUT")
  51. vs := values{
  52. "encryption": "",
  53. }
  54. testFormValues(t, r, vs)
  55. body := new(BucketPutEncryptionOptions)
  56. xml.NewDecoder(r.Body).Decode(body)
  57. want := opt
  58. want.XMLName = xml.Name{Local: "ServerSideEncryptionConfiguration"}
  59. if !reflect.DeepEqual(body, want) {
  60. t.Errorf("Bucket.PutEncryption request\n body: %+v\n, want %+v\n", body, want)
  61. }
  62. })
  63. _, err := client.Bucket.PutEncryption(context.Background(), opt)
  64. if err != nil {
  65. t.Fatalf("Bucket.PutEncryption returned error: %v", err)
  66. }
  67. }
  68. func TestBucketService_DeleteEncryption(t *testing.T) {
  69. setup()
  70. defer teardown()
  71. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  72. testMethod(t, r, http.MethodDelete)
  73. vs := values{
  74. "encryption": "",
  75. }
  76. testFormValues(t, r, vs)
  77. w.WriteHeader(http.StatusNoContent)
  78. })
  79. _, err := client.Bucket.DeleteEncryption(context.Background())
  80. if err != nil {
  81. t.Fatalf("Bucket.DeleteEncryption returned error: %v", err)
  82. }
  83. }