Skip to content

Commit

Permalink
#944 clone headers when copying request details, forward original req…
Browse files Browse the repository at this point in the history
…uest in spy mode
  • Loading branch information
tommysitu committed Aug 30, 2020
1 parent 9a0eee5 commit 60b4aa2
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 6 deletions.
2 changes: 1 addition & 1 deletion core/models/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down
6 changes: 1 addition & 5 deletions core/modes/spy_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 newProcessResult(response, 0, nil), nil
Expand Down
63 changes: 63 additions & 0 deletions functional-tests/core/ft_modes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 60b4aa2

Please sign in to comment.