Skip to content

Commit

Permalink
Initialze query and header maps only if it's needed when capturing ne…
Browse files Browse the repository at this point in the history
…w pair
  • Loading branch information
tommysitu committed Jun 20, 2019
1 parent 7bd82cb commit d401b20
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 18 deletions.
36 changes: 21 additions & 15 deletions core/hoverfly_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,24 +155,30 @@ func (hf *Hoverfly) Save(request *models.RequestDetails, response *models.Respon
}
}

requestHeaders := map[string][]models.RequestFieldMatchers{}
for headerKey, headerValues := range headers {
requestHeaders[headerKey] = []models.RequestFieldMatchers{
{
Matcher: matchers.Exact,
Value: strings.Join(headerValues, ";"),
},
var requestHeaders map[string][]models.RequestFieldMatchers
if len(headers) > 0 {
requestHeaders = map[string][]models.RequestFieldMatchers{}
for headerKey, headerValues := range headers {
requestHeaders[headerKey] = []models.RequestFieldMatchers{
{
Matcher: matchers.Exact,
Value: strings.Join(headerValues, ";"),
},
}
}
}

queries := &models.QueryRequestFieldMatchers{}
for key, values := range request.Query {
queries.Add(key, []models.RequestFieldMatchers{
{
Matcher: matchers.Exact,
Value: strings.Join(values, ";"),
},
})
var queries *models.QueryRequestFieldMatchers
if len(request.Query) > 0 {
queries = &models.QueryRequestFieldMatchers{}
for key, values := range request.Query {
queries.Add(key, []models.RequestFieldMatchers{
{
Matcher: matchers.Exact,
Value: strings.Join(values, ";"),
},
})
}
}

pair := models.RequestMatcherResponsePair{
Expand Down
4 changes: 2 additions & 2 deletions core/hoverfly_funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -892,9 +892,9 @@ func Test_Hoverfly_Save_SavesIncompleteRequestAndResponseToSimulation(t *testing
Value: "",
}))

Expect(*unit.Simulation.GetMatchingPairs()[0].RequestMatcher.Query).To(HaveLen(0))
Expect(unit.Simulation.GetMatchingPairs()[0].RequestMatcher.Query).To(BeNil())

Expect(unit.Simulation.GetMatchingPairs()[0].RequestMatcher.Headers).To(HaveLen(0))
Expect(unit.Simulation.GetMatchingPairs()[0].RequestMatcher.Headers).To(BeNil())

Expect(unit.Simulation.GetMatchingPairs()[0].Response.Body).To(Equal("testresponsebody"))
Expect(unit.Simulation.GetMatchingPairs()[0].Response.Headers).To(HaveKeyWithValue("testheader", []string{"testvalue"}))
Expand Down
73 changes: 72 additions & 1 deletion functional-tests/core/ft_capture_mode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,78 @@ var _ = Describe("When I run Hoverfly", func() {
Value: "",
},
},
Query: &v2.QueryMatcherViewV5{},
}))

Expect(payload.RequestResponsePairs[0].Response).To(Equal(v2.ResponseDetailsViewV5{
Status: 200,
Body: "Hello world",
EncodedBody: false,
Headers: map[string][]string{
"Content-Length": {"11"},
"Content-Type": {"text/plain"},
"Date": {"date"},
"Hoverfly": {"Was-Here"},
},
Templated: false,
}))
})

It("Should not capture the existing 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")
w.Write([]byte("Hello world"))
}))

defer fakeServer.Close()
expectedDestination := strings.Replace(fakeServer.URL, "http://", "", 1)

existingSimBytes, err := ioutil.ReadFile("testdata/fake-server.json")
Expect(err).To(BeNil())
existingSim := string(existingSimBytes)
existingSim = strings.Replace(existingSim, "127.0.0.1:53751", expectedDestination, 1)
hoverfly.ImportSimulation(existingSim)

resp := hoverfly.Proxy(sling.New().Get(fakeServer.URL))
Expect(resp.StatusCode).To(Equal(200))


payload := hoverfly.ExportSimulation()

Expect(payload.RequestResponsePairs).To(HaveLen(1))

Expect(payload.RequestResponsePairs[0].RequestMatcher).To(Equal(v2.RequestMatcherViewV5{
Path: []v2.MatcherViewV5{
{
Matcher: matchers.Exact,
Value: "/",
},
},
Method: []v2.MatcherViewV5{
{
Matcher: matchers.Exact,
Value: "GET",
},
},
Destination: []v2.MatcherViewV5{
{
Matcher: matchers.Exact,
Value: expectedDestination,
},
},
Scheme: []v2.MatcherViewV5{
{
Matcher: matchers.Exact,
Value: "http",
},
},
Body: []v2.MatcherViewV5{
{
Matcher: matchers.Exact,
Value: "",
},
},
}))

Expect(payload.RequestResponsePairs[0].Response).To(Equal(v2.ResponseDetailsViewV5{
Expand Down
69 changes: 69 additions & 0 deletions functional-tests/core/testdata/fake-server.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"data": {
"pairs": [
{
"request": {
"path": [
{
"matcher": "exact",
"value": "/"
}
],
"method": [
{
"matcher": "exact",
"value": "GET"
}
],
"destination": [
{
"matcher": "exact",
"value": "127.0.0.1:53751"
}
],
"scheme": [
{
"matcher": "exact",
"value": "http"
}
],
"body": [
{
"matcher": "exact",
"value": ""
}
]
},
"response": {
"status": 200,
"body": "Hello world",
"encodedBody": false,
"headers": {
"Content-Length": [
"11"
],
"Content-Type": [
"text/plain"
],
"Date": [
"date"
],
"Hoverfly": [
"Was-Here"
]
},
"templated": false
}
}
],
"globalActions": {
"delays": [],
"delaysLogNormal": []
}
},
"meta": {
"schemaVersion": "v5",
"hoverflyVersion": "v1.0.0",
"timeExported": "2019-06-14T12:00:42+01:00"
}
}

0 comments on commit d401b20

Please sign in to comment.