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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
package aws
import (
"strconv"
"testing"
)
type mockOptions struct {
Bool bool
Str string
DualStackEndpointState DualStackEndpointState
FIPSEndpointState FIPSEndpointState
}
func (m mockOptions) GetDisableHTTPS() bool {
return m.Bool
}
func (m mockOptions) GetUseDualStackEndpoint() DualStackEndpointState {
return m.DualStackEndpointState
}
func (m mockOptions) GetUseFIPSEndpoint() FIPSEndpointState {
return m.FIPSEndpointState
}
func (m mockOptions) GetResolvedRegion() string {
return m.Str
}
func TestGetDisableHTTPS(t *testing.T) {
cases := []struct {
Options []interface{}
ExpectFound bool
ExpectValue bool
}{
{
Options: []interface{}{struct{}{}},
},
{
Options: []interface{}{mockOptions{
Bool: false,
}},
ExpectFound: true,
ExpectValue: false,
},
{
Options: []interface{}{mockOptions{
Bool: true,
}},
ExpectFound: true,
ExpectValue: true,
},
{
Options: []interface{}{struct{}{}, mockOptions{Bool: true}, mockOptions{Bool: false}},
ExpectFound: true,
ExpectValue: true,
},
}
for i, tt := range cases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
value, found := GetDisableHTTPS(tt.Options...)
if found != tt.ExpectFound {
t.Fatalf("expect value to not be found")
}
if value != tt.ExpectValue {
t.Errorf("expect %v, got %v", tt.ExpectValue, value)
}
})
}
}
func TestGetResolvedRegion(t *testing.T) {
cases := []struct {
Options []interface{}
ExpectFound bool
ExpectValue string
}{
{
Options: []interface{}{struct{}{}},
},
{
Options: []interface{}{mockOptions{Str: ""}},
ExpectFound: true,
ExpectValue: "",
},
{
Options: []interface{}{mockOptions{Str: "foo"}},
ExpectFound: true,
ExpectValue: "foo",
},
{
Options: []interface{}{struct{}{}, mockOptions{Str: "bar"}, mockOptions{Str: "baz"}},
ExpectFound: true,
ExpectValue: "bar",
},
}
for i, tt := range cases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
value, found := GetResolvedRegion(tt.Options...)
if found != tt.ExpectFound {
t.Fatalf("expect value to not be found")
}
if value != tt.ExpectValue {
t.Errorf("expect %v, got %v", tt.ExpectValue, value)
}
})
}
}
func TestGetUseDualStackEndpoint(t *testing.T) {
cases := []struct {
Options []interface{}
ExpectFound bool
ExpectValue DualStackEndpointState
}{
{
Options: []interface{}{struct{}{}},
},
{
Options: []interface{}{mockOptions{DualStackEndpointState: DualStackEndpointStateUnset}},
ExpectFound: true,
ExpectValue: DualStackEndpointStateUnset,
},
{
Options: []interface{}{mockOptions{DualStackEndpointState: DualStackEndpointStateEnabled}},
ExpectFound: true,
ExpectValue: DualStackEndpointStateEnabled,
},
{
Options: []interface{}{struct{}{}, mockOptions{DualStackEndpointState: DualStackEndpointStateEnabled}, mockOptions{DualStackEndpointState: DualStackEndpointStateDisabled}},
ExpectFound: true,
ExpectValue: DualStackEndpointStateEnabled,
},
}
for i, tt := range cases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
value, found := GetUseDualStackEndpoint(tt.Options...)
if found != tt.ExpectFound {
t.Fatalf("expect value to not be found")
}
if value != tt.ExpectValue {
t.Errorf("expect %v, got %v", tt.ExpectValue, value)
}
})
}
}
func TestGetUseFIPSEndpoint(t *testing.T) {
cases := []struct {
Options []interface{}
ExpectFound bool
ExpectValue FIPSEndpointState
}{
{
Options: []interface{}{struct{}{}},
},
{
Options: []interface{}{mockOptions{FIPSEndpointState: FIPSEndpointStateUnset}},
ExpectFound: true,
ExpectValue: FIPSEndpointStateUnset,
},
{
Options: []interface{}{mockOptions{FIPSEndpointState: FIPSEndpointStateEnabled}},
ExpectFound: true,
ExpectValue: FIPSEndpointStateEnabled,
},
{
Options: []interface{}{struct{}{}, mockOptions{FIPSEndpointState: FIPSEndpointStateEnabled}, mockOptions{FIPSEndpointState: FIPSEndpointStateDisabled}},
ExpectFound: true,
ExpectValue: FIPSEndpointStateEnabled,
},
}
for i, tt := range cases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
value, found := GetUseFIPSEndpoint(tt.Options...)
if found != tt.ExpectFound {
t.Fatalf("expect value to not be found")
}
if value != tt.ExpectValue {
t.Errorf("expect %v, got %v", tt.ExpectValue, value)
}
})
}
}
var _ EndpointResolverWithOptions = EndpointResolverWithOptionsFunc(nil)
func TestEndpointResolverWithOptionsFunc_ResolveEndpoint(t *testing.T) {
var er EndpointResolverWithOptions = EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (Endpoint, error) {
if e, a := "foo", service; e != a {
t.Errorf("expect %v, got %v", e, a)
}
if e, a := "bar", region; e != a {
t.Errorf("expect %v, got %v", e, a)
}
if e, a := 2, len(options); e != a {
t.Errorf("expect %v, got %v", e, a)
}
return Endpoint{
URL: "https://foo.amazonaws.com",
}, nil
})
e, err := er.ResolveEndpoint("foo", "bar", 1, 2)
if err != nil {
t.Errorf("expect no error, got %v", err)
}
if e, a := "https://foo.amazonaws.com", e.URL; e != a {
t.Errorf("expect %v, got %v", e, a)
}
}
|