@@ -76,7 +76,16 @@ func runHTTPRequest(
7676 req .SetBasicAuth (requestStep .Request .BasicAuth .Username , requestStep .Request .BasicAuth .Password )
7777 }
7878
79- resp , err := client .Do (req )
79+ requestClient := client
80+ if requestStep .Request .FollowRedirects != nil && ! * requestStep .Request .FollowRedirects {
81+ clientCopy := * client
82+ clientCopy .CheckRedirect = func (req * http.Request , via []* http.Request ) error {
83+ return http .ErrUseLastResponse
84+ }
85+ requestClient = & clientCopy
86+ }
87+
88+ resp , err := requestClient .Do (req )
8089 if err != nil {
8190 errString := fmt .Sprintf ("Failed to fetch: %s" , err .Error ())
8291 result = api.HTTPRequestResult {Err : errString }
@@ -100,7 +109,12 @@ func runHTTPRequest(
100109 trailers [k ] = strings .Join (v , "," )
101110 }
102111
103- parseVariables (body , requestStep .ResponseVariables , variables )
112+ if err := parseVariables (body , requestStep .ResponseVariables , variables ); err != nil {
113+ return api.HTTPRequestResult {Err : fmt .Sprintf ("Failed to parse response variable: %s" , err )}
114+ }
115+ if err := parseHeaderVariables (headers , requestStep .ResponseHeaderVariables , variables ); err != nil {
116+ return api.HTTPRequestResult {Err : fmt .Sprintf ("Failed to parse response header variable: %s" , err )}
117+ }
104118
105119 result = api.HTTPRequestResult {
106120 StatusCode : resp .StatusCode ,
@@ -195,6 +209,43 @@ func parseVariables(body []byte, vardefs []api.HTTPRequestResponseVariable, vari
195209 return nil
196210}
197211
212+ func parseHeaderVariables (headers map [string ]string , vardefs []api.HTTPRequestResponseHeaderVariable , variables map [string ]string ) error {
213+ for _ , vardef := range vardefs {
214+ headerValue , ok := findHeaderValue (headers , vardef .Header )
215+ if ! ok {
216+ return fmt .Errorf ("header %q not found" , vardef .Header )
217+ }
218+
219+ value := headerValue
220+ if vardef .Regex != "" {
221+ re , err := regexp .Compile (vardef .Regex )
222+ if err != nil {
223+ return err
224+ }
225+ if re .NumSubexp () != 1 {
226+ return fmt .Errorf ("regex for header variable %q must have exactly one capture group" , vardef .Name )
227+ }
228+ matches := re .FindStringSubmatch (headerValue )
229+ if len (matches ) != 2 {
230+ return fmt .Errorf ("header %q did not match regex" , vardef .Header )
231+ }
232+ value = matches [1 ]
233+ }
234+
235+ variables [vardef .Name ] = value
236+ }
237+ return nil
238+ }
239+
240+ func findHeaderValue (headers map [string ]string , key string ) (string , bool ) {
241+ for actualKey , value := range headers {
242+ if strings .EqualFold (actualKey , key ) {
243+ return value , true
244+ }
245+ }
246+ return "" , false
247+ }
248+
198249func InterpolateVariables (template string , vars map [string ]string ) string {
199250 r := regexp .MustCompile (`\$\{([^}]+)\}` )
200251 return r .ReplaceAllStringFunc (template , func (m string ) string {
0 commit comments