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

Fix matching score bug for body matcher #1116

Merged
merged 1 commit into from
Feb 28, 2024
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
5 changes: 1 addition & 4 deletions core/matching/body_formdata_matching.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ func BodyMatching(fields []models.RequestFieldMatchers, req models.RequestDetail
var score int

if len(fields) == 0 {
return &FieldMatch{
Matched: matched,
Score: 1,
}
return &FieldMatch{Matched: true}
}

for _, field := range fields {
Expand Down
100 changes: 72 additions & 28 deletions core/matching/first_match_strategy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,16 @@ import (
. "github.com/onsi/gomega"
)

var testResponse = models.ResponseDetails{
Body: "request matched",
}

func Test_FirstMatchStrategy_EmptyRequestMatchersShouldMatchOnAnyRequest(t *testing.T) {
RegisterTestingT(t)

simulation := models.NewSimulation()

simulation.AddPair(&models.RequestMatcherResponsePair{
RequestMatcher: models.RequestMatcher{},
Response: testResponse,
Response: models.ResponseDetails{
Body: "request matched",
},
})

r := models.RequestDetails{
Expand Down Expand Up @@ -50,7 +48,9 @@ func Test_FirstMatchStrategy_RequestMatchersShouldMatchOnBody(t *testing.T) {
},
},
},
Response: testResponse,
Response: models.ResponseDetails{
Body: "request matched",
},
})

r := models.RequestDetails{
Expand Down Expand Up @@ -86,7 +86,9 @@ func Test_FirstMatchStrategy_ReturnResponseWhenAllHeadersMatch(t *testing.T) {
RequestMatcher: models.RequestMatcher{
Headers: headers,
},
Response: testResponse,
Response: models.ResponseDetails{
Body: "request matched",
},
})

r := models.RequestDetails{
Expand Down Expand Up @@ -127,7 +129,9 @@ func Test_FirstMatchStrategy_ReturnNilWhenOneHeaderNotPresentInRequest(t *testin
RequestMatcher: models.RequestMatcher{
Headers: headers,
},
Response: testResponse,
Response: models.ResponseDetails{
Body: "request matched",
},
})

r := models.RequestDetails{
Expand Down Expand Up @@ -167,7 +171,9 @@ func Test_FirstMatchStrategy_ReturnNilWhenOneHeaderValueDifferent(t *testing.T)
RequestMatcher: models.RequestMatcher{
Headers: headers,
},
Response: testResponse,
Response: models.ResponseDetails{
Body: "request matched",
},
})

r := models.RequestDetails{
Expand Down Expand Up @@ -207,7 +213,9 @@ func Test_FirstMatchStrategy_ReturnResponseWithMultiValuedHeaderMatch(t *testing
RequestMatcher: models.RequestMatcher{
Headers: headers,
},
Response: testResponse,
Response: models.ResponseDetails{
Body: "request matched",
},
})

r := models.RequestDetails{
Expand Down Expand Up @@ -248,7 +256,9 @@ func Test_FirstMatchStrategy_ReturnNilWithDifferentMultiValuedHeaders(t *testing
RequestMatcher: models.RequestMatcher{
Headers: headers,
},
Response: testResponse,
Response: models.ResponseDetails{
Body: "request matched",
},
})

r := models.RequestDetails{
Expand Down Expand Up @@ -307,7 +317,9 @@ func Test_FirstMatchStrategy_EndpointMatchWithHeaders(t *testing.T) {
},
},
},
Response: testResponse,
Response: models.ResponseDetails{
Body: "request matched",
},
})

r := models.RequestDetails{
Expand Down Expand Up @@ -369,7 +381,9 @@ func Test_FirstMatchStrategy_EndpointMismatchWithHeadersReturnsNil(t *testing.T)
},
},
},
Response: testResponse,
Response: models.ResponseDetails{
Body: "request matched",
},
})

r := models.RequestDetails{
Expand Down Expand Up @@ -416,7 +430,9 @@ func Test_FirstMatchStrategy_AbleToMatchAnEmptyPathInAReasonableWay(t *testing.T
},
},
},
Response: testResponse,
Response: models.ResponseDetails{
Body: "request matched",
},
})

r := models.RequestDetails{
Expand Down Expand Up @@ -456,7 +472,9 @@ func Test_FirstMatchStrategy_RequestMatcherResponsePairCanBeConvertedToARequestR
},
},
},
Response: testResponse,
Response: models.ResponseDetails{
Body: "request matched",
},
}

