Skip to content

Commit e9578bf

Browse files
Merge pull request #259 from SpectoLabs/redirects
Fixed a bug where redirects where not saved
2 parents 2b9652d + 21471a4 commit e9578bf

File tree

4 files changed

+62
-11
lines changed

4 files changed

+62
-11
lines changed

circle.yml

+7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@ machine:
44

55
environment:
66
GOPATH: /home/ubuntu/.go_workspace
7+
GODIST: "go1.7.linux-amd64.tar.gz"
8+
IMPORT_PATH: "github.com/$CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME"
79

10+
post:
11+
- mkdir -p download
12+
- test -e download/$GODIST || curl -o download/$GODIST https://storage.googleapis.com/golang/$GODIST
13+
- sudo rm -rf /usr/local/go
14+
- sudo tar -C /usr/local -xzf download/$GODIST
815
checkout:
916
post:
1017
- mkdir -p $GOPATH/src/github.com/SpectoLabs/hoverfly || echo "project dir already exists"

core/hoverfly.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,14 @@ func GetNewHoverfly(cfg *Configuration, requestCache, metadataCache cache.Cache,
6565
TemplateStore: matching.RequestTemplateStore{},
6666
Webserver: &cfg.Webserver,
6767
}
68+
6869
h := &Hoverfly{
6970
RequestCache: requestCache,
7071
MetadataCache: metadataCache,
7172
Authentication: authentication,
72-
HTTP: &http.Client{Transport: &http.Transport{
73+
HTTP: &http.Client{CheckRedirect: func(req *http.Request, via []*http.Request) error {
74+
return http.ErrUseLastResponse
75+
}, Transport: &http.Transport{
7376
TLSClientConfig: &tls.Config{InsecureSkipVerify: cfg.TLSVerification},
7477
}},
7578
Cfg: cfg,
@@ -266,7 +269,6 @@ func (hf *Hoverfly) processRequest(req *http.Request) *http.Response {
266269
}
267270

268271
return response
269-
270272
}
271273

272274
// AddHook - adds a hook to DBClient

functional-tests/core/ft_modes_test.go

+50-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package hoverfly_test
22

33
import (
44
"bytes"
5+
"encoding/json"
56
"fmt"
7+
"github.com/SpectoLabs/hoverfly/core/views"
68
"github.com/dghubble/sling"
79
. "github.com/onsi/ginkgo"
810
. "github.com/onsi/gomega"
@@ -26,6 +28,15 @@ var _ = Describe("Running Hoverfly in various modes", func() {
2628
BeforeEach(func() {
2729
hoverflyCmd = startHoverfly(adminPort, proxyPort)
2830

31+
SetHoverflyMode("capture")
32+
})
33+
34+
AfterEach(func() {
35+
stopHoverfly()
36+
})
37+
38+
It("Should capture the request and response", func() {
39+
2940
fakeServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
3041
w.Header().Set("Content-Type", "text/plain")
3142
w.Header().Set("Date", "date")
@@ -35,16 +46,9 @@ var _ = Describe("Running Hoverfly in various modes", func() {
3546
defer fakeServer.Close()
3647

3748
fakeServerUrl, _ = url.Parse(fakeServer.URL)
38-
SetHoverflyMode("capture")
3949
resp := CallFakeServerThroughProxy(fakeServer)
4050
Expect(resp.StatusCode).To(Equal(200))
41-
})
42-
43-
AfterEach(func() {
44-
stopHoverfly()
45-
})
4651

47-
It("Should capture the request and response", func() {
4852
expectedDestination := strings.Replace(fakeServerUrl.String(), "http://", "", 1)
4953

5054
recordsJson, err := ioutil.ReadAll(ExportHoverflyRecords())
@@ -93,6 +97,43 @@ var _ = Describe("Running Hoverfly in various modes", func() {
9397
]
9498
}`, expectedDestination)))
9599
})
100+
101+
It("Should capture a redirect response", func() {
102+
103+
fakeServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
104+
w.Header().Set("Content-Type", "text/plain")
105+
w.Header().Set("Date", "date")
106+
w.Write([]byte("redirection got you here"))
107+
}))
108+
fakeServerUrl, _ := url.Parse(fakeServer.URL)
109+
fakeServerRedirect := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
110+
w.Header().Set("Location", fakeServer.URL)
111+
w.Header().Set("Content-Type", "text/plain")
112+
w.WriteHeader(301)
113+
}))
114+
fakeServerRedirectUrl, _ := url.Parse(fakeServerRedirect.URL)
115+
116+
defer fakeServer.Close()
117+
defer fakeServerRedirect.Close()
118+
119+
resp := CallFakeServerThroughProxy(fakeServerRedirect)
120+
Expect(resp.StatusCode).To(Equal(301))
121+
122+
expectedRedirectDestination := strings.Replace(fakeServerRedirectUrl.String(), "http://", "", 1)
123+
124+
recordsJson, err := ioutil.ReadAll(ExportHoverflyRecords())
125+
Expect(err).To(BeNil())
126+
127+
payload := views.RequestResponsePairPayload{}
128+
129+
json.Unmarshal(recordsJson, &payload)
130+
Expect(payload.Data).To(HaveLen(1))
131+
132+
Expect(payload.Data[0].Request.Destination).To(Equal(expectedRedirectDestination))
133+
134+
Expect(payload.Data[0].Response.Status).To(Equal(301))
135+
Expect(payload.Data[0].Response.Headers["Location"][0]).To(Equal(fakeServerUrl.String()))
136+
})
96137
})
97138

98139
Context("with middleware", func() {
@@ -279,7 +320,8 @@ var _ = Describe("Running Hoverfly in various modes", func() {
279320
})
280321

281322
It("Should modify the request using middleware", func() {
282-
DoRequestThroughProxy(sling.New().Get(fakeServer.URL))
323+
resp := DoRequestThroughProxy(sling.New().Get(fakeServer.URL))
324+
Expect(resp.StatusCode).To(Equal(200))
283325
Expect(requestBody).To(Equal("CHANGED_REQUEST_BODY"))
284326
})
285327

functional-tests/core/ft_suite_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func DoRequestThroughProxy(r *sling.Sling) *http.Response {
166166
Expect(err).To(BeNil())
167167

168168
proxy, err := url.Parse(hoverflyProxyUrl)
169-
proxyHttpClient := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxy)}}
169+
proxyHttpClient := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxy)}, CheckRedirect: func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse }}
170170
response, err := proxyHttpClient.Do(req)
171171

172172
Expect(err).To(BeNil())

0 commit comments

Comments
 (0)