@@ -126,3 +126,103 @@ func TestTruncateAndStringifyBodyCapsBinaryBody(t *testing.T) {
126126 t .Fatalf ("len(truncateAndStringifyBody(binary)) = %d, want %d" , len (got ), 16 * 1024 )
127127 }
128128}
129+
130+ func TestRunHTTPRequestCapturesResponseHeaderVariableAndDoesNotFollowRedirect (t * testing.T ) {
131+ followRedirects := false
132+
133+ server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
134+ if r .URL .Path != "/login" {
135+ t .Errorf ("path = %q, want /login" , r .URL .Path )
136+ return
137+ }
138+
139+ w .Header ().Set ("Set-Cookie" , "session_id=abc123; Path=/; HttpOnly" )
140+ w .Header ().Set ("Location" , "/account" )
141+ w .WriteHeader (http .StatusFound )
142+ _ , _ = w .Write ([]byte ("Found. Redirecting to /account" ))
143+ }))
144+ defer server .Close ()
145+
146+ variables := map [string ]string {}
147+ requestStep := api.CLIStepHTTPRequest {
148+ ResponseHeaderVariables : []api.HTTPRequestResponseHeaderVariable {{
149+ Name : "sessionID" ,
150+ Header : "Set-Cookie" ,
151+ Regex : "session_id=([^;]+)" ,
152+ }},
153+ Request : api.HTTPRequest {
154+ Method : http .MethodPost ,
155+ FullURL : api .BaseURLPlaceholder + "/login" ,
156+ FollowRedirects : & followRedirects ,
157+ BodyForm : map [string ]string {
158+ "email" : "pacifica@example.com" ,
159+ "password" : "password123" ,
160+ "returnTo" : "/account" ,
161+ },
162+ },
163+ }
164+
165+ result := runHTTPRequest (server .Client (), server .URL , variables , requestStep )
166+ if result .Err != "" {
167+ t .Fatalf ("unexpected request error: %s" , result .Err )
168+ }
169+ if result .StatusCode != http .StatusFound {
170+ t .Fatalf ("StatusCode = %d, want %d" , result .StatusCode , http .StatusFound )
171+ }
172+ if result .ResponseHeaders ["Set-Cookie" ] == "" {
173+ t .Fatalf ("expected Set-Cookie response header" )
174+ }
175+ if result .Variables ["sessionID" ] != "abc123" {
176+ t .Fatalf ("captured sessionID = %q, want abc123" , result .Variables ["sessionID" ])
177+ }
178+ }
179+
180+ func TestParseVariablesLeavesMissingValuesUnset (t * testing.T ) {
181+ variables := map [string ]string {}
182+ err := parseVariables (
183+ []byte (`{"token":"abc123","missing":null}` ),
184+ []api.HTTPRequestResponseVariable {
185+ {Name : "token" , Path : ".token" },
186+ {Name : "missing" , Path : ".missing" },
187+ {Name : "notFound" , Path : ".not_found" },
188+ },
189+ variables ,
190+ )
191+ if err != nil {
192+ t .Fatalf ("unexpected parseVariables error: %v" , err )
193+ }
194+ if variables ["token" ] != "abc123" {
195+ t .Fatalf ("token = %q, want abc123" , variables ["token" ])
196+ }
197+ if _ , ok := variables ["missing" ]; ok {
198+ t .Fatalf ("expected null variable to remain unset" )
199+ }
200+ if _ , ok := variables ["notFound" ]; ok {
201+ t .Fatalf ("expected missing variable to remain unset" )
202+ }
203+ }
204+
205+ func TestParseHeaderVariablesLeavesMissingValuesUnset (t * testing.T ) {
206+ variables := map [string ]string {}
207+ err := parseHeaderVariables (
208+ map [string ]string {"Set-Cookie" : "session_id=abc123; Path=/; HttpOnly" },
209+ []api.HTTPRequestResponseHeaderVariable {
210+ {Name : "sessionID" , Header : "Set-Cookie" , Regex : "session_id=([^;]+)" },
211+ {Name : "missingHeader" , Header : "X-Missing" },
212+ {Name : "missingMatch" , Header : "Set-Cookie" , Regex : "missing=([^;]+)" },
213+ },
214+ variables ,
215+ )
216+ if err != nil {
217+ t .Fatalf ("unexpected parseHeaderVariables error: %v" , err )
218+ }
219+ if variables ["sessionID" ] != "abc123" {
220+ t .Fatalf ("sessionID = %q, want abc123" , variables ["sessionID" ])
221+ }
222+ if _ , ok := variables ["missingHeader" ]; ok {
223+ t .Fatalf ("expected missing header variable to remain unset" )
224+ }
225+ if _ , ok := variables ["missingMatch" ]; ok {
226+ t .Fatalf ("expected non-matching header variable to remain unset" )
227+ }
228+ }
0 commit comments