|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package meshtests |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "sigs.k8s.io/gateway-api/conformance/utils/echo" |
| 23 | + "sigs.k8s.io/gateway-api/conformance/utils/http" |
| 24 | + "sigs.k8s.io/gateway-api/conformance/utils/suite" |
| 25 | + "sigs.k8s.io/gateway-api/pkg/features" |
| 26 | +) |
| 27 | + |
| 28 | +func init() { |
| 29 | + MeshConformanceTests = append(MeshConformanceTests, MeshHTTPRouteQueryParamMatching) |
| 30 | +} |
| 31 | + |
| 32 | +var MeshHTTPRouteQueryParamMatching = suite.ConformanceTest{ |
| 33 | + ShortName: "MeshHTTPRouteQueryParamMatching", |
| 34 | + Description: "A single HTTPRoute with query param matching for different backends", |
| 35 | + Manifests: []string{"tests/mesh/httproute-query-param-matching.yaml"}, |
| 36 | + Features: []features.FeatureName{ |
| 37 | + features.SupportMesh, |
| 38 | + features.SupportHTTPRoute, |
| 39 | + features.SupportMeshHTTPRouteQueryParamMatching, |
| 40 | + }, |
| 41 | + Test: func(t *testing.T, s *suite.ConformanceTestSuite) { |
| 42 | + ns := "gateway-conformance-mesh" |
| 43 | + client := echo.ConnectToApp(t, s, echo.MeshAppEchoV1) |
| 44 | + |
| 45 | + testCases := []http.ExpectedResponse{{ |
| 46 | + Request: http.Request{Path: "/?animal=whale"}, |
| 47 | + Backend: "echo-v1", |
| 48 | + Namespace: ns, |
| 49 | + }, { |
| 50 | + Request: http.Request{Path: "/?animal=dolphin"}, |
| 51 | + Backend: "echo-v2", |
| 52 | + Namespace: ns, |
| 53 | + }, { |
| 54 | + Request: http.Request{Path: "/?animal=whale&otherparam=irrelevant"}, |
| 55 | + Backend: "echo-v1", |
| 56 | + Namespace: ns, |
| 57 | + }, { |
| 58 | + Request: http.Request{Path: "/?animal=dolphin&color=yellow"}, |
| 59 | + Backend: "echo-v2", |
| 60 | + Namespace: ns, |
| 61 | + }, { |
| 62 | + Request: http.Request{Path: "/?color=blue"}, |
| 63 | + Response: http.Response{StatusCode: 404}, |
| 64 | + }, { |
| 65 | + Request: http.Request{Path: "/?animal=dog"}, |
| 66 | + Response: http.Response{StatusCode: 404}, |
| 67 | + }, { |
| 68 | + Request: http.Request{Path: "/?animal=whaledolphin"}, |
| 69 | + Response: http.Response{StatusCode: 404}, |
| 70 | + }, { |
| 71 | + Request: http.Request{Path: "/"}, |
| 72 | + Response: http.Response{StatusCode: 404}, |
| 73 | + }} |
| 74 | + |
| 75 | + // Combinations of query param matching with other core matches. |
| 76 | + testCases = append(testCases, []http.ExpectedResponse{ |
| 77 | + { |
| 78 | + Request: http.Request{Path: "/path1?animal=whale"}, |
| 79 | + Backend: "echo-v1", |
| 80 | + Namespace: ns, |
| 81 | + }, |
| 82 | + { |
| 83 | + Request: http.Request{Headers: map[string]string{"version": "one"}, Path: "/?animal=whale"}, |
| 84 | + Backend: "echo-v2", |
| 85 | + Namespace: ns, |
| 86 | + }, |
| 87 | + }...) |
| 88 | + |
| 89 | + // Ensure that combinations of matches which are OR'd together match |
| 90 | + // even if only one of them is used in the request. |
| 91 | + testCases = append(testCases, []http.ExpectedResponse{ |
| 92 | + { |
| 93 | + Request: http.Request{Path: "/path3?animal=shark"}, |
| 94 | + Backend: "echo-v1", |
| 95 | + Namespace: ns, |
| 96 | + }, |
| 97 | + { |
| 98 | + Request: http.Request{Headers: map[string]string{"version": "three"}, Path: "/path4?animal=kraken"}, |
| 99 | + Backend: "echo-v1", |
| 100 | + Namespace: ns, |
| 101 | + }, |
| 102 | + }...) |
| 103 | + |
| 104 | + // Ensure that combinations of match types which are ANDed together do not match |
| 105 | + // when only a subset of match types is used in the request. |
| 106 | + testCases = append(testCases, []http.ExpectedResponse{ |
| 107 | + { |
| 108 | + Request: http.Request{Path: "/?animal=shark"}, |
| 109 | + Response: http.Response{StatusCode: 404}, |
| 110 | + }, |
| 111 | + { |
| 112 | + Request: http.Request{Path: "/path4?animal=kraken"}, |
| 113 | + Response: http.Response{StatusCode: 404}, |
| 114 | + }, |
| 115 | + }...) |
| 116 | + |
| 117 | + // For requests that satisfy multiple matches, ensure precedence order |
| 118 | + // defined by the Gateway API spec is maintained. |
| 119 | + testCases = append(testCases, []http.ExpectedResponse{ |
| 120 | + { |
| 121 | + Request: http.Request{Path: "/path5?animal=hydra"}, |
| 122 | + Backend: "echo-v1", |
| 123 | + Namespace: ns, |
| 124 | + }, |
| 125 | + }...) |
| 126 | + |
| 127 | + for i := range testCases { |
| 128 | + tc := testCases[i] |
| 129 | + t.Run(tc.GetTestCaseName(i), func(t *testing.T) { |
| 130 | + t.Parallel() |
| 131 | + client.MakeRequestAndExpectEventuallyConsistentResponse(t, tc, s.TimeoutConfig) |
| 132 | + }) |
| 133 | + } |
| 134 | + }, |
| 135 | +} |
0 commit comments