Skip to content

Commit

Permalink
Merge pull request #259 from SpectoLabs/redirects
Browse files Browse the repository at this point in the history
Fixed a bug where redirects where not saved
  • Loading branch information
danielbryantuk authored Sep 8, 2016
2 parents 2b9652d + 21471a4 commit e9578bf
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 11 deletions.
7 changes: 7 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ machine:

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

post:
- mkdir -p download
- test -e download/$GODIST || curl -o download/$GODIST https://storage.googleapis.com/golang/$GODIST
- sudo rm -rf /usr/local/go
- sudo tar -C /usr/local -xzf download/$GODIST
checkout:
post:
- mkdir -p $GOPATH/src/github.com/SpectoLabs/hoverfly || echo "project dir already exists"
Expand Down
6 changes: 4 additions & 2 deletions core/hoverfly.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,14 @@ func GetNewHoverfly(cfg *Configuration, requestCache, metadataCache cache.Cache,
TemplateStore: matching.RequestTemplateStore{},
Webserver: &cfg.Webserver,
}

h := &Hoverfly{
RequestCache: requestCache,
MetadataCache: metadataCache,
Authentication: authentication,
HTTP: &http.Client{Transport: &http.Transport{
HTTP: &http.Client{CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}, Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: cfg.TLSVerification},
}},
Cfg: cfg,
Expand Down Expand Up @@ -266,7 +269,6 @@ func (hf *Hoverfly) processRequest(req *http.Request) *http.Response {
}

return response

}

// AddHook - adds a hook to DBClient
Expand Down
58 changes: 50 additions & 8 deletions functional-tests/core/ft_modes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package hoverfly_test

import (
"bytes"
"encoding/json"
"fmt"
"github.com/SpectoLabs/hoverfly/core/views"
"github.com/dghubble/sling"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand All @@ -26,6 +28,15 @@ var _ = Describe("Running Hoverfly in various modes", func() {
BeforeEach(func() {
hoverflyCmd = startHoverfly(adminPort, proxyPort)

SetHoverflyMode("capture")
})

AfterEach(func() {
stopHoverfly()
})

It("Should capture the request and response", func() {

fakeServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.Header().Set("Date", "date")
Expand All @@ -35,16 +46,9 @@ var _ = Describe("Running Hoverfly in various modes", func() {
defer fakeServer.Close()

fakeServerUrl, _ = url.Parse(fakeServer.URL)
SetHoverflyMode("capture")
resp := CallFakeServerThroughProxy(fakeServer)
Expect(resp.StatusCode).To(Equal(200))
})

AfterEach(func() {
stopHoverfly()
})

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

recordsJson, err := ioutil.ReadAll(ExportHoverflyRecords())
Expand Down Expand Up @@ -93,6 +97,43 @@ var _ = Describe("Running Hoverfly in various modes", func() {
]
}`, expectedDestination)))
})

It("Should capture a redirect response", func() {

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("redirection got you here"))
}))
fakeServerUrl, _ := url.Parse(fakeServer.URL)
fakeServerRedirect := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Location", fakeServer.URL)
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(301)
}))
fakeServerRedirectUrl, _ := url.Parse(fakeServerRedirect.URL)

defer fakeServer.Close()
defer fakeServerRedirect.Close()

resp := CallFakeServerThroughProxy(fakeServerRedirect)
Expect(resp.StatusCode).To(Equal(301))

expectedRedirectDestination := strings.Replace(fakeServerRedirectUrl.String(), "http://", "", 1)

recordsJson, err := ioutil.ReadAll(ExportHoverflyRecords())
Expect(err).To(BeNil())

payload := views.RequestResponsePairPayload{}

json.Unmarshal(recordsJson, &payload)
Expect(payload.Data).To(HaveLen(1))

Expect(payload.Data[0].Request.Destination).To(Equal(expectedRedirectDestination))

Expect(payload.Data[0].Response.Status).To(Equal(301))
Expect(payload.Data[0].Response.Headers["Location"][0]).To(Equal(fakeServerUrl.String()))
})
})

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

It("Should modify the request using middleware", func() {
DoRequestThroughProxy(sling.New().Get(fakeServer.URL))
resp := DoRequestThroughProxy(sling.New().Get(fakeServer.URL))
Expect(resp.StatusCode).To(Equal(200))
Expect(requestBody).To(Equal("CHANGED_REQUEST_BODY"))
})

Expand Down
2 changes: 1 addition & 1 deletion functional-tests/core/ft_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func DoRequestThroughProxy(r *sling.Sling) *http.Response {
Expect(err).To(BeNil())

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

Expect(err).To(BeNil())
Expand Down

0 comments on commit e9578bf

Please sign in to comment.