|
| 1 | +// Copyright 2020, OpenTelemetry Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package filters |
| 16 | + |
| 17 | +import ( |
| 18 | + "net/http" |
| 19 | + "net/url" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "go.opentelemetry.io/otel/plugin/othttp" |
| 23 | +) |
| 24 | + |
| 25 | +type scenario struct { |
| 26 | + name string |
| 27 | + filter othttp.Filter |
| 28 | + req *http.Request |
| 29 | + exp bool |
| 30 | +} |
| 31 | + |
| 32 | +func TestAny(t *testing.T) { |
| 33 | + for _, s := range []scenario{ |
| 34 | + { |
| 35 | + name: "no matching filters", |
| 36 | + filter: Any(Path("/foo"), Hostname("bar.baz")), |
| 37 | + req: &http.Request{URL: &url.URL{Path: "/boo", Host: "baz.bar:8080"}}, |
| 38 | + exp: false, |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "one matching filter", |
| 42 | + filter: Any(Path("/foo"), Hostname("bar.baz")), |
| 43 | + req: &http.Request{URL: &url.URL{Path: "/foo", Host: "baz.bar:8080"}}, |
| 44 | + exp: true, |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "all matching filters", |
| 48 | + filter: Any(Path("/foo"), Hostname("bar.baz")), |
| 49 | + req: &http.Request{URL: &url.URL{Path: "/foo", Host: "bar.baz:8080"}}, |
| 50 | + exp: true, |
| 51 | + }, |
| 52 | + } { |
| 53 | + res := s.filter(s.req) |
| 54 | + if s.exp != res { |
| 55 | + t.Errorf("Failed testing %q. Expected %t, got %t", s.name, s.exp, res) |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func TestAll(t *testing.T) { |
| 61 | + for _, s := range []scenario{ |
| 62 | + { |
| 63 | + name: "no matching filters", |
| 64 | + filter: All(Path("/foo"), Hostname("bar.baz")), |
| 65 | + req: &http.Request{URL: &url.URL{Path: "/boo", Host: "baz.bar:8080"}}, |
| 66 | + exp: false, |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "one matching filter", |
| 70 | + filter: All(Path("/foo"), Hostname("bar.baz")), |
| 71 | + req: &http.Request{URL: &url.URL{Path: "/foo", Host: "baz.bar:8080"}}, |
| 72 | + exp: false, |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "all matching filters", |
| 76 | + filter: All(Path("/foo"), Hostname("bar.baz")), |
| 77 | + req: &http.Request{URL: &url.URL{Path: "/foo", Host: "bar.baz:8080"}}, |
| 78 | + exp: true, |
| 79 | + }, |
| 80 | + } { |
| 81 | + res := s.filter(s.req) |
| 82 | + if s.exp != res { |
| 83 | + t.Errorf("Failed testing %q. Expected %t, got %t", s.name, s.exp, res) |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func TestNone(t *testing.T) { |
| 89 | + for _, s := range []scenario{ |
| 90 | + { |
| 91 | + name: "no matching filters", |
| 92 | + filter: None(Path("/foo"), Hostname("bar.baz")), |
| 93 | + req: &http.Request{URL: &url.URL{Path: "/boo", Host: "baz.bar:8080"}}, |
| 94 | + exp: true, |
| 95 | + }, |
| 96 | + { |
| 97 | + name: "one matching filter", |
| 98 | + filter: None(Path("/foo"), Hostname("bar.baz")), |
| 99 | + req: &http.Request{URL: &url.URL{Path: "/foo", Host: "baz.bar:8080"}}, |
| 100 | + exp: false, |
| 101 | + }, |
| 102 | + { |
| 103 | + name: "all matching filters", |
| 104 | + filter: None(Path("/foo"), Hostname("bar.baz")), |
| 105 | + req: &http.Request{URL: &url.URL{Path: "/foo", Host: "bar.baz:8080"}}, |
| 106 | + exp: false, |
| 107 | + }, |
| 108 | + } { |
| 109 | + res := s.filter(s.req) |
| 110 | + if s.exp != res { |
| 111 | + t.Errorf("Failed testing %q. Expected %t, got %t", s.name, s.exp, res) |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +func TestNot(t *testing.T) { |
| 117 | + req := &http.Request{URL: &url.URL{Path: "/foo", Host: "bar.baz:8080"}} |
| 118 | + filter := Path("/foo") |
| 119 | + if filter(req) == Not(filter)(req) { |
| 120 | + t.Error("Not filter should invert the result of the supplied filter") |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +func TestPathPrefix(t *testing.T) { |
| 125 | + for _, s := range []scenario{ |
| 126 | + { |
| 127 | + name: "non-matching prefix", |
| 128 | + filter: PathPrefix("/foo"), |
| 129 | + req: &http.Request{URL: &url.URL{Path: "/boo/far", Host: "baz.bar:8080"}}, |
| 130 | + exp: false, |
| 131 | + }, |
| 132 | + { |
| 133 | + name: "matching prefix", |
| 134 | + filter: PathPrefix("/foo"), |
| 135 | + req: &http.Request{URL: &url.URL{Path: "/foo/bar", Host: "bar.baz:8080"}}, |
| 136 | + exp: true, |
| 137 | + }, |
| 138 | + } { |
| 139 | + res := s.filter(s.req) |
| 140 | + if s.exp != res { |
| 141 | + t.Errorf("Failed testing %q. Expected %t, got %t", s.name, s.exp, res) |
| 142 | + } |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +func TestMethod(t *testing.T) { |
| 147 | + for _, s := range []scenario{ |
| 148 | + { |
| 149 | + name: "non-matching method", |
| 150 | + filter: Method(http.MethodGet), |
| 151 | + req: &http.Request{Method: http.MethodHead, URL: &url.URL{Path: "/boo/far", Host: "baz.bar:8080"}}, |
| 152 | + exp: false, |
| 153 | + }, |
| 154 | + { |
| 155 | + name: "matching method", |
| 156 | + filter: Method(http.MethodGet), |
| 157 | + req: &http.Request{Method: http.MethodGet, URL: &url.URL{Path: "/boo/far", Host: "baz.bar:8080"}}, |
| 158 | + exp: true, |
| 159 | + }, |
| 160 | + } { |
| 161 | + res := s.filter(s.req) |
| 162 | + if s.exp != res { |
| 163 | + t.Errorf("Failed testing %q. Expected %t, got %t", s.name, s.exp, res) |
| 164 | + } |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +func TestQuery(t *testing.T) { |
| 169 | + matching, _ := url.Parse("http://bar.baz:8080/foo/bar?key=value") |
| 170 | + nonMatching, _ := url.Parse("http://bar.baz:8080/foo/bar?key=other") |
| 171 | + for _, s := range []scenario{ |
| 172 | + { |
| 173 | + name: "non-matching query parameter", |
| 174 | + filter: Query("key", "value"), |
| 175 | + req: &http.Request{Method: http.MethodHead, URL: nonMatching}, |
| 176 | + exp: false, |
| 177 | + }, |
| 178 | + { |
| 179 | + name: "matching query parameter", |
| 180 | + filter: Query("key", "value"), |
| 181 | + req: &http.Request{Method: http.MethodGet, URL: matching}, |
| 182 | + exp: true, |
| 183 | + }, |
| 184 | + } { |
| 185 | + res := s.filter(s.req) |
| 186 | + if s.exp != res { |
| 187 | + t.Errorf("Failed testing %q. Expected %t, got %t", s.name, s.exp, res) |
| 188 | + } |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +func TestQueryContains(t *testing.T) { |
| 193 | + matching, _ := url.Parse("http://bar.baz:8080/foo/bar?key=value") |
| 194 | + nonMatching, _ := url.Parse("http://bar.baz:8080/foo/bar?key=other") |
| 195 | + for _, s := range []scenario{ |
| 196 | + { |
| 197 | + name: "non-matching query parameter", |
| 198 | + filter: QueryContains("key", "alu"), |
| 199 | + req: &http.Request{Method: http.MethodHead, URL: nonMatching}, |
| 200 | + exp: false, |
| 201 | + }, |
| 202 | + { |
| 203 | + name: "matching query parameter", |
| 204 | + filter: QueryContains("key", "alu"), |
| 205 | + req: &http.Request{Method: http.MethodGet, URL: matching}, |
| 206 | + exp: true, |
| 207 | + }, |
| 208 | + } { |
| 209 | + res := s.filter(s.req) |
| 210 | + if s.exp != res { |
| 211 | + t.Errorf("Failed testing %q. Expected %t, got %t", s.name, s.exp, res) |
| 212 | + } |
| 213 | + } |
| 214 | +} |
| 215 | + |
| 216 | +func TestHeader(t *testing.T) { |
| 217 | + matching := http.Header{} |
| 218 | + matching.Add("key", "value") |
| 219 | + nonMatching := http.Header{} |
| 220 | + nonMatching.Add("key", "other") |
| 221 | + for _, s := range []scenario{ |
| 222 | + { |
| 223 | + name: "non-matching query parameter", |
| 224 | + filter: Header("key", "value"), |
| 225 | + req: &http.Request{Method: http.MethodHead, Header: nonMatching}, |
| 226 | + exp: false, |
| 227 | + }, |
| 228 | + { |
| 229 | + name: "matching query parameter", |
| 230 | + filter: Header("key", "value"), |
| 231 | + req: &http.Request{Method: http.MethodGet, Header: matching}, |
| 232 | + exp: true, |
| 233 | + }, |
| 234 | + } { |
| 235 | + res := s.filter(s.req) |
| 236 | + if s.exp != res { |
| 237 | + t.Errorf("Failed testing %q. Expected %t, got %t", s.name, s.exp, res) |
| 238 | + } |
| 239 | + } |
| 240 | +} |
| 241 | + |
| 242 | +func TestHeaderContains(t *testing.T) { |
| 243 | + matching := http.Header{} |
| 244 | + matching.Add("key", "value") |
| 245 | + nonMatching := http.Header{} |
| 246 | + nonMatching.Add("key", "other") |
| 247 | + for _, s := range []scenario{ |
| 248 | + { |
| 249 | + name: "non-matching query parameter", |
| 250 | + filter: HeaderContains("key", "alu"), |
| 251 | + req: &http.Request{Method: http.MethodHead, Header: nonMatching}, |
| 252 | + exp: false, |
| 253 | + }, |
| 254 | + { |
| 255 | + name: "matching query parameter", |
| 256 | + filter: HeaderContains("key", "alu"), |
| 257 | + req: &http.Request{Method: http.MethodGet, Header: matching}, |
| 258 | + exp: true, |
| 259 | + }, |
| 260 | + } { |
| 261 | + res := s.filter(s.req) |
| 262 | + if s.exp != res { |
| 263 | + t.Errorf("Failed testing %q. Expected %t, got %t", s.name, s.exp, res) |
| 264 | + } |
| 265 | + } |
| 266 | +} |
0 commit comments