|
| 1 | +package gateway |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "k8s.io/utils/ptr" |
| 7 | + gatewayv1 "sigs.k8s.io/gateway-api/apis/v1" |
| 8 | +) |
| 9 | + |
| 10 | +func TestHostnameMatches(t *testing.T) { |
| 11 | + tests := []struct { |
| 12 | + name string |
| 13 | + listenerHostname gatewayv1.Hostname |
| 14 | + routeHostname gatewayv1.Hostname |
| 15 | + want bool |
| 16 | + }{ |
| 17 | + {"exact match", "foo.example.com", "foo.example.com", true}, |
| 18 | + {"listener wildcard, route exact match", "*.example.com", "foo.example.com", true}, |
| 19 | + {"listener wildcard, route exact non-match (diff domain)", "*.example.com", "foo.example.net", false}, |
| 20 | + {"listener wildcard, route exact non-match (domain itself)", "*.example.com", "example.com", false}, |
| 21 | + {"listener wildcard, route wildcard match (subdomain)", "*.foo.example.com", "*.example.com", true}, |
| 22 | + {"listener wildcard, route wildcard match (same)", "*.example.com", "*.example.com", true}, |
| 23 | + {"listener wildcard, route wildcard non-match", "*.example.com", "*.example.net", false}, |
| 24 | + {"listener exact, route wildcard match", "foo.example.com", "*.example.com", true}, |
| 25 | + {"listener exact, route wildcard non-match (diff domain)", "foo.example.net", "*.example.com", false}, |
| 26 | + {"listener exact, route wildcard non-match (domain itself)", "example.com", "*.example.com", false}, |
| 27 | + {"no wildcard, non-match", "foo.example.com", "bar.example.com", false}, |
| 28 | + {"no wildcard, non-match diff domain", "foo.example.com", "foo.example.net", false}, |
| 29 | + {"empty listener", "", "foo.example.com", false}, // Exact match fails |
| 30 | + {"empty route", "foo.example.com", "", false}, // Exact match fails |
| 31 | + {"both empty", "", "", true}, // Exact match |
| 32 | + {"listener wildcard, route empty", "*.example.com", "", false}, |
| 33 | + {"listener empty, route wildcard", "", "*.example.com", false}, |
| 34 | + {"complex wildcard listener match", "*.apps.example.com", "foo.bar.apps.example.com", true}, |
| 35 | + {"complex wildcard route match", "foo.bar.apps.example.com", "*.apps.example.com", true}, |
| 36 | + {"wildcard vs non-wildcard TLD", "*.com", "example.com", true}, // *.com matches example.com |
| 37 | + {"non-wildcard TLD vs wildcard", "example.com", "*.com", true}, // *.com matches example.com |
| 38 | + {"wildcard TLD vs TLD", "*.com", "com", false}, // *.com does not match com |
| 39 | + {"TLD vs wildcard TLD", "com", "*.com", false}, // *.com does not match com |
| 40 | + } |
| 41 | + |
| 42 | + for _, tt := range tests { |
| 43 | + t.Run(tt.name, func(t *testing.T) { |
| 44 | + if got := hostnameMatches(tt.listenerHostname, tt.routeHostname); got != tt.want { |
| 45 | + t.Errorf("hostnameMatches(%q, %q) = %v, want %v", tt.listenerHostname, tt.routeHostname, got, tt.want) |
| 46 | + } |
| 47 | + }) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func TestHostnamesIntersect(t *testing.T) { |
| 52 | + tests := []struct { |
| 53 | + name string |
| 54 | + listenerHostname *gatewayv1.Hostname |
| 55 | + routeHostnames []gatewayv1.Hostname |
| 56 | + want bool |
| 57 | + }{ |
| 58 | + { |
| 59 | + name: "empty route hostnames", |
| 60 | + listenerHostname: ptr.To(gatewayv1.Hostname("foo.example.com")), |
| 61 | + routeHostnames: []gatewayv1.Hostname{}, |
| 62 | + want: true, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "nil listener hostname", |
| 66 | + listenerHostname: nil, |
| 67 | + routeHostnames: []gatewayv1.Hostname{"foo.example.com"}, |
| 68 | + want: true, |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "empty listener hostname", |
| 72 | + listenerHostname: ptr.To(gatewayv1.Hostname("")), |
| 73 | + routeHostnames: []gatewayv1.Hostname{"foo.example.com"}, |
| 74 | + want: true, |
| 75 | + }, |
| 76 | + { |
| 77 | + name: "exact match intersection", |
| 78 | + listenerHostname: ptr.To(gatewayv1.Hostname("foo.example.com")), |
| 79 | + routeHostnames: []gatewayv1.Hostname{"bar.example.com", "foo.example.com"}, |
| 80 | + want: true, |
| 81 | + }, |
| 82 | + { |
| 83 | + name: "wildcard listener intersection", |
| 84 | + listenerHostname: ptr.To(gatewayv1.Hostname("*.example.com")), |
| 85 | + routeHostnames: []gatewayv1.Hostname{"bar.example.net", "foo.example.com"}, |
| 86 | + want: true, |
| 87 | + }, |
| 88 | + { |
| 89 | + name: "wildcard route intersection", |
| 90 | + listenerHostname: ptr.To(gatewayv1.Hostname("foo.example.com")), |
| 91 | + routeHostnames: []gatewayv1.Hostname{"bar.example.net", "*.example.com"}, |
| 92 | + want: true, |
| 93 | + }, |
| 94 | + { |
| 95 | + name: "wildcard both intersection", |
| 96 | + listenerHostname: ptr.To(gatewayv1.Hostname("*.example.com")), |
| 97 | + routeHostnames: []gatewayv1.Hostname{"bar.example.net", "*.apps.example.com"}, |
| 98 | + want: true, |
| 99 | + }, |
| 100 | + { |
| 101 | + name: "no intersection", |
| 102 | + listenerHostname: ptr.To(gatewayv1.Hostname("foo.example.com")), |
| 103 | + routeHostnames: []gatewayv1.Hostname{"bar.example.com", "baz.example.com"}, |
| 104 | + want: false, |
| 105 | + }, |
| 106 | + { |
| 107 | + name: "no intersection with wildcards", |
| 108 | + listenerHostname: ptr.To(gatewayv1.Hostname("*.example.net")), |
| 109 | + routeHostnames: []gatewayv1.Hostname{"bar.example.com", "*.apps.example.com"}, |
| 110 | + want: false, |
| 111 | + }, |
| 112 | + { |
| 113 | + name: "listener wildcard, route domain itself (no intersection)", |
| 114 | + listenerHostname: ptr.To(gatewayv1.Hostname("*.example.com")), |
| 115 | + routeHostnames: []gatewayv1.Hostname{"example.com", "foo.example.net"}, |
| 116 | + want: false, |
| 117 | + }, |
| 118 | + { |
| 119 | + name: "listener exact, route wildcard domain itself (no intersection)", |
| 120 | + listenerHostname: ptr.To(gatewayv1.Hostname("example.com")), |
| 121 | + routeHostnames: []gatewayv1.Hostname{"*.example.com", "foo.example.net"}, |
| 122 | + want: false, |
| 123 | + }, |
| 124 | + { |
| 125 | + name: "both nil/empty", |
| 126 | + listenerHostname: nil, |
| 127 | + routeHostnames: []gatewayv1.Hostname{}, |
| 128 | + want: true, |
| 129 | + }, |
| 130 | + } |
| 131 | + |
| 132 | + for _, tt := range tests { |
| 133 | + t.Run(tt.name, func(t *testing.T) { |
| 134 | + if got := hostnamesIntersect(tt.listenerHostname, tt.routeHostnames); got != tt.want { |
| 135 | + t.Errorf("hostnamesIntersect() = %v, want %v", got, tt.want) |
| 136 | + } |
| 137 | + }) |
| 138 | + } |
| 139 | +} |
0 commit comments