File: local_test.go

package info (click to toggle)
golang-github-aws-aws-sdk-go-v2 1.30.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 662,428 kB
  • sloc: java: 16,875; makefile: 432; sh: 175
file content (70 lines) | stat: -rw-r--r-- 1,472 bytes parent folder | download | duplicates (5)
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
package config

import (
	"fmt"
	"strings"
	"testing"

	"github.com/aws/aws-sdk-go-v2/internal/awstesting"
)

func TestValidateLocalURL(t *testing.T) {
	origFn := lookupHostFn
	defer func() { lookupHostFn = origFn }()

	lookupHostFn = func(host string) ([]string, error) {
		m := map[string]struct {
			Addrs []string
			Err   error
		}{
			"localhost":       {Addrs: []string{"::1", "127.0.0.1"}},
			"actuallylocal":   {Addrs: []string{"127.0.0.2"}},
			"notlocal":        {Addrs: []string{"::1", "127.0.0.1", "192.168.1.10"}},
			"www.example.com": {Addrs: []string{"10.10.10.10"}},
		}

		h, ok := m[host]
		if !ok {
			return nil, fmt.Errorf("unknown host")
		}

		return h.Addrs, h.Err
	}

	cases := []struct {
		Host string
		Fail bool
	}{
		{"localhost", false},
		{"actuallylocal", false},
		{"127.0.0.1", false},
		{"127.1.1.1", false},
		{"[::1]", false},
		{"www.example.com", true},
		{"169.254.170.2", true},
	}

	restoreEnv := awstesting.StashEnv()
	defer awstesting.PopEnv(restoreEnv)

	for _, c := range cases {
		t.Run(c.Host, func(t *testing.T) {
			u := fmt.Sprintf("http://%s/abc/123", c.Host)

			err := validateLocalURL(u)
			if c.Fail {
				if err == nil {
					t.Fatalf("expect error, got none")
				} else {
					if e, a := "invalid endpoint host", err.Error(); !strings.Contains(a, e) {
						t.Errorf("expect %s to be in %s", e, a)
					}
				}
			} else {
				if err != nil {
					t.Fatalf("expect no error, got %v", err)
				}
			}
		})
	}
}