...
1 package config
2
3 import (
4 "fmt"
5 "strings"
6 "testing"
7
8 "github.com/aws/aws-sdk-go-v2/internal/awstesting"
9 )
10
11 func TestValidateLocalURL(t *testing.T) {
12 origFn := lookupHostFn
13 defer func() { lookupHostFn = origFn }()
14
15 lookupHostFn = func(host string) ([]string, error) {
16 m := map[string]struct {
17 Addrs []string
18 Err error
19 }{
20 "localhost": {Addrs: []string{"::1", "127.0.0.1"}},
21 "actuallylocal": {Addrs: []string{"127.0.0.2"}},
22 "notlocal": {Addrs: []string{"::1", "127.0.0.1", "192.168.1.10"}},
23 "www.example.com": {Addrs: []string{"10.10.10.10"}},
24 }
25
26 h, ok := m[host]
27 if !ok {
28 return nil, fmt.Errorf("unknown host")
29 }
30
31 return h.Addrs, h.Err
32 }
33
34 cases := []struct {
35 Host string
36 Fail bool
37 }{
38 {"localhost", false},
39 {"actuallylocal", false},
40 {"127.0.0.1", false},
41 {"127.1.1.1", false},
42 {"[::1]", false},
43 {"www.example.com", true},
44 {"169.254.170.2", true},
45 }
46
47 restoreEnv := awstesting.StashEnv()
48 defer awstesting.PopEnv(restoreEnv)
49
50 for _, c := range cases {
51 t.Run(c.Host, func(t *testing.T) {
52 u := fmt.Sprintf("http://%s/abc/123", c.Host)
53
54 err := validateLocalURL(u)
55 if c.Fail {
56 if err == nil {
57 t.Fatalf("expect error, got none")
58 } else {
59 if e, a := "invalid endpoint host", err.Error(); !strings.Contains(a, e) {
60 t.Errorf("expect %s to be in %s", e, a)
61 }
62 }
63 } else {
64 if err != nil {
65 t.Fatalf("expect no error, got %v", err)
66 }
67 }
68 })
69 }
70 }
71
View as plain text