Skip to content

Commit

Permalink
Fix matching score bug for body matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
tommysitu committed Feb 28, 2024
1 parent b4b6e0d commit d4bca67
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 58 deletions.
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

0 comments on commit d4bca67

Please sign in to comment.