diff --git a/core/matching/body_formdata_matching.go b/core/matching/body_formdata_matching.go index d718d55cf..68a49bc30 100644 --- a/core/matching/body_formdata_matching.go +++ b/core/matching/body_formdata_matching.go @@ -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 { diff --git a/core/matching/first_match_strategy_test.go b/core/matching/first_match_strategy_test.go index 8c228b380..0b9adde75 100644 --- a/core/matching/first_match_strategy_test.go +++ b/core/matching/first_match_strategy_test.go @@ -10,10 +10,6 @@ import ( . "github.com/onsi/gomega" ) -var testResponse = models.ResponseDetails{ - Body: "request matched", -} - func Test_FirstMatchStrategy_EmptyRequestMatchersShouldMatchOnAnyRequest(t *testing.T) { RegisterTestingT(t) @@ -21,7 +17,9 @@ func Test_FirstMatchStrategy_EmptyRequestMatchersShouldMatchOnAnyRequest(t *test simulation.AddPair(&models.RequestMatcherResponsePair{ RequestMatcher: models.RequestMatcher{}, - Response: testResponse, + Response: models.ResponseDetails{ + Body: "request matched", + }, }) r := models.RequestDetails{ @@ -50,7 +48,9 @@ func Test_FirstMatchStrategy_RequestMatchersShouldMatchOnBody(t *testing.T) { }, }, }, - Response: testResponse, + Response: models.ResponseDetails{ + Body: "request matched", + }, }) r := models.RequestDetails{ @@ -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{ @@ -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{ @@ -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{ @@ -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{ @@ -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{ @@ -307,7 +317,9 @@ func Test_FirstMatchStrategy_EndpointMatchWithHeaders(t *testing.T) { }, }, }, - Response: testResponse, + Response: models.ResponseDetails{ + Body: "request matched", + }, }) r := models.RequestDetails{ @@ -369,7 +381,9 @@ func Test_FirstMatchStrategy_EndpointMismatchWithHeadersReturnsNil(t *testing.T) }, }, }, - Response: testResponse, + Response: models.ResponseDetails{ + Body: "request matched", + }, }) r := models.RequestDetails{ @@ -416,7 +430,9 @@ func Test_FirstMatchStrategy_AbleToMatchAnEmptyPathInAReasonableWay(t *testing.T }, }, }, - Response: testResponse, + Response: models.ResponseDetails{ + Body: "request matched", + }, }) r := models.RequestDetails{ @@ -456,7 +472,9 @@ func Test_FirstMatchStrategy_RequestMatcherResponsePairCanBeConvertedToARequestR }, }, }, - Response: testResponse, + Response: models.ResponseDetails{ + Body: "request matched", + }, } pairView := requestMatcherResponsePair.BuildView() @@ -487,7 +505,9 @@ func Test_FirstMatchStrategy_RequestMatchersCanUseGlobsAndBeMatched(t *testing.T }, }, }, - Response: testResponse, + Response: models.ResponseDetails{ + Body: "request matched", + }, }) request := models.RequestDetails{ @@ -516,7 +536,9 @@ func Test_FirstMatchStrategy_RequestMatchersCanUseGlobsOnSchemeAndBeMatched(t *t }, }, }, - Response: testResponse, + Response: models.ResponseDetails{ + Body: "request matched", + }, }) request := models.RequestDetails{ @@ -548,7 +570,9 @@ func Test_FirstMatchStrategy_RequestMatchersCanUseGlobsOnHeadersAndBeMatched(t * }, }, }, - Response: testResponse, + Response: models.ResponseDetails{ + Body: "request matched", + }, }) request := models.RequestDetails{ @@ -578,7 +602,9 @@ func Test_FirstMatchStrategy_RequestMatcherResponsePair_ConvertToRequestResponse }, }, }, - Response: testResponse, + Response: models.ResponseDetails{ + Body: "request matched", + }, } pairView := requestMatcherResponsePair.BuildView() @@ -640,7 +666,9 @@ func Test_FirstMatchShouldNotBeCacheableIfMatchedOnEverythingApartFromHeadersAtL }, }, }, - Response: testResponse, + Response: models.ResponseDetails{ + Body: "request matched", + }, }) simulation.AddPair(&models.RequestMatcherResponsePair{ @@ -652,7 +680,9 @@ func Test_FirstMatchShouldNotBeCacheableIfMatchedOnEverythingApartFromHeadersAtL }, }, }, - Response: testResponse, + Response: models.ResponseDetails{ + Body: "request matched", + }, }) r := models.RequestDetails{ @@ -729,7 +759,9 @@ func Test_FirstMatchShouldBeCacheableIfMatchedOnEverythingApartFromHeadersZeroTi }, }, }, - Response: testResponse, + Response: models.ResponseDetails{ + Body: "request matched", + }, }) simulation.AddPair(&models.RequestMatcherResponsePair{ @@ -741,7 +773,9 @@ func Test_FirstMatchShouldBeCacheableIfMatchedOnEverythingApartFromHeadersZeroTi }, }, }, - Response: testResponse, + Response: models.ResponseDetails{ + Body: "request matched", + }, }) r := models.RequestDetails{ @@ -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{ @@ -909,7 +945,9 @@ func Test_FirstMatchShouldNotBeCacheableIfMatchedOnEverythingApartFromStateAtLea "foo": "bar", }, }, - Response: testResponse, + Response: models.ResponseDetails{ + Body: "request matched", + }, }) simulation.AddPair(&models.RequestMatcherResponsePair{ @@ -921,7 +959,9 @@ func Test_FirstMatchShouldNotBeCacheableIfMatchedOnEverythingApartFromStateAtLea }, }, }, - Response: testResponse, + Response: models.ResponseDetails{ + Body: "request matched", + }, }) r := models.RequestDetails{ @@ -990,7 +1030,9 @@ func Test_FirstMatchShouldBeCacheableIfMatchedOnEverythingApartFromStateZeroTime "foo": "bar", }, }, - Response: testResponse, + Response: models.ResponseDetails{ + Body: "request matched", + }, }) simulation.AddPair(&models.RequestMatcherResponsePair{ @@ -1002,7 +1044,9 @@ func Test_FirstMatchShouldBeCacheableIfMatchedOnEverythingApartFromStateZeroTime }, }, }, - Response: testResponse, + Response: models.ResponseDetails{ + Body: "request matched", + }, }) r := models.RequestDetails{ diff --git a/core/matching/strongest_match_strategy_test.go b/core/matching/strongest_match_strategy_test.go index 46da201d5..f69cf36bc 100644 --- a/core/matching/strongest_match_strategy_test.go +++ b/core/matching/strongest_match_strategy_test.go @@ -18,7 +18,9 @@ func Test_ClosestRequestMatcherRequestMatcher_EmptyRequestMatchersShouldMatchOnA simulation.AddPair(&models.RequestMatcherResponsePair{ RequestMatcher: models.RequestMatcher{}, - Response: testResponse, + Response: models.ResponseDetails{ + Body: "request matched", + }, }) r := models.RequestDetails{ @@ -48,7 +50,9 @@ func Test_ClosestRequestMatcherRequestMatcher_RequestMatchersShouldMatchOnBody(t }, }, }, - Response: testResponse, + Response: models.ResponseDetails{ + Body: "request matched", + }, }) r := models.RequestDetails{ @@ -60,6 +64,42 @@ func Test_ClosestRequestMatcherRequestMatcher_RequestMatchersShouldMatchOnBody(t Expect(result.Pair.Response.Body).To(Equal("request matched")) } +func Test_StrongestMatch_RequestMatchersShouldMatchOnBodyPrioritizingNotNilMatcher(t *testing.T) { + RegisterTestingT(t) + + simulation := models.NewSimulation() + + simulation.AddPair(&models.RequestMatcherResponsePair{ + RequestMatcher: models.RequestMatcher{ + Body: []models.RequestFieldMatchers{ + { + Matcher: matchers.JsonPartial, + Value: `{"bot": true}`, + }, + }, + }, + Response: models.ResponseDetails{ + Body: "json partial body matched", + }, + }) + + // if both matchers have the same score, this one will be picked because it's the most recent one. + simulation.AddPair(&models.RequestMatcherResponsePair{ + RequestMatcher: models.RequestMatcher{}, + Response: models.ResponseDetails{ + Body: "absent body matched", + }, + }) + + r := models.RequestDetails{ + Body: `{"bot": true, "name": "chatty"}`, + } + result := matching.MatchingStrategyRunner(r, false, simulation, &state.State{State: map[string]string{}}, &matching.StrongestMatchStrategy{}) + Expect(result.Error).To(BeNil()) + + Expect(result.Pair.Response.Body).To(Equal("json partial body matched")) +} + func Test_ClosestRequestMatcherRequestMatcher_ReturnResponseWhenAllHeadersMatch(t *testing.T) { RegisterTestingT(t) @@ -84,7 +124,9 @@ func Test_ClosestRequestMatcherRequestMatcher_ReturnResponseWhenAllHeadersMatch( RequestMatcher: models.RequestMatcher{ Headers: headers, }, - Response: testResponse, + Response: models.ResponseDetails{ + Body: "request matched", + }, }) r := models.RequestDetails{ @@ -125,7 +167,9 @@ func Test_ClosestRequestMatcherRequestMatcher_ReturnNilWhenOneHeaderNotPresentIn RequestMatcher: models.RequestMatcher{ Headers: headers, }, - Response: testResponse, + Response: models.ResponseDetails{ + Body: "request matched", + }, }) r := models.RequestDetails{ @@ -165,7 +209,9 @@ func Test_ClosestRequestMatcherRequestMatcher_ReturnNilWhenOneHeaderValueDiffere RequestMatcher: models.RequestMatcher{ Headers: headers, }, - Response: testResponse, + Response: models.ResponseDetails{ + Body: "request matched", + }, }) r := models.RequestDetails{ @@ -205,7 +251,9 @@ func Test_ClosestRequestMatcherRequestMatcher_ReturnResponseWithMultiValuedHeade RequestMatcher: models.RequestMatcher{ Headers: headers, }, - Response: testResponse, + Response: models.ResponseDetails{ + Body: "request matched", + }, }) r := models.RequestDetails{ @@ -245,7 +293,9 @@ func Test_ClosestRequestMatcherRequestMatcher_ReturnNilWithDifferentMultiValuedH RequestMatcher: models.RequestMatcher{ Headers: headers, }, - Response: testResponse, + Response: models.ResponseDetails{ + Body: "request matched", + }, }) r := models.RequestDetails{ @@ -304,7 +354,9 @@ func Test_ClosestRequestMatcherRequestMatcher_EndpointMatchWithHeaders(t *testin }, }, }, - Response: testResponse, + Response: models.ResponseDetails{ + Body: "request matched", + }, }) r := models.RequestDetails{ @@ -366,7 +418,9 @@ func Test_ClosestRequestMatcherRequestMatcher_EndpointMismatchWithHeadersReturns }, }, }, - Response: testResponse, + Response: models.ResponseDetails{ + Body: "request matched", + }, }) r := models.RequestDetails{ @@ -413,7 +467,9 @@ func Test_ClosestRequestMatcherRequestMatcher_AbleToMatchAnEmptyPathInAReasonabl }, }, }, - Response: testResponse, + Response: models.ResponseDetails{ + Body: "request matched", + }, }) r := models.RequestDetails{ @@ -455,7 +511,9 @@ func Test_ClosestRequestMatcherRequestMatcher_RequestMatchersCanUseGlobsAndBeMat }, }, }, - Response: testResponse, + Response: models.ResponseDetails{ + Body: "request matched", + }, }) request := models.RequestDetails{ @@ -484,7 +542,9 @@ func Test_ClosestRequestMatcherRequestMatcher_RequestMatchersCanUseGlobsOnScheme }, }, }, - Response: testResponse, + Response: models.ResponseDetails{ + Body: "request matched", + }, }) request := models.RequestDetails{ @@ -516,7 +576,9 @@ func Test_ClosestRequestMatcherRequestMatcher_RequestMatchersCanUseGlobsOnHeader }, }, }, - Response: testResponse, + Response: models.ResponseDetails{ + Body: "request matched", + }, }) request := models.RequestDetails{ @@ -810,7 +872,9 @@ func Test__NotBeCacheableIfMatchedOnEverythingApartFromHeadersAtLeastOnce(t *tes }, }, }, - Response: testResponse, + Response: models.ResponseDetails{ + Body: "request matched", + }, }) simulation.AddPair(&models.RequestMatcherResponsePair{ @@ -822,7 +886,9 @@ func Test__NotBeCacheableIfMatchedOnEverythingApartFromHeadersAtLeastOnce(t *tes }, }, }, - Response: testResponse, + Response: models.ResponseDetails{ + Body: "request matched", + }, }) r := models.RequestDetails{ @@ -899,7 +965,9 @@ func Test__ShouldBeCacheableIfMatchedOnEverythingApartFromHeadersZeroTimes(t *te }, }, }, - Response: testResponse, + Response: models.ResponseDetails{ + Body: "request matched", + }, }) simulation.AddPair(&models.RequestMatcherResponsePair{ @@ -911,7 +979,9 @@ func Test__ShouldBeCacheableIfMatchedOnEverythingApartFromHeadersZeroTimes(t *te }, }, }, - Response: testResponse, + Response: models.ResponseDetails{ + Body: "request matched", + }, }) r := models.RequestDetails{ @@ -1594,7 +1664,9 @@ func Test_StrongestMatch_ShouldNotBeCacheableIfMatchedOnEverythingApartFromHeade }, }, }, - Response: testResponse, + Response: models.ResponseDetails{ + Body: "request matched", + }, }) simulation.AddPair(&models.RequestMatcherResponsePair{ @@ -1606,7 +1678,9 @@ func Test_StrongestMatch_ShouldNotBeCacheableIfMatchedOnEverythingApartFromHeade }, }, }, - Response: testResponse, + Response: models.ResponseDetails{ + Body: "request matched", + }, }) r := models.RequestDetails{ @@ -1683,7 +1757,9 @@ func Test_StrongestMatch__ShouldBeCacheableIfMatchedOnEverythingApartFromHeaders }, }, }, - Response: testResponse, + Response: models.ResponseDetails{ + Body: "request matched", + }, }) simulation.AddPair(&models.RequestMatcherResponsePair{ @@ -1695,7 +1771,9 @@ func Test_StrongestMatch__ShouldBeCacheableIfMatchedOnEverythingApartFromHeaders }, }, }, - Response: testResponse, + Response: models.ResponseDetails{ + Body: "request matched", + }, }) r := models.RequestDetails{ @@ -1803,7 +1881,9 @@ func Test_MatchingStrategyRunner_RequestMatchersShouldMatchOnStateAndNotBeCachea RequestMatcher: models.RequestMatcher{ RequiresState: map[string]string{"key1": "value1", "key2": "value2"}, }, - Response: testResponse, + Response: models.ResponseDetails{ + Body: "request matched", + }, }) r := models.RequestDetails{ @@ -1863,7 +1943,9 @@ func Test_StrongestMatch_ShouldNotBeCacheableIfMatchedOnEverythingApartFromState "foo": "bar", }, }, - Response: testResponse, + Response: models.ResponseDetails{ + Body: "request matched", + }, }) simulation.AddPair(&models.RequestMatcherResponsePair{ @@ -1875,7 +1957,9 @@ func Test_StrongestMatch_ShouldNotBeCacheableIfMatchedOnEverythingApartFromState }, }, }, - Response: testResponse, + Response: models.ResponseDetails{ + Body: "request matched", + }, }) r := models.RequestDetails{ @@ -1944,7 +2028,9 @@ func Test_StrongestMatch__ShouldBeCacheableIfMatchedOnEverythingApartFromStateZe "foo": "bar", }, }, - Response: testResponse, + Response: models.ResponseDetails{ + Body: "request matched", + }, }) simulation.AddPair(&models.RequestMatcherResponsePair{ @@ -1956,7 +2042,9 @@ func Test_StrongestMatch__ShouldBeCacheableIfMatchedOnEverythingApartFromStateZe }, }, }, - Response: testResponse, + Response: models.ResponseDetails{ + Body: "request matched", + }, }) r := models.RequestDetails{