func (this *RequestMatcher) SaveRequestResponsePair(pair *models.RequestResponsePair) error { var key string if *this.Webserver { key = pair.IdWithoutHost() } else { key = pair.Id() } log.WithFields(log.Fields{ "path": pair.Request.Path, "rawQuery": pair.Request.Query, "requestMethod": pair.Request.Method, "bodyLen": len(pair.Request.Body), "destination": pair.Request.Destination, "hashKey": key, }).Debug("Capturing") pairBytes, err := pair.Encode() if err != nil { return err } return this.RequestCache.Set([]byte(key), pairBytes) }
func Test_rebuildHashes_whenDataIsHashedForAProxy_andStillAProxy_keysAreNotChanged(t *testing.T) { RegisterTestingT(t) webserver := false db := cache.NewInMemoryCache() pair := models.RequestResponsePair{ Request: models.RequestDetails{ Path: "/hello", Destination: "a-host.com", }, Response: models.ResponseDetails{ Body: "a body", }, } pairBytes, _ := pair.Encode() db.Set([]byte(pair.Id()), pairBytes) rebuildHashes(db, webserver) result, err := db.Get([]byte(pair.Id())) Expect(err).To(BeNil()) Expect(result).To(Equal(pairBytes)) }
// save gets request fingerprint, extracts request body, status code and headers, then saves it to cache func (hf *Hoverfly) save(req *http.Request, reqBody []byte, resp *http.Response, respBody []byte) { if resp == nil { resp = emptyResp } else { responseObj := models.ResponseDetails{ Status: resp.StatusCode, Body: string(respBody), Headers: resp.Header, } requestObj := models.RequestDetails{ Path: req.URL.Path, Method: req.Method, Destination: req.Host, Scheme: req.URL.Scheme, Query: req.URL.RawQuery, Body: string(reqBody), Headers: req.Header, } pair := models.RequestResponsePair{ Response: responseObj, Request: requestObj, } err := hf.RequestMatcher.SaveRequestResponsePair(&pair) if err != nil { log.WithFields(log.Fields{ "error": err.Error(), }).Error("Failed to save payload") } pairBytes, err := pair.Encode() if err != nil { log.WithFields(log.Fields{ "error": err.Error(), }).Error("Failed to serialize payload") } else { // hook var en Entry en.ActionType = ActionTypeRequestCaptured en.Message = "captured" en.Time = time.Now() en.Data = pairBytes if err := hf.Hooks.Fire(ActionTypeRequestCaptured, &en); err != nil { log.WithFields(log.Fields{ "error": err.Error(), "message": en.Message, "actionType": ActionTypeRequestCaptured, }).Error("failed to fire hook") } } } }
func TestRequestResponsePairEncodeEmpty(t *testing.T) { RegisterTestingT(t) pair := models.RequestResponsePair{} pairBytes, err := pair.Encode() Expect(err).To(BeNil()) _, err = models.NewRequestResponsePairFromBytes(pairBytes) Expect(err).To(BeNil()) }
func TestRequestResponsePairEncodeDecode(t *testing.T) { RegisterTestingT(t) resp := models.ResponseDetails{ Status: 200, Body: "body here", } pair := models.RequestResponsePair{Response: resp} pairBytes, err := pair.Encode() Expect(err).To(BeNil()) pairFromBytes, err := models.NewRequestResponsePairFromBytes(pairBytes) Expect(err).To(BeNil()) Expect(pairFromBytes.Response.Body).To(Equal(resp.Body)) Expect(pairFromBytes.Response.Status).To(Equal(resp.Status)) }
func TestHoverflyGetSimulationReturnsMultipleRequestResponsePairs(t *testing.T) { RegisterTestingT(t) server, unit := testTools(201, `{'message': 'here'}`) defer server.Close() recording := models.RequestResponsePair{ Request: models.RequestDetails{ Destination: "testhost.com", Path: "/test", }, Response: models.ResponseDetails{ Status: 200, Body: "test", }, } recordingBytes, err := recording.Encode() Expect(err).To(BeNil()) unit.RequestCache.Set([]byte("key"), recordingBytes) unit.RequestCache.Set([]byte("key2"), recordingBytes) simulation, err := unit.GetSimulation() Expect(err).To(BeNil()) Expect(simulation.DataView.RequestResponsePairs).To(HaveLen(2)) Expect(*simulation.DataView.RequestResponsePairs[0].Request.Destination).To(Equal("testhost.com")) Expect(*simulation.DataView.RequestResponsePairs[0].Request.Path).To(Equal("/test")) Expect(*simulation.DataView.RequestResponsePairs[0].Request.RequestType).To(Equal("recording")) Expect(simulation.DataView.RequestResponsePairs[0].Response.Status).To(Equal(200)) Expect(simulation.DataView.RequestResponsePairs[0].Response.Body).To(Equal("test")) Expect(*simulation.DataView.RequestResponsePairs[1].Request.Destination).To(Equal("testhost.com")) Expect(*simulation.DataView.RequestResponsePairs[1].Request.Path).To(Equal("/test")) Expect(*simulation.DataView.RequestResponsePairs[1].Request.RequestType).To(Equal("recording")) Expect(simulation.DataView.RequestResponsePairs[1].Response.Status).To(Equal(200)) Expect(simulation.DataView.RequestResponsePairs[1].Response.Body).To(Equal("test")) }