customizations_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package s3_test
  2. import (
  3. "crypto/md5"
  4. "encoding/base64"
  5. "io/ioutil"
  6. "net/http"
  7. "net/http/httptest"
  8. "strings"
  9. "testing"
  10. "github.com/aws/aws-sdk-go/aws"
  11. "github.com/aws/aws-sdk-go/aws/request"
  12. "github.com/aws/aws-sdk-go/awstesting/unit"
  13. "github.com/aws/aws-sdk-go/service/s3"
  14. "github.com/stretchr/testify/assert"
  15. )
  16. func assertMD5(t *testing.T, req *request.Request) {
  17. err := req.Build()
  18. assert.NoError(t, err)
  19. b, _ := ioutil.ReadAll(req.HTTPRequest.Body)
  20. out := md5.Sum(b)
  21. assert.NotEmpty(t, b)
  22. assert.Equal(t, base64.StdEncoding.EncodeToString(out[:]), req.HTTPRequest.Header.Get("Content-MD5"))
  23. }
  24. func TestMD5InPutBucketCors(t *testing.T) {
  25. svc := s3.New(unit.Session)
  26. req, _ := svc.PutBucketCorsRequest(&s3.PutBucketCorsInput{
  27. Bucket: aws.String("bucketname"),
  28. CORSConfiguration: &s3.CORSConfiguration{
  29. CORSRules: []*s3.CORSRule{
  30. {
  31. AllowedMethods: []*string{aws.String("GET")},
  32. AllowedOrigins: []*string{aws.String("*")},
  33. },
  34. },
  35. },
  36. })
  37. assertMD5(t, req)
  38. }
  39. func TestMD5InPutBucketLifecycle(t *testing.T) {
  40. svc := s3.New(unit.Session)
  41. req, _ := svc.PutBucketLifecycleRequest(&s3.PutBucketLifecycleInput{
  42. Bucket: aws.String("bucketname"),
  43. LifecycleConfiguration: &s3.LifecycleConfiguration{
  44. Rules: []*s3.Rule{
  45. {
  46. ID: aws.String("ID"),
  47. Prefix: aws.String("Prefix"),
  48. Status: aws.String("Enabled"),
  49. },
  50. },
  51. },
  52. })
  53. assertMD5(t, req)
  54. }
  55. func TestMD5InPutBucketPolicy(t *testing.T) {
  56. svc := s3.New(unit.Session)
  57. req, _ := svc.PutBucketPolicyRequest(&s3.PutBucketPolicyInput{
  58. Bucket: aws.String("bucketname"),
  59. Policy: aws.String("{}"),
  60. })
  61. assertMD5(t, req)
  62. }
  63. func TestMD5InPutBucketTagging(t *testing.T) {
  64. svc := s3.New(unit.Session)
  65. req, _ := svc.PutBucketTaggingRequest(&s3.PutBucketTaggingInput{
  66. Bucket: aws.String("bucketname"),
  67. Tagging: &s3.Tagging{
  68. TagSet: []*s3.Tag{
  69. {Key: aws.String("KEY"), Value: aws.String("VALUE")},
  70. },
  71. },
  72. })
  73. assertMD5(t, req)
  74. }
  75. func TestMD5InDeleteObjects(t *testing.T) {
  76. svc := s3.New(unit.Session)
  77. req, _ := svc.DeleteObjectsRequest(&s3.DeleteObjectsInput{
  78. Bucket: aws.String("bucketname"),
  79. Delete: &s3.Delete{
  80. Objects: []*s3.ObjectIdentifier{
  81. {Key: aws.String("key")},
  82. },
  83. },
  84. })
  85. assertMD5(t, req)
  86. }
  87. func TestMD5InPutBucketLifecycleConfiguration(t *testing.T) {
  88. svc := s3.New(unit.Session)
  89. req, _ := svc.PutBucketLifecycleConfigurationRequest(&s3.PutBucketLifecycleConfigurationInput{
  90. Bucket: aws.String("bucketname"),
  91. LifecycleConfiguration: &s3.BucketLifecycleConfiguration{
  92. Rules: []*s3.LifecycleRule{
  93. {Prefix: aws.String("prefix"), Status: aws.String(s3.ExpirationStatusEnabled)},
  94. },
  95. },
  96. })
  97. assertMD5(t, req)
  98. }
  99. const (
  100. metaKeyPrefix = `X-Amz-Meta-`
  101. utf8KeySuffix = `My-Info`
  102. utf8Value = "hello-世界\u0444"
  103. )
  104. func TestPutObjectMetadataWithUnicode(t *testing.T) {
  105. server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  106. assert.Equal(t, utf8Value, r.Header.Get(metaKeyPrefix+utf8KeySuffix))
  107. }))
  108. svc := s3.New(unit.Session, &aws.Config{
  109. Endpoint: aws.String(server.URL),
  110. DisableSSL: aws.Bool(true),
  111. })
  112. _, err := svc.PutObject(&s3.PutObjectInput{
  113. Bucket: aws.String("my_bucket"),
  114. Key: aws.String("my_key"),
  115. Body: strings.NewReader(""),
  116. Metadata: func() map[string]*string {
  117. v := map[string]*string{}
  118. v[utf8KeySuffix] = aws.String(utf8Value)
  119. return v
  120. }(),
  121. })
  122. assert.NoError(t, err)
  123. }
  124. func TestGetObjectMetadataWithUnicode(t *testing.T) {
  125. server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  126. w.Header().Set(metaKeyPrefix+utf8KeySuffix, utf8Value)
  127. }))
  128. svc := s3.New(unit.Session, &aws.Config{
  129. Endpoint: aws.String(server.URL),
  130. DisableSSL: aws.Bool(true),
  131. })
  132. resp, err := svc.GetObject(&s3.GetObjectInput{
  133. Bucket: aws.String("my_bucket"),
  134. Key: aws.String("my_key"),
  135. })
  136. assert.NoError(t, err)
  137. resp.Body.Close()
  138. assert.Equal(t, utf8Value, *resp.Metadata[utf8KeySuffix])
  139. }