From f3547d09cf0748b3784d8d9149741ef5491c231e Mon Sep 17 00:00:00 2001 From: kapishmalik Date: Mon, 11 Mar 2024 18:53:10 +0530 Subject: [PATCH 1/2] add negation matcher to hoverfly --- core/matching/matchers/matchers.go | 4 +++ core/matching/matchers/negation_match.go | 11 +++++++ core/matching/matchers/negation_match_test.go | 32 +++++++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 core/matching/matchers/negation_match.go create mode 100644 core/matching/matchers/negation_match_test.go diff --git a/core/matching/matchers/matchers.go b/core/matching/matchers/matchers.go index 3adf5b1d1..6efba03fd 100644 --- a/core/matching/matchers/matchers.go +++ b/core/matching/matchers/matchers.go @@ -60,6 +60,10 @@ var Matchers = map[string]MatcherDetails{ MatcherFunction: JwtMatcher, MatchValueGenerator: JwtMatchValueGenerator, }, + Negation: { + MatcherFunction: NegationMatch, + MatchValueGenerator: IdentityValueGenerator, + }, } type MatcherDetails struct { diff --git a/core/matching/matchers/negation_match.go b/core/matching/matchers/negation_match.go new file mode 100644 index 000000000..41b22f2cb --- /dev/null +++ b/core/matching/matchers/negation_match.go @@ -0,0 +1,11 @@ +package matchers + +var Negation = "negation" + +func NegationMatch(match interface{}, toMatch string) bool { + matchString, ok := match.(string) + if ok { + return matchString != toMatch + } + return true +} diff --git a/core/matching/matchers/negation_match_test.go b/core/matching/matchers/negation_match_test.go new file mode 100644 index 000000000..9a488f7bf --- /dev/null +++ b/core/matching/matchers/negation_match_test.go @@ -0,0 +1,32 @@ +package matchers_test + +import ( + "testing" + + "github.com/SpectoLabs/hoverfly/core/matching/matchers" + . "github.com/onsi/gomega" +) + +func Test_NegationMatch_MatchesTrueWithIncorrectDataType(t *testing.T) { + RegisterTestingT(t) + + Expect(matchers.NegationMatch(1, "yes")).To(BeTrue()) +} + +func Test_NegationMatch_MatchesTrueWithNil(t *testing.T) { + RegisterTestingT(t) + + Expect(matchers.NegationMatch(nil, "yes")).To(BeTrue()) +} + +func Test_NegationMatch_MatchesFalseWithExactMatch(t *testing.T) { + RegisterTestingT(t) + + Expect(matchers.NegationMatch("yes", "yes")).To(BeFalse()) +} + +func Test_NegationMatch_MatchesTrueWithIncorrectExactMatch(t *testing.T) { + RegisterTestingT(t) + + Expect(matchers.NegationMatch("yes", "no")).To(BeTrue()) +} From df8d9f15a8b0700bfdb8905676eedc31307693dd Mon Sep 17 00:00:00 2001 From: kapishmalik Date: Tue, 12 Mar 2024 12:46:10 +0530 Subject: [PATCH 2/2] add integration tests --- core/matching/matchers/negation_match.go | 2 +- .../core/ft_simulation_matching_test.go | 26 +++++++ functional-tests/testdata/negation_match.go | 68 +++++++++++++++++++ 3 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 functional-tests/testdata/negation_match.go diff --git a/core/matching/matchers/negation_match.go b/core/matching/matchers/negation_match.go index 41b22f2cb..d983f3eef 100644 --- a/core/matching/matchers/negation_match.go +++ b/core/matching/matchers/negation_match.go @@ -1,6 +1,6 @@ package matchers -var Negation = "negation" +var Negation = "negate" func NegationMatch(match interface{}, toMatch string) bool { matchString, ok := match.(string) diff --git a/functional-tests/core/ft_simulation_matching_test.go b/functional-tests/core/ft_simulation_matching_test.go index 9116a333e..3f3a2d61a 100644 --- a/functional-tests/core/ft_simulation_matching_test.go +++ b/functional-tests/core/ft_simulation_matching_test.go @@ -438,4 +438,30 @@ var _ = Describe(" When using different matchers", func() { }) }) + + Context("Using negate match", func() { + + BeforeEach(func() { + hoverfly.ImportSimulation(testdata.NegationMatch) + }) + + It("path should not match with different path passed", func() { + req := sling.New().Get("http://test.com/path2") + + response := hoverfly.Proxy(req) + Expect(response.StatusCode).To(Equal(200)) + + Expect(io.ReadAll(response.Body)).Should(Equal([]byte("destination matched"))) + }) + + It("path should not match with no path passed", func() { + req := sling.New().Get("http://test.com") + + response := hoverfly.Proxy(req) + Expect(response.StatusCode).To(Equal(200)) + + Expect(io.ReadAll(response.Body)).Should(Equal([]byte("destination matched"))) + }) + + }) }) diff --git a/functional-tests/testdata/negation_match.go b/functional-tests/testdata/negation_match.go new file mode 100644 index 000000000..4ad4d8a5b --- /dev/null +++ b/functional-tests/testdata/negation_match.go @@ -0,0 +1,68 @@ +package testdata + +var NegationMatch = `{ + "data": { + "pairs": [ + { + "request": { + "path": [ + { + "matcher": "negate", + "value": "/path1" + } + ], + "method": [ + { + "matcher": "exact", + "value": "GET" + } + ], + "destination": [ + { + "matcher": "exact", + "value": "test.com" + } + ], + "scheme": [ + { + "matcher": "exact", + "value": "http" + } + ], + "deprecatedQuery": [ + { + "matcher": "exact", + "value": "" + } + ], + "body": [ + { + "matcher": "exact", + "value": "" + } + ] + }, + "response": { + "status": 200, + "body": "destination matched", + "encodedBody": false, + "headers": { + "Header": [ + "value1" + ] + }, + "templated": false + } + } + ], + "globalActions": { + "delays": [], + "delaysLogNormal": [] + } + }, + "meta": { + "schemaVersion": "v5", + "hoverflyVersion": "v0.17.0", + "timeExported": "2018-05-03T12:12:46+01:00" + } +}`