@@ -2,7 +2,9 @@ package hoverfly_test
22
33import (
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
0 commit comments