github.com/aavshr/aws-sdk-go@v1.41.3/service/s3/bucket_location_test.go (about)

     1  package s3_test
     2  
     3  import (
     4  	"bytes"
     5  	"io/ioutil"
     6  	"net/http"
     7  	"testing"
     8  
     9  	"github.com/aavshr/aws-sdk-go/aws"
    10  	"github.com/aavshr/aws-sdk-go/aws/awsutil"
    11  	"github.com/aavshr/aws-sdk-go/aws/request"
    12  	"github.com/aavshr/aws-sdk-go/awstesting/unit"
    13  	"github.com/aavshr/aws-sdk-go/service/s3"
    14  )
    15  
    16  var s3LocationTests = []struct {
    17  	body string
    18  	loc  string
    19  }{
    20  	{`<?xml version="1.0" encoding="UTF-8"?><LocationConstraint xmlns="http://s3.amazonaws.com/doc/2006-03-01/"/>`, ``},
    21  	{`<?xml version="1.0" encoding="UTF-8"?><LocationConstraint xmlns="http://s3.amazonaws.com/doc/2006-03-01/">EU</LocationConstraint>`, `EU`},
    22  }
    23  
    24  func TestGetBucketLocation(t *testing.T) {
    25  	for _, test := range s3LocationTests {
    26  		s := s3.New(unit.Session)
    27  		s.Handlers.Send.Clear()
    28  		s.Handlers.Send.PushBack(func(r *request.Request) {
    29  			reader := ioutil.NopCloser(bytes.NewReader([]byte(test.body)))
    30  			r.HTTPResponse = &http.Response{StatusCode: 200, Body: reader}
    31  		})
    32  
    33  		resp, err := s.GetBucketLocation(&s3.GetBucketLocationInput{Bucket: aws.String("bucket")})
    34  		if err != nil {
    35  			t.Errorf("expected no error, but received %v", err)
    36  		}
    37  
    38  		if test.loc == "" {
    39  			if v := resp.LocationConstraint; v != nil {
    40  				t.Errorf("expect location constraint to be nil, got %s", *v)
    41  			}
    42  		} else {
    43  			if e, a := test.loc, *resp.LocationConstraint; e != a {
    44  				t.Errorf("expect %s location constraint, got %v", e, a)
    45  			}
    46  		}
    47  	}
    48  }
    49  
    50  func TestNormalizeBucketLocation(t *testing.T) {
    51  	cases := []struct {
    52  		In, Out string
    53  	}{
    54  		{"", "us-east-1"},
    55  		{"EU", "eu-west-1"},
    56  		{"us-east-1", "us-east-1"},
    57  		{"something", "something"},
    58  	}
    59  
    60  	for i, c := range cases {
    61  		actual := s3.NormalizeBucketLocation(c.In)
    62  		if e, a := c.Out, actual; e != a {
    63  			t.Errorf("%d, expect %s bucket location, got %s", i, e, a)
    64  		}
    65  	}
    66  }
    67  
    68  func TestWithNormalizeBucketLocation(t *testing.T) {
    69  	req := &request.Request{}
    70  	req.ApplyOptions(s3.WithNormalizeBucketLocation)
    71  
    72  	cases := []struct {
    73  		In, Out string
    74  	}{
    75  		{"", "us-east-1"},
    76  		{"EU", "eu-west-1"},
    77  		{"us-east-1", "us-east-1"},
    78  		{"something", "something"},
    79  	}
    80  
    81  	for i, c := range cases {
    82  		req.Data = &s3.GetBucketLocationOutput{
    83  			LocationConstraint: aws.String(c.In),
    84  		}
    85  		req.Handlers.Unmarshal.Run(req)
    86  
    87  		v := req.Data.(*s3.GetBucketLocationOutput).LocationConstraint
    88  		if e, a := c.Out, aws.StringValue(v); e != a {
    89  			t.Errorf("%d, expect %s bucket location, got %s", i, e, a)
    90  		}
    91  	}
    92  }
    93  
    94  func TestPopulateLocationConstraint(t *testing.T) {
    95  	s := s3.New(unit.Session)
    96  	in := &s3.CreateBucketInput{
    97  		Bucket: aws.String("bucket"),
    98  	}
    99  	req, _ := s.CreateBucketRequest(in)
   100  	if err := req.Build(); err != nil {
   101  		t.Fatalf("expect no error, got %v", err)
   102  	}
   103  
   104  	v, _ := awsutil.ValuesAtPath(req.Params, "CreateBucketConfiguration.LocationConstraint")
   105  	if e, a := "mock-region", *(v[0].(*string)); e != a {
   106  		t.Errorf("expect %s location constraint, got %s", e, a)
   107  	}
   108  	if v := in.CreateBucketConfiguration; v != nil {
   109  		// don't modify original params
   110  		t.Errorf("expect create bucket Configuration to be nil, got %s", *v)
   111  	}
   112  }
   113  
   114  func TestNoPopulateLocationConstraintIfProvided(t *testing.T) {
   115  	s := s3.New(unit.Session)
   116  	req, _ := s.CreateBucketRequest(&s3.CreateBucketInput{
   117  		Bucket:                    aws.String("bucket"),
   118  		CreateBucketConfiguration: &s3.CreateBucketConfiguration{},
   119  	})
   120  	if err := req.Build(); err != nil {
   121  		t.Fatalf("expect no error, got %v", err)
   122  	}
   123  	v, _ := awsutil.ValuesAtPath(req.Params, "CreateBucketConfiguration.LocationConstraint")
   124  	if l := len(v); l != 0 {
   125  		t.Errorf("expect no values, got %d", l)
   126  	}
   127  }
   128  
   129  func TestNoPopulateLocationConstraintIfClassic(t *testing.T) {
   130  	s := s3.New(unit.Session, &aws.Config{Region: aws.String("us-east-1")})
   131  	req, _ := s.CreateBucketRequest(&s3.CreateBucketInput{
   132  		Bucket: aws.String("bucket"),
   133  	})
   134  	if err := req.Build(); err != nil {
   135  		t.Fatalf("expect no error, got %v", err)
   136  	}
   137  	v, _ := awsutil.ValuesAtPath(req.Params, "CreateBucketConfiguration.LocationConstraint")
   138  	if l := len(v); l != 0 {
   139  		t.Errorf("expect no values, got %d", l)
   140  	}
   141  }