From d213537d524585d0b69cb400f0f44c73b8f74a91 Mon Sep 17 00:00:00 2001 From: Tommy Situ Date: Sun, 30 Aug 2020 23:22:36 +0100 Subject: [PATCH] #944 clone headers when copying request details, forward original request in spy mode --- core/models/payload.go | 2 +- core/modes/spy_mode.go | 6 +-- functional-tests/core/ft_modes_test.go | 63 ++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 6 deletions(-) diff --git a/core/models/payload.go b/core/models/payload.go index 21d41690f..c803dd90c 100644 --- a/core/models/payload.go +++ b/core/models/payload.go @@ -100,7 +100,7 @@ func NewRequestDetailsFromHttpRequest(req *http.Request) (RequestDetails, error) Scheme: scheme, Query: req.URL.Query(), Body: reqBody, - Headers: req.Header, + Headers: req.Header.Clone(), rawQuery: req.URL.RawQuery, } diff --git a/core/modes/spy_mode.go b/core/modes/spy_mode.go index b19563371..853733f05 100644 --- a/core/modes/spy_mode.go +++ b/core/modes/spy_mode.go @@ -49,11 +49,7 @@ func (this SpyMode) Process(request *http.Request, details models.RequestDetails if matchingErr != nil { log.Info("Going to call real server") - modifiedRequest, err := ReconstructRequest(pair) - if err != nil { - return ReturnErrorAndLog(request, err, &pair, "There was an error when reconstructing the request.", Spy) - } - response, err := this.Hoverfly.DoRequest(modifiedRequest) + response, err := this.Hoverfly.DoRequest(request) if err == nil { log.Info("Going to return response from real server") return response, nil diff --git a/functional-tests/core/ft_modes_test.go b/functional-tests/core/ft_modes_test.go index f66681785..4e7117b4f 100644 --- a/functional-tests/core/ft_modes_test.go +++ b/functional-tests/core/ft_modes_test.go @@ -66,6 +66,69 @@ var _ = Describe("Running Hoverfly in various modes", func() { }) }) + Context("When running in spy mode", func() { + + var fakeServer *httptest.Server + + Context("Without middleware", func() { + + BeforeEach(func() { + hoverfly.Start() + + fakeServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/plain") + w.Header().Set("Date", "date") + w.Write([]byte("Hello world")) + })) + + hoverfly.SetMode("spy") + hoverfly.ImportSimulation(`{ + "data": { + "pairs": [ + { + "request" : { + "headers" : { + "X-API-TEST" : [ { + "value" : "test", + "matcher" : "exact" + } ] + } + }, + "response": { + "status": 200, + "body": "Simulated" + } + } + ] + }, + "meta": { + "schemaVersion": "v5" + } + }`) + }) + + AfterEach(func() { + fakeServer.Close() + }) + + It("Should forward the request and get response from destination if match not found", func() { + resp := hoverfly.Proxy(sling.New().Get(fakeServer.URL)) + Expect(resp.StatusCode).To(Equal(http.StatusOK)) + body, err := ioutil.ReadAll(resp.Body) + Expect(err).To(BeNil()) + Expect(string(body)).To(Equal("Hello world")) + }) + + It("Should simulate if match found", func() { + resp := hoverfly.Proxy(sling.New().Get(fakeServer.URL).Set("X-API-TEST", "test")) + Expect(resp.StatusCode).To(Equal(http.StatusOK)) + body, err := ioutil.ReadAll(resp.Body) + Expect(err).To(BeNil()) + Expect(string(body)).To(Equal("Simulated")) + }) + }) + }) + Context("When running in synthesise mode", func() { Context("With middleware", func() {