Skip to content

Commit d21085a

Browse files
committed
SpectoLabs#1009 fix corrupted pre-load cache
1 parent 9e24f6e commit d21085a

File tree

3 files changed

+76
-5
lines changed

3 files changed

+76
-5
lines changed

core/matching/cache_matcher.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,10 @@ func (this *CacheMatcher) PreloadCache(simulation *models.Simulation) error {
143143
return errors.NoCacheSetError()
144144
}
145145
for _, pair := range simulation.GetMatchingPairs() {
146+
146147
if requestDetails := pair.RequestMatcher.ToEagerlyCacheable(); requestDetails != nil {
147-
this.SaveRequestMatcherResponsePair(*requestDetails, &pair, nil)
148+
pairCopy := pair
149+
this.SaveRequestMatcherResponsePair(*requestDetails, &pairCopy, nil)
148150
}
149151
}
150152

core/matching/cache_matcher_test.go

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func Test_CacheMatcher_PreloadCache_WillPreemptivelyCacheFullExactMatchRequestMa
106106

107107
simulation := models.NewSimulation()
108108

109-
simulation.AddPair(&models.RequestMatcherResponsePair{
109+
pair1 := &models.RequestMatcherResponsePair{
110110
RequestMatcher: models.RequestMatcher{
111111
Body: []models.RequestFieldMatchers{
112112
{
@@ -148,14 +148,79 @@ func Test_CacheMatcher_PreloadCache_WillPreemptivelyCacheFullExactMatchRequestMa
148148
},
149149
Response: models.ResponseDetails{
150150
Status: 200,
151-
Body: "body",
151+
Body: "body 1",
152152
},
153-
})
153+
}
154+
155+
pair2 := &models.RequestMatcherResponsePair{
156+
RequestMatcher: models.RequestMatcher{
157+
Body: []models.RequestFieldMatchers{
158+
{
159+
Matcher: matchers.Exact,
160+
Value: "body",
161+
},
162+
},
163+
164+
Destination: []models.RequestFieldMatchers{
165+
{
166+
Matcher: matchers.Exact,
167+
Value: "destination",
168+
},
169+
},
170+
Method: []models.RequestFieldMatchers{
171+
{
172+
Matcher: matchers.Exact,
173+
Value: "method",
174+
},
175+
},
176+
Path: []models.RequestFieldMatchers{
177+
{
178+
Matcher: matchers.Exact,
179+
Value: "path",
180+
},
181+
},
182+
Query: &models.QueryRequestFieldMatchers{
183+
"queryKey": {
184+
{
185+
Matcher: matchers.Exact,
186+
Value: "queryValue",
187+
},
188+
},
189+
},
190+
Scheme: []models.RequestFieldMatchers{
191+
{
192+
Matcher: matchers.Exact,
193+
Value: "scheme",
194+
},
195+
},
196+
},
197+
Response: models.ResponseDetails{
198+
Status: 200,
199+
Body: "body 2",
200+
},
201+
}
202+
simulation.AddPair(pair1)
203+
simulation.AddPair(pair2)
154204

155205
err := unit.PreloadCache(simulation)
156206

157207
Expect(err).To(BeNil())
158-
Expect(unit.RequestCache.RecordsCount()).To(Equal(1))
208+
Expect(unit.RequestCache.RecordsCount()).To(Equal(2))
209+
210+
cacheable1 := *pair1.RequestMatcher.ToEagerlyCacheable()
211+
cached1, _ := unit.RequestCache.Get(cacheable1.Hash())
212+
var cachedResponse1 *models.CachedResponse
213+
cachedResponse1 = cached1.(*models.CachedResponse)
214+
Expect(cachedResponse1.MatchingPair.Response.Body).To(Equal("body 1"))
215+
Expect(cachedResponse1.MatchingPair.RequestMatcher.Query).To(BeNil())
216+
217+
cacheable2 := *pair2.RequestMatcher.ToEagerlyCacheable()
218+
cached2, _ := unit.RequestCache.Get(cacheable2.Hash())
219+
var cachedResponse2 *models.CachedResponse
220+
cachedResponse2 = cached2.(*models.CachedResponse)
221+
Expect(cachedResponse2.MatchingPair.Response.Body).To(Equal("body 2"))
222+
Expect(cachedResponse2.MatchingPair.RequestMatcher.Query.Get("queryKey")[0].Matcher).To(Equal(matchers.Exact))
223+
Expect(cachedResponse2.MatchingPair.RequestMatcher.Query.Get("queryKey")[0].Value).To(Equal("queryValue"))
159224
}
160225

161226
func Test_CacheMatcher_PreloadCache_WillNotPreemptivelyCacheRequestMatchersWithoutExactMatches(t *testing.T) {

core/models/request_matcher.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ func (q *QueryRequestFieldMatchers) Add(k string, v []RequestFieldMatchers) {
195195
(*q)[k] = v
196196
}
197197

198+
func (q *QueryRequestFieldMatchers) Get(k string) []RequestFieldMatchers {
199+
return (*q)[k]
200+
}
201+
198202
func (this RequestMatcher) IncludesHeaderMatching() bool {
199203
return this.Headers != nil && len(this.Headers) > 0
200204
}

0 commit comments

Comments
 (0)