|
8 | 8 | "os" |
9 | 9 | "strings" |
10 | 10 |
|
| 11 | + "github.com/SpectoLabs/hoverfly/core/handlers/v2" |
11 | 12 | "github.com/SpectoLabs/hoverfly/functional-tests" |
12 | 13 | "github.com/dghubble/sling" |
13 | 14 | . "github.com/onsi/ginkgo" |
@@ -127,6 +128,66 @@ var _ = Describe("Running Hoverfly in various modes", func() { |
127 | 128 | Expect(string(body)).To(Equal("Simulated")) |
128 | 129 | }) |
129 | 130 | }) |
| 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 | + }) |
130 | 191 | }) |
131 | 192 |
|
132 | 193 | Context("When running in synthesise mode", func() { |
|
0 commit comments