Skip to content

Commit

Permalink
Pass through original query in capture mode
Browse files Browse the repository at this point in the history
  • Loading branch information
tommysitu authored and benjih committed Sep 22, 2017
1 parent 783a960 commit 21fbb4b
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
6 changes: 6 additions & 0 deletions core/models/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type RequestDetails struct {
Query map[string][]string
Body string
Headers map[string][]string
rawQuery string
}

func NewRequestDetailsFromHttpRequest(req *http.Request) (RequestDetails, error) {
Expand Down Expand Up @@ -93,6 +94,7 @@ func NewRequestDetailsFromHttpRequest(req *http.Request) (RequestDetails, error)
Query: req.URL.Query(),
Body: string(reqBody),
Headers: req.Header,
rawQuery: req.URL.RawQuery,
}

for key, value := range requestDetails.Query {
Expand Down Expand Up @@ -272,3 +274,7 @@ func (r *ResponseDetails) ConvertToResponseDetailsViewV4() v2.ResponseDetailsVie
TransitionsState: r.TransitionsState,
}
}

func (this RequestDetails) GetRawQuery() string {
return this.rawQuery
}
2 changes: 1 addition & 1 deletion core/modes/capture_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (this CaptureMode) Process(request *http.Request, details models.RequestDet
return ReturnErrorAndLog(request, err, &pair, "There was an error when applying middleware to http request", Capture)
}

modifiedRequest, err := ReconstructRequest(pair)
modifiedRequest, err := ReconstructRequestForPassThrough(pair)
if err != nil {
return ReturnErrorAndLog(request, err, &pair, "There was an error when applying middleware to http request", Capture)
}
Expand Down
24 changes: 24 additions & 0 deletions core/modes/modes.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,30 @@ func ReconstructRequest(pair models.RequestResponsePair) (*http.Request, error)
return newRequest, nil
}


// ReconstructRequest replaces original request with details provided in Constructor Payload.RequestMatcher
func ReconstructRequestForPassThrough(pair models.RequestResponsePair) (*http.Request, error) {
if pair.Request.Destination == "" {
return nil, fmt.Errorf("failed to reconstruct request, destination not specified")
}

newRequest, err := http.NewRequest(
pair.Request.Method,
fmt.Sprintf("%s://%s%s", pair.Request.Scheme, pair.Request.Destination, pair.Request.Path),
bytes.NewBuffer([]byte(pair.Request.Body)))

if err != nil {
return nil, err
}

newRequest.Method = pair.Request.Method

newRequest.URL.RawQuery = pair.Request.GetRawQuery()
newRequest.Header = pair.Request.Headers

return newRequest, nil
}

// ReconstructResponse changes original response with details provided in Constructor Payload.Response
func ReconstructResponse(request *http.Request, pair models.RequestResponsePair) *http.Response {
response := &http.Response{}
Expand Down
19 changes: 19 additions & 0 deletions functional-tests/core/ft_capture_mode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,25 @@ var _ = Describe("When I run Hoverfly", func() {

Expect(payload.RequestResponsePairs[0].RequestMatcher.Body.XmlMatch).To(Equal(util.StringToPointer(`<document/>`)))
})

It("Should pass through the original query", func() {

var capturedRequestQuery string

fakeServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
capturedRequestQuery = r.URL.RawQuery

w.Write([]byte("okay"))
}))

defer fakeServer.Close()

request, _ := sling.New().Post(fakeServer.URL + "?z=1&y=2&x=3").Request()
request.URL.RawQuery = "z=1&y=2&x=3"
hoverfly.ProxyRequest(request)

Expect(capturedRequestQuery).To(Equal("z=1&y=2&x=3"))
})
})
})
})
5 changes: 5 additions & 0 deletions functional-tests/functional_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,11 @@ func (this Hoverfly) Proxy(r *sling.Sling) *http.Response {
req, err := r.Request()
Expect(err).To(BeNil())

return this.ProxyRequest(req)
}

func (this Hoverfly) ProxyRequest(req *http.Request) *http.Response {

proxy, _ := url.Parse(this.proxyUrl)
proxyHttpClient := &http.Client{
Transport: &http.Transport{
Expand Down

0 comments on commit 21fbb4b

Please sign in to comment.