1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
package customizations_test
import (
"context"
"errors"
"net/http"
"strings"
"testing"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/smithy-go"
)
func Test_EmptyResponse(t *testing.T) {
cases := map[string]struct {
response *http.Response
expectError bool
}{
"success case with no response body": {
response: &http.Response{
StatusCode: 200,
Body: asReadCloser(
``,
),
},
},
"error case with no response body": {
response: &http.Response{
StatusCode: 400,
Body: asReadCloser(
``,
),
},
expectError: true,
},
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
ctx, cancelFn := context.WithTimeout(context.Background(), 5*time.Second)
defer cancelFn()
cfg := aws.Config{
Region: "mock-region",
Retryer: func() aws.Retryer {
return aws.NopRetryer{}
},
}
client := s3.NewFromConfig(cfg,
func(options *s3.Options) {
options.UsePathStyle = true
options.HTTPClient = &mockHTTPClient{c.response}
},
)
params := &s3.HeadBucketInput{Bucket: aws.String("aws-sdk-go-data")}
_, err := client.HeadBucket(ctx, params)
if c.expectError {
var apiErr smithy.APIError
if !errors.As(err, &apiErr) {
t.Fatalf("expect error to be API error, was not, %v", err)
}
if len(apiErr.ErrorCode()) == 0 {
t.Errorf("expect non-empty error code")
}
if len(apiErr.ErrorMessage()) == 0 {
t.Errorf("expect non-empty error message")
}
} else {
if err != nil {
t.Errorf("expected no error, got %v", err.Error())
}
}
})
}
}
func TestBucketLocationPopulation(t *testing.T) {
cases := map[string]struct {
response *http.Response
expectLocation string
expectError string
}{
"empty location": {
response: &http.Response{
StatusCode: 200,
Body: asReadCloser(
`<?xml version="1.0" encoding="UTF-8"?><LocationConstraint xmlns="http://s3.amazonaws.com/doc/2006-03-01/"/>`,
),
},
expectLocation: "",
},
"EU location": {
response: &http.Response{
StatusCode: 200,
Body: asReadCloser(
`<?xml version="1.0" encoding="UTF-8"?><LocationConstraint xmlns="http://s3.amazonaws.com/doc/2006-03-01/">EU</LocationConstraint>`,
),
},
expectLocation: "EU",
},
"AfSouth1 location": {
response: &http.Response{
StatusCode: 200,
Body: asReadCloser(
`<?xml version="1.0" encoding="UTF-8"?><LocationConstraint xmlns="http://s3.amazonaws.com/doc/2006-03-01/">af-south-1</LocationConstraint>`,
),
},
expectLocation: "af-south-1",
},
"IncompleteResponse": {
response: &http.Response{
Body: asReadCloser(
`<?xml version="1.0" encoding="UTF-8"?><LocationConstraint xmlns="http://s3.amazonaws.com/doc/2006-03-01/">`,
),
},
expectError: "unexpected EOF",
},
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
ctx, cancelFn := context.WithTimeout(context.Background(), 5*time.Second)
defer cancelFn()
cfg := aws.Config{
Region: "us-east-1",
Retryer: func() aws.Retryer { return aws.NopRetryer{} },
}
client := s3.NewFromConfig(cfg, func(options *s3.Options) {
options.UsePathStyle = true
options.HTTPClient = &mockHTTPClient{c.response}
})
params := &s3.GetBucketLocationInput{
Bucket: aws.String("aws-sdk-go-data"),
}
resp, err := client.GetBucketLocation(ctx, params)
if len(c.expectError) != 0 && err == nil {
t.Fatal("expect error, got none")
}
if err != nil && len(c.expectError) == 0 {
t.Fatalf("expect no error, got %v", err)
} else {
if err != nil {
if !strings.Contains(err.Error(), c.expectError) {
t.Fatalf("expect error to be %v, got %v", err.Error(), c.expectError)
}
return
}
}
if e, a := c.expectLocation, resp.LocationConstraint; !strings.EqualFold(e, string(a)) {
t.Fatalf("expected location constraint to be deserialized as %v, got %v", e, a)
}
})
}
}
|