Skip to content

Commit

Permalink
#1009 fix corrupted pre-load cache
Browse files Browse the repository at this point in the history
  • Loading branch information
tommysitu committed Mar 8, 2022
1 parent 9e24f6e commit d21085a
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 5 deletions.
4 changes: 3 additions & 1 deletion core/matching/cache_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,10 @@ func (this *CacheMatcher) PreloadCache(simulation *models.Simulation) error {
return errors.NoCacheSetError()
}
for _, pair := range simulation.GetMatchingPairs() {

if requestDetails := pair.RequestMatcher.ToEagerlyCacheable(); requestDetails != nil {
this.SaveRequestMatcherResponsePair(*requestDetails, &pair, nil)
pairCopy := pair
this.SaveRequestMatcherResponsePair(*requestDetails, &pairCopy, nil)
}
}

Expand Down
73 changes: 69 additions & 4 deletions core/matching/cache_matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func Test_CacheMatcher_PreloadCache_WillPreemptivelyCacheFullExactMatchRequestMa

simulation := models.NewSimulation()

simulation.AddPair(&models.RequestMatcherResponsePair{
pair1 := &models.RequestMatcherResponsePair{
RequestMatcher: models.RequestMatcher{
Body: []models.RequestFieldMatchers{
{
Expand Down Expand Up @@ -148,14 +148,79 @@ func Test_CacheMatcher_PreloadCache_WillPreemptivelyCacheFullExactMatchRequestMa
},
Response: models.ResponseDetails{
Status: 200,
Body: "body",
Body: "body 1",
},
})
}

pair2 := &models.RequestMatcherResponsePair{
RequestMatcher: models.RequestMatcher{
Body: []models.RequestFieldMatchers{
{
Matcher: matchers.Exact,
Value: "body",
},
},

Destination: []models.RequestFieldMatchers{
{
Matcher: matchers.Exact,
Value: "destination",
},
},
Method: []models.RequestFieldMatchers{
{
Matcher: matchers.Exact,
Value: "method",
},
},
Path: []models.RequestFieldMatchers{
{
Matcher: matchers.Exact,
Value: "path",
},
},
Query: &models.QueryRequestFieldMatchers{
"queryKey": {
{
Matcher: matchers.Exact,
Value: "queryValue",
},
},
},
Scheme: []models.RequestFieldMatchers{
{
Matcher: matchers.Exact,
Value: "scheme",
},
},
},
Response: models.ResponseDetails{
Status: 200,
Body: "body 2",
},
}
simulation.AddPair(pair1)
simulation.AddPair(pair2)

err := unit.PreloadCache(simulation)

Expect(err).To(BeNil())
Expect(unit.RequestCache.RecordsCount()).To(Equal(1))
Expect(unit.RequestCache.RecordsCount()).To(Equal(2))

cacheable1 := *pair1.RequestMatcher.ToEagerlyCacheable()
cached1, _ := unit.RequestCache.Get(cacheable1.Hash())
var cachedResponse1 *models.CachedResponse
cachedResponse1 = cached1.(*models.CachedResponse)
Expect(cachedResponse1.MatchingPair.Response.Body).To(Equal("body 1"))
Expect(cachedResponse1.MatchingPair.RequestMatcher.Query).To(BeNil())

cacheable2 := *pair2.RequestMatcher.ToEagerlyCacheable()
cached2, _ := unit.RequestCache.Get(cacheable2.Hash())
var cachedResponse2 *models.CachedResponse
cachedResponse2 = cached2.(*models.CachedResponse)
Expect(cachedResponse2.MatchingPair.Response.Body).To(Equal("body 2"))
Expect(cachedResponse2.MatchingPair.RequestMatcher.Query.Get("queryKey")[0].Matcher).To(Equal(matchers.Exact))
Expect(cachedResponse2.MatchingPair.RequestMatcher.Query.Get("queryKey")[0].Value).To(Equal("queryValue"))
}

func Test_CacheMatcher_PreloadCache_WillNotPreemptivelyCacheRequestMatchersWithoutExactMatches(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions core/models/request_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ func (q *QueryRequestFieldMatchers) Add(k string, v []RequestFieldMatchers) {
(*q)[k] = v
}

func (q *QueryRequestFieldMatchers) Get(k string) []RequestFieldMatchers {
return (*q)[k]
}

func (this RequestMatcher) IncludesHeaderMatching() bool {
return this.Headers != nil && len(this.Headers) > 0
}
Expand Down

0 comments on commit d21085a

Please sign in to comment.