Skip to content

Commit 9fed32c

Browse files
schthmstommysitu
authored andcommitted
fix: address review comments
1 parent 785bb46 commit 9fed32c

3 files changed

Lines changed: 76 additions & 9 deletions

File tree

core/cmd/hoverfly/main.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -522,15 +522,6 @@ func main() {
522522
hoverfly.Authentication = authBackend
523523
hoverfly.HTTP = hv.GetDefaultHoverflyHTTPClient(hoverfly.Cfg.TLSVerification, hoverfly.Cfg.UpstreamProxy)
524524

525-
if *spy && *captureOnMiss {
526-
if err := hoverfly.SetModeWithArguments(v2.ModeView{
527-
Mode: modes.Spy,
528-
Arguments: v2.ModeArgumentsView{CaptureOnMiss: true},
529-
}); err != nil {
530-
log.WithError(err).Fatal("Failed to set spy mode with captureOnMiss")
531-
}
532-
}
533-
534525
// if add new user supplied - adding it to database
535526
if *addNew || *authEnabled {
536527
var err error
@@ -689,6 +680,19 @@ func main() {
689680
hoverfly.CacheMatcher.PreloadCache(hoverfly.Simulation)
690681
}
691682

683+
if *captureOnMiss && !*spy {
684+
log.Fatal("-capture-on-miss can only be used with -spy mode")
685+
}
686+
687+
if *spy && *captureOnMiss {
688+
if err := hoverfly.SetModeWithArguments(v2.ModeView{
689+
Mode: modes.Spy,
690+
Arguments: v2.ModeArgumentsView{CaptureOnMiss: true},
691+
}); err != nil {
692+
log.WithError(err).Fatal("Failed to set spy mode with captureOnMiss")
693+
}
694+
}
695+
692696
// start metrics registry flush
693697
if *metrics {
694698
hoverfly.Counter.Init()

core/modes/spy_mode_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ func Test_SpyMode_OnCacheMiss_WhenCaptureOnMissEnabled_SavesRequestAndResponse(t
180180
Expect(stub.savedResponse).ToNot(BeNil())
181181
Expect(stub.savedResponse.Status).To(Equal(200))
182182
Expect(stub.savedResponse.Body).To(Equal("test"))
183+
Expect(stub.savedArguments).ToNot(BeNil())
184+
Expect(stub.savedArguments.CaptureOnMiss).To(BeTrue())
183185
}
184186

185187
func Test_SpyMode_OnCacheMiss_WhenCaptureOnMissDisabled_DoesNotSave(t *testing.T) {

functional-tests/core/ft_modes_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"os"
99
"strings"
1010

11+
"github.com/SpectoLabs/hoverfly/core/handlers/v2"
1112
"github.com/SpectoLabs/hoverfly/functional-tests"
1213
"github.com/dghubble/sling"
1314
. "github.com/onsi/ginkgo"
@@ -127,6 +128,66 @@ var _ = Describe("Running Hoverfly in various modes", func() {
127128
Expect(string(body)).To(Equal("Simulated"))
128129
})
129130
})
131+
132+
Context("With capture-on-miss enabled", func() {
133+
134+
var fakeServer *httptest.Server
135+
136+
BeforeEach(func() {
137+
hoverfly.Start()
138+
139+
fakeServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
140+
w.Header().Set("Content-Type", "text/plain")
141+
w.Write([]byte("Real response"))
142+
}))
143+
144+
hoverfly.SetModeWithArgs("spy", v2.ModeArgumentsView{CaptureOnMiss: true})
145+
hoverfly.ImportSimulation(`{
146+
"data": {
147+
"pairs": [
148+
{
149+
"request": {
150+
"headers": {
151+
"X-API-TEST": [ { "value": "test", "matcher": "exact" } ]
152+
}
153+
},
154+
"response": {
155+
"status": 200,
156+
"body": "Simulated"
157+
}
158+
}
159+
]
160+
},
161+
"meta": { "schemaVersion": "v5" }
162+
}`)
163+
})
164+
165+
AfterEach(func() {
166+
fakeServer.Close()
167+
})
168+
169+
It("Should save the request/response pair when there is a cache miss", func() {
170+
resp := hoverfly.Proxy(sling.New().Get(fakeServer.URL))
171+
Expect(resp.StatusCode).To(Equal(http.StatusOK))
172+
body, err := ioutil.ReadAll(resp.Body)
173+
Expect(err).To(BeNil())
174+
Expect(string(body)).To(Equal("Real response"))
175+
176+
simulation := hoverfly.ExportSimulation()
177+
Expect(simulation.RequestResponsePairs).To(HaveLen(2))
178+
})
179+
180+
It("Should not save the request/response pair when there is a cache hit", func() {
181+
resp := hoverfly.Proxy(sling.New().Get(fakeServer.URL).Set("X-API-TEST", "test"))
182+
Expect(resp.StatusCode).To(Equal(http.StatusOK))
183+
body, err := ioutil.ReadAll(resp.Body)
184+
Expect(err).To(BeNil())
185+
Expect(string(body)).To(Equal("Simulated"))
186+
187+
simulation := hoverfly.ExportSimulation()
188+
Expect(simulation.RequestResponsePairs).To(HaveLen(1))
189+
})
190+
})
130191
})
131192

132193
Context("When running in synthesise mode", func() {

0 commit comments

Comments
 (0)