Skip to content

Commit 60b4aa2

Browse files
committed
#944 clone headers when copying request details, forward original request in spy mode
1 parent 9a0eee5 commit 60b4aa2

File tree

3 files changed

+65
-6
lines changed

3 files changed

+65
-6
lines changed

core/models/payload.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func NewRequestDetailsFromHttpRequest(req *http.Request) (RequestDetails, error)
100100
Scheme: scheme,
101101
Query: req.URL.Query(),
102102
Body: reqBody,
103-
Headers: req.Header,
103+
Headers: req.Header.Clone(),
104104
rawQuery: req.URL.RawQuery,
105105
}
106106

core/modes/spy_mode.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,7 @@ func (this SpyMode) Process(request *http.Request, details models.RequestDetails
4949

5050
if matchingErr != nil {
5151
log.Info("Going to call real server")
52-
modifiedRequest, err := ReconstructRequest(pair)
53-
if err != nil {
54-
return ReturnErrorAndLog(request, err, &pair, "There was an error when reconstructing the request.", Spy)
55-
}
56-
response, err := this.Hoverfly.DoRequest(modifiedRequest)
52+
response, err := this.Hoverfly.DoRequest(request)
5753
if err == nil {
5854
log.Info("Going to return response from real server")
5955
return newProcessResult(response, 0, nil), nil

functional-tests/core/ft_modes_test.go

+63
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,69 @@ var _ = Describe("Running Hoverfly in various modes", func() {
6666
})
6767
})
6868

69+
Context("When running in spy mode", func() {
70+
71+
var fakeServer *httptest.Server
72+
73+
Context("Without middleware", func() {
74+
75+
BeforeEach(func() {
76+
hoverfly.Start()
77+
78+
fakeServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
79+
w.Header().Set("Content-Type", "text/plain")
80+
w.Header().Set("Date", "date")
81+
w.Write([]byte("Hello world"))
82+
}))
83+
84+
hoverfly.SetMode("spy")
85+
hoverfly.ImportSimulation(`{
86+
"data": {
87+
"pairs": [
88+
{
89+
"request" : {
90+
"headers" : {
91+
"X-API-TEST" : [ {
92+
"value" : "test",
93+
"matcher" : "exact"
94+
} ]
95+
}
96+
},
97+
"response": {
98+
"status": 200,
99+
"body": "Simulated"
100+
}
101+
}
102+
]
103+
},
104+
"meta": {
105+
"schemaVersion": "v5"
106+
}
107+
}`)
108+
})
109+
110+
AfterEach(func() {
111+
fakeServer.Close()
112+
})
113+
114+
It("Should forward the request and get response from destination if match not found", func() {
115+
resp := hoverfly.Proxy(sling.New().Get(fakeServer.URL))
116+
Expect(resp.StatusCode).To(Equal(http.StatusOK))
117+
body, err := ioutil.ReadAll(resp.Body)
118+
Expect(err).To(BeNil())
119+
Expect(string(body)).To(Equal("Hello world"))
120+
})
121+
122+
It("Should simulate if match found", func() {
123+
resp := hoverfly.Proxy(sling.New().Get(fakeServer.URL).Set("X-API-TEST", "test"))
124+
Expect(resp.StatusCode).To(Equal(http.StatusOK))
125+
body, err := ioutil.ReadAll(resp.Body)
126+
Expect(err).To(BeNil())
127+
Expect(string(body)).To(Equal("Simulated"))
128+
})
129+
})
130+
})
131+
69132
Context("When running in synthesise mode", func() {
70133

71134
Context("With middleware", func() {

0 commit comments

Comments
 (0)