Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add negation matcher to hoverfly #1117

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions core/matching/matchers/matchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ var Matchers = map[string]MatcherDetails{
MatcherFunction: JwtMatcher,
MatchValueGenerator: JwtMatchValueGenerator,
},
Negation: {
MatcherFunction: NegationMatch,
MatchValueGenerator: IdentityValueGenerator,
},
}

type MatcherDetails struct {
Expand Down
11 changes: 11 additions & 0 deletions core/matching/matchers/negation_match.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package matchers

var Negation = "negate"

func NegationMatch(match interface{}, toMatch string) bool {
matchString, ok := match.(string)
if ok {
return matchString != toMatch
}
return true
}
32 changes: 32 additions & 0 deletions core/matching/matchers/negation_match_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
26 changes: 26 additions & 0 deletions functional-tests/core/ft_simulation_matching_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")))
})

})
})
68 changes: 68 additions & 0 deletions functional-tests/testdata/negation_match.go
Original file line number Diff line number Diff line change
@@ -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"
}
}`
Loading