| // |
| 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 v1 |
| 16 |
| 17 | import ( |
| 18 | "testing" |
| 19 |
| 20 | "github.com/stretchr/testify/require" |
| 21 | ) |
| 22 |
| 23 | func TestDecodeProxyConfigurerJSON_StrictPluginUnknownFields(t *testing.T) { |
| 24 | require := require.New(t) |
| 25 |
| 26 | data := []byte(`{ |
| 27 | "name":"p1", |
| 28 | "type":"tcp", |
| 29 | "localPort":10080, |
| 30 | "plugin":{ |
| 31 | "type":"http2https", |
| 32 | "localAddr":"127.0.0.1:8080", |
| 33 | "unknownInPlugin":"value" |
| 34 | } |
| 35 | }`) |
| 36 |
| 37 | _, err := DecodeProxyConfigurerJSON(data, DecodeOptions{DisallowUnknownFields: false}) |
| 38 | require.NoError(err) |
| 39 |
| 40 | _, err = DecodeProxyConfigurerJSON(data, DecodeOptions{DisallowUnknownFields: true}) |
| 41 | require.ErrorContains(err, "unknownInPlugin") |
| 42 | } |
| 43 |
| 44 | func TestDecodeVisitorConfigurerJSON_StrictPluginUnknownFields(t *testing.T) { |
| 45 | require := require.New(t) |
| 46 |
| 47 | data := []byte(`{ |
| 48 | "name":"v1", |
| 49 | "type":"stcp", |
| 50 | "serverName":"server", |
| 51 | "bindPort":10081, |
| 52 | "plugin":{ |
| 53 | "type":"virtual_net", |
| 54 | "destinationIP":"10.0.0.1", |
| 55 | "unknownInPlugin":"value" |
| 56 | } |
| 57 | }`) |
| 58 |
| 59 | _, err := DecodeVisitorConfigurerJSON(data, DecodeOptions{DisallowUnknownFields: false}) |
| 60 | require.NoError(err) |
| 61 |
| 62 | _, err = DecodeVisitorConfigurerJSON(data, DecodeOptions{DisallowUnknownFields: true}) |
| 63 | require.ErrorContains(err, "unknownInPlugin") |
| 64 | } |
| 65 |
| 66 | func TestDecodeClientConfigJSON_StrictUnknownProxyField(t *testing.T) { |
| 67 | require := require.New(t) |
| 68 |
| 69 | data := []byte(`{ |
| 70 | "serverPort":7000, |
| 71 | "proxies":[ |
| 72 | { |
| 73 | "name":"p1", |
| 74 | "type":"tcp", |
| 75 | "localPort":10080, |
| 76 | "unknownField":"value" |
| 77 | } |
| 78 | ] |
| 79 | }`) |
| 80 |
| 81 | _, err := DecodeClientConfigJSON(data, DecodeOptions{DisallowUnknownFields: false}) |
| 82 | require.NoError(err) |
| 83 |
| 84 | _, err = DecodeClientConfigJSON(data, DecodeOptions{DisallowUnknownFields: true}) |
| 85 | require.ErrorContains(err, "unknownField") |
| 86 | } |