bucket_location_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package s3_test
  2. import (
  3. "bytes"
  4. "io/ioutil"
  5. "net/http"
  6. "testing"
  7. "github.com/aws/aws-sdk-go/aws"
  8. "github.com/aws/aws-sdk-go/aws/awsutil"
  9. "github.com/aws/aws-sdk-go/aws/request"
  10. "github.com/aws/aws-sdk-go/awstesting/unit"
  11. "github.com/aws/aws-sdk-go/service/s3"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. var s3LocationTests = []struct {
  15. body string
  16. loc string
  17. }{
  18. {`<?xml version="1.0" encoding="UTF-8"?><LocationConstraint xmlns="http://s3.amazonaws.com/doc/2006-03-01/"/>`, ``},
  19. {`<?xml version="1.0" encoding="UTF-8"?><LocationConstraint xmlns="http://s3.amazonaws.com/doc/2006-03-01/">EU</LocationConstraint>`, `EU`},
  20. }
  21. func TestGetBucketLocation(t *testing.T) {
  22. for _, test := range s3LocationTests {
  23. s := s3.New(unit.Session)
  24. s.Handlers.Send.Clear()
  25. s.Handlers.Send.PushBack(func(r *request.Request) {
  26. reader := ioutil.NopCloser(bytes.NewReader([]byte(test.body)))
  27. r.HTTPResponse = &http.Response{StatusCode: 200, Body: reader}
  28. })
  29. resp, err := s.GetBucketLocation(&s3.GetBucketLocationInput{Bucket: aws.String("bucket")})
  30. assert.NoError(t, err)
  31. if test.loc == "" {
  32. assert.Nil(t, resp.LocationConstraint)
  33. } else {
  34. assert.Equal(t, test.loc, *resp.LocationConstraint)
  35. }
  36. }
  37. }
  38. func TestPopulateLocationConstraint(t *testing.T) {
  39. s := s3.New(unit.Session)
  40. in := &s3.CreateBucketInput{
  41. Bucket: aws.String("bucket"),
  42. }
  43. req, _ := s.CreateBucketRequest(in)
  44. err := req.Build()
  45. assert.NoError(t, err)
  46. v, _ := awsutil.ValuesAtPath(req.Params, "CreateBucketConfiguration.LocationConstraint")
  47. assert.Equal(t, "mock-region", *(v[0].(*string)))
  48. assert.Nil(t, in.CreateBucketConfiguration) // don't modify original params
  49. }
  50. func TestNoPopulateLocationConstraintIfProvided(t *testing.T) {
  51. s := s3.New(unit.Session)
  52. req, _ := s.CreateBucketRequest(&s3.CreateBucketInput{
  53. Bucket: aws.String("bucket"),
  54. CreateBucketConfiguration: &s3.CreateBucketConfiguration{},
  55. })
  56. err := req.Build()
  57. assert.NoError(t, err)
  58. v, _ := awsutil.ValuesAtPath(req.Params, "CreateBucketConfiguration.LocationConstraint")
  59. assert.Equal(t, 0, len(v))
  60. }
  61. func TestNoPopulateLocationConstraintIfClassic(t *testing.T) {
  62. s := s3.New(unit.Session, &aws.Config{Region: aws.String("us-east-1")})
  63. req, _ := s.CreateBucketRequest(&s3.CreateBucketInput{
  64. Bucket: aws.String("bucket"),
  65. })
  66. err := req.Build()
  67. assert.NoError(t, err)
  68. v, _ := awsutil.ValuesAtPath(req.Params, "CreateBucketConfiguration.LocationConstraint")
  69. assert.Equal(t, 0, len(v))
  70. }