pairView := requestMatcherResponsePair.BuildView()
Expand Down Expand Up @@ -487,7 +505,9 @@ func Test_FirstMatchStrategy_RequestMatchersCanUseGlobsAndBeMatched(t *testing.T
},
},
},
Response: testResponse,
Response: models.ResponseDetails{
Body: "request matched",
},
})

request := models.RequestDetails{
Expand Down Expand Up @@ -516,7 +536,9 @@ func Test_FirstMatchStrategy_RequestMatchersCanUseGlobsOnSchemeAndBeMatched(t *t
},
},
},
Response: testResponse,
Response: models.ResponseDetails{
Body: "request matched",
},
})

request := models.RequestDetails{
Expand Down Expand Up @@ -548,7 +570,9 @@ func Test_FirstMatchStrategy_RequestMatchersCanUseGlobsOnHeadersAndBeMatched(t *
},
},
},
Response: testResponse,
Response: models.ResponseDetails{
Body: "request matched",
},
})

request := models.RequestDetails{
Expand Down Expand Up @@ -578,7 +602,9 @@ func Test_FirstMatchStrategy_RequestMatcherResponsePair_ConvertToRequestResponse
},
},
},
Response: testResponse,
Response: models.ResponseDetails{
Body: "request matched",
},
}

pairView := requestMatcherResponsePair.BuildView()
Expand Down Expand Up @@ -640,7 +666,9 @@ func Test_FirstMatchShouldNotBeCacheableIfMatchedOnEverythingApartFromHeadersAtL
},
},
},
Response: testResponse,
Response: models.ResponseDetails{
Body: "request matched",
},
})

simulation.AddPair(&models.RequestMatcherResponsePair{
Expand All @@ -652,7 +680,9 @@ func Test_FirstMatchShouldNotBeCacheableIfMatchedOnEverythingApartFromHeadersAtL
},
},
},
Response: testResponse,
Response: models.ResponseDetails{
Body: "request matched",
},
})

r := models.RequestDetails{
Expand Down Expand Up @@ -729,7 +759,9 @@ func Test_FirstMatchShouldBeCacheableIfMatchedOnEverythingApartFromHeadersZeroTi
},
},
},
Response: testResponse,
Response: models.ResponseDetails{
Body: "request matched",
},
})

simulation.AddPair(&models.RequestMatcherResponsePair{
Expand All @@ -741,7 +773,9 @@ func Test_FirstMatchShouldBeCacheableIfMatchedOnEverythingApartFromHeadersZeroTi
},
},
},
Response: testResponse,
Response: models.ResponseDetails{
Body: "request matched",
},
})

r := models.RequestDetails{
Expand Down Expand Up @@ -849,7 +883,9 @@ func Test_FirstMatchStrategy_RequestMatchersShouldMatchOnStateAndNotBeCacheable(
RequestMatcher: models.RequestMatcher{
RequiresState: map[string]string{"key1": "value1", "key2": "value2"},
},
Response: testResponse,
Response: models.ResponseDetails{
Body: "request matched",
},
})

r := models.RequestDetails{
Expand Down Expand Up @@ -909,7 +945,9 @@ func Test_FirstMatchShouldNotBeCacheableIfMatchedOnEverythingApartFromStateAtLea
"foo": "bar",
},
},
Response: testResponse,
Response: models.ResponseDetails{
Body: "request matched",
},
})

simulation.AddPair(&models.RequestMatcherResponsePair{
Expand All @@ -921,7 +959,9 @@ func Test_FirstMatchShouldNotBeCacheableIfMatchedOnEverythingApartFromStateAtLea
},
},
},
Response: testResponse,
Response: models.ResponseDetails{
Body: "request matched",
},
})

r := models.RequestDetails{
Expand Down Expand Up @@ -990,7 +1030,9 @@ func Test_FirstMatchShouldBeCacheableIfMatchedOnEverythingApartFromStateZeroTime
"foo": "bar",
},
},
Response: testResponse,
Response: models.ResponseDetails{
Body: "request matched",
},
})

simulation.AddPair(&models.RequestMatcherResponsePair{
Expand All @@ -1002,7 +1044,9 @@ func Test_FirstMatchShouldBeCacheableIfMatchedOnEverythingApartFromStateZeroTime
},
},
},
Response: testResponse,
Response: models.ResponseDetails{
Body: "request matched",
},
})

r := models.RequestDetails{
Expand Down
Loading
Loading