@marcel91200 / caddy Mirror · read-only
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 11, 01:00 UTC

master
go 92 lines 2.51 KB
Raw
1 package reverseproxy
2
3 import "testing"
4
5 func TestResolveIpVersion(t *testing.T) {
6 falseBool := false
7 trueBool := true
8 tests := []struct {
9 Versions *IPVersions
10 expectedIpVersion string
11 }{
12 {
13 Versions: &IPVersions{IPv4: &trueBool},
14 expectedIpVersion: "ip4",
15 },
16 {
17 Versions: &IPVersions{IPv4: &falseBool},
18 expectedIpVersion: "ip",
19 },
20 {
21 Versions: &IPVersions{IPv4: &trueBool, IPv6: &falseBool},
22 expectedIpVersion: "ip4",
23 },
24 {
25 Versions: &IPVersions{IPv6: &trueBool},
26 expectedIpVersion: "ip6",
27 },
28 {
29 Versions: &IPVersions{IPv6: &falseBool},
30 expectedIpVersion: "ip",
31 },
32 {
33 Versions: &IPVersions{IPv6: &trueBool, IPv4: &falseBool},
34 expectedIpVersion: "ip6",
35 },
36 {
37 Versions: &IPVersions{},
38 expectedIpVersion: "ip",
39 },
40 {
41 Versions: &IPVersions{IPv4: &trueBool, IPv6: &trueBool},
42 expectedIpVersion: "ip",
43 },
44 {
45 Versions: &IPVersions{IPv4: &falseBool, IPv6: &falseBool},
46 expectedIpVersion: "ip",
47 },
48 }
49 for _, test := range tests {
50 ipVersion := resolveIpVersion(test.Versions)
51 if ipVersion != test.expectedIpVersion {
52 t.Errorf("resolveIpVersion(): Expected %s got %s", test.expectedIpVersion, ipVersion)
53 }
54 }
55 }
56
57 func TestResolverCacheKey(t *testing.T) {
58 key := func(addrs ...string) string {
59 if addrs == nil {
60 return resolverCacheKey(nil)
61 }
62 return resolverCacheKey(&UpstreamResolver{Addresses: addrs})
63 }
64
65 // The default resolver (nil or empty) yields the empty key, so default-resolver
66 // sources keep sharing the cache.
67 if got := key(); got != "" {
68 t.Errorf("nil resolver: got %q, want \"\"", got)
69 }
70 if got := resolverCacheKey(&UpstreamResolver{}); got != "" {
71 t.Errorf("empty resolver: got %q, want \"\"", got)
72 }
73
74 // Order does not matter: the same set maps to the same key.
75 if a, b := key("1.1.1.1", "8.8.8.8"), key("8.8.8.8", "1.1.1.1"); a != b {
76 t.Errorf("order should not matter: %q != %q", a, b)
77 }
78
79 // Different sets must not collide, even when an address contains the
80 // delimiter that a naive join would use. {"a,b"} and {"a","b"} are distinct
81 // resolver configurations and must get distinct keys.
82 colliding := [][2][]string{
83 {{"a,b"}, {"a", "b"}},
84 {{"unix//run/x,y.sock"}, {"unix//run/x", "y.sock"}},
85 {{"10:1"}, {"1", "0:1"}}, // guards the length-prefix encoding itself
86 }
87 for _, c := range colliding {
88 if k1, k2 := key(c[0]...), key(c[1]...); k1 == k2 {
89 t.Errorf("distinct address sets %q and %q share key %q", c[0], c[1], k1)
90 }
91 }
92 }