Skip to content

Commit 2c8ffe1

Browse files
tolerate empty matches for response vars
1 parent 55ee118 commit 2c8ffe1

2 files changed

Lines changed: 15 additions & 4 deletions

File tree

checks/http.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ func parseVariables(body []byte, vardefs []api.HTTPRequestResponseVariable, vari
215215
return fmt.Errorf("invalid response body variable configuration")
216216
}
217217
matches := re.FindStringSubmatch(bodyString)
218-
if len(matches) == 2 && matches[1] != "" {
218+
if len(matches) == 2 {
219219
variables[vardef.Name] = matches[1]
220220
}
221221

@@ -254,7 +254,7 @@ func parseHeaderVariables(headers map[string]string, vardefs []api.HTTPRequestRe
254254
}
255255

256256
matches := re.FindStringSubmatch(headerValue)
257-
if len(matches) != 2 || matches[1] == "" {
257+
if len(matches) != 2 {
258258
continue
259259
}
260260
value = matches[1]

checks/http_test.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,10 @@ func TestParseVariablesLeavesMissingValuesUnset(t *testing.T) {
205205
func TestParseVariablesCapturesBodyRegex(t *testing.T) {
206206
variables := map[string]string{}
207207
err := parseVariables(
208-
[]byte(`<a href="/password-reset/abc123">reset</a>`),
208+
[]byte(`<a href="/password-reset/abc123?next=">reset</a>`),
209209
[]api.HTTPRequestResponseVariable{
210210
{Name: "resetToken", BodyRegex: `/password-reset/([a-z0-9]+)`},
211+
{Name: "nextPath", BodyRegex: `next=([^"]*)`},
211212
},
212213
variables,
213214
)
@@ -217,6 +218,9 @@ func TestParseVariablesCapturesBodyRegex(t *testing.T) {
217218
if variables["resetToken"] != "abc123" {
218219
t.Fatalf("resetToken = %q, want abc123", variables["resetToken"])
219220
}
221+
if nextPath, ok := variables["nextPath"]; !ok || nextPath != "" {
222+
t.Fatalf("nextPath = %q, %t, want empty string, true", nextPath, ok)
223+
}
220224
}
221225

222226
func TestParseVariablesRequiresCaptureSource(t *testing.T) {
@@ -237,9 +241,13 @@ func TestParseVariablesRequiresCaptureSource(t *testing.T) {
237241
func TestParseHeaderVariablesLeavesMissingValuesUnset(t *testing.T) {
238242
variables := map[string]string{}
239243
err := parseHeaderVariables(
240-
map[string]string{"Set-Cookie": "session_id=abc123; Path=/; HttpOnly"},
244+
map[string]string{
245+
"Set-Cookie": "session_id=abc123; Path=/; HttpOnly",
246+
"Location": "/login?next=",
247+
},
241248
[]api.HTTPRequestResponseHeaderVariable{
242249
{Name: "sessionID", Header: "Set-Cookie", Regex: "session_id=([^;]+)"},
250+
{Name: "nextPath", Header: "Location", Regex: "next=([^&]*)"},
243251
{Name: "missingHeader", Header: "X-Missing"},
244252
{Name: "missingMatch", Header: "Set-Cookie", Regex: "missing=([^;]+)"},
245253
},
@@ -251,6 +259,9 @@ func TestParseHeaderVariablesLeavesMissingValuesUnset(t *testing.T) {
251259
if variables["sessionID"] != "abc123" {
252260
t.Fatalf("sessionID = %q, want abc123", variables["sessionID"])
253261
}
262+
if nextPath, ok := variables["nextPath"]; !ok || nextPath != "" {
263+
t.Fatalf("nextPath = %q, %t, want empty string, true", nextPath, ok)
264+
}
254265
if _, ok := variables["missingHeader"]; ok {
255266
t.Fatalf("expected missing header variable to remain unset")
256267
}

0 commit comments

Comments
 (0)