@@ -2,7 +2,9 @@ package hoverfly_test
2
2
3
3
import (
4
4
"bytes"
5
+ "encoding/json"
5
6
"fmt"
7
+ "github.com/SpectoLabs/hoverfly/core/views"
6
8
"github.com/dghubble/sling"
7
9
. "github.com/onsi/ginkgo"
8
10
. "github.com/onsi/gomega"
@@ -26,6 +28,15 @@ var _ = Describe("Running Hoverfly in various modes", func() {
26
28
BeforeEach (func () {
27
29
hoverflyCmd = startHoverfly (adminPort , proxyPort )
28
30
31
+ SetHoverflyMode ("capture" )
32
+ })
33
+
34
+ AfterEach (func () {
35
+ stopHoverfly ()
36
+ })
37
+
38
+ It ("Should capture the request and response" , func () {
39
+
29
40
fakeServer = httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
30
41
w .Header ().Set ("Content-Type" , "text/plain" )
31
42
w .Header ().Set ("Date" , "date" )
@@ -35,16 +46,9 @@ var _ = Describe("Running Hoverfly in various modes", func() {
35
46
defer fakeServer .Close ()
36
47
37
48
fakeServerUrl , _ = url .Parse (fakeServer .URL )
38
- SetHoverflyMode ("capture" )
39
49
resp := CallFakeServerThroughProxy (fakeServer )
40
50
Expect (resp .StatusCode ).To (Equal (200 ))
41
- })
42
-
43
- AfterEach (func () {
44
- stopHoverfly ()
45
- })
46
51
47
- It ("Should capture the request and response" , func () {
48
52
expectedDestination := strings .Replace (fakeServerUrl .String (), "http://" , "" , 1 )
49
53
50
54
recordsJson , err := ioutil .ReadAll (ExportHoverflyRecords ())
@@ -93,6 +97,43 @@ var _ = Describe("Running Hoverfly in various modes", func() {
93
97
]
94
98
}` , expectedDestination )))
95
99
})
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
+ })
96
137
})
97
138
98
139
Context ("with middleware" , func () {
@@ -279,7 +320,8 @@ var _ = Describe("Running Hoverfly in various modes", func() {
279
320
})
280
321
281
322
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 ))
283
325
Expect (requestBody ).To (Equal ("CHANGED_REQUEST_BODY" ))
284
326
})
285
327
0 commit comments