Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions checks/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func parseVariables(body []byte, vardefs []api.HTTPRequestResponseVariable, vari
return fmt.Errorf("invalid response body variable configuration")
}
matches := re.FindStringSubmatch(bodyString)
if len(matches) == 2 && matches[1] != "" {
if len(matches) == 2 {
variables[vardef.Name] = matches[1]
}

Expand Down Expand Up @@ -254,7 +254,7 @@ func parseHeaderVariables(headers map[string]string, vardefs []api.HTTPRequestRe
}

matches := re.FindStringSubmatch(headerValue)
if len(matches) != 2 || matches[1] == "" {
if len(matches) != 2 {
continue
}
value = matches[1]
Expand Down
15 changes: 13 additions & 2 deletions checks/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,10 @@ func TestParseVariablesLeavesMissingValuesUnset(t *testing.T) {
func TestParseVariablesCapturesBodyRegex(t *testing.T) {
variables := map[string]string{}
err := parseVariables(
[]byte(`<a href="/password-reset/abc123">reset</a>`),
[]byte(`<a href="/password-reset/abc123?next=">reset</a>`),
[]api.HTTPRequestResponseVariable{
{Name: "resetToken", BodyRegex: `/password-reset/([a-z0-9]+)`},
{Name: "nextPath", BodyRegex: `next=([^"]*)`},
},
variables,
)
Expand All @@ -217,6 +218,9 @@ func TestParseVariablesCapturesBodyRegex(t *testing.T) {
if variables["resetToken"] != "abc123" {
t.Fatalf("resetToken = %q, want abc123", variables["resetToken"])
}
if nextPath, ok := variables["nextPath"]; !ok || nextPath != "" {
t.Fatalf("nextPath = %q, %t, want empty string, true", nextPath, ok)
}
}

func TestParseVariablesRequiresCaptureSource(t *testing.T) {
Expand All @@ -237,9 +241,13 @@ func TestParseVariablesRequiresCaptureSource(t *testing.T) {
func TestParseHeaderVariablesLeavesMissingValuesUnset(t *testing.T) {
variables := map[string]string{}
err := parseHeaderVariables(
map[string]string{"Set-Cookie": "session_id=abc123; Path=/; HttpOnly"},
map[string]string{
"Set-Cookie": "session_id=abc123; Path=/; HttpOnly",
"Location": "/login?next=",
},
[]api.HTTPRequestResponseHeaderVariable{
{Name: "sessionID", Header: "Set-Cookie", Regex: "session_id=([^;]+)"},
{Name: "nextPath", Header: "Location", Regex: "next=([^&]*)"},
{Name: "missingHeader", Header: "X-Missing"},
{Name: "missingMatch", Header: "Set-Cookie", Regex: "missing=([^;]+)"},
},
Expand All @@ -251,6 +259,9 @@ func TestParseHeaderVariablesLeavesMissingValuesUnset(t *testing.T) {
if variables["sessionID"] != "abc123" {
t.Fatalf("sessionID = %q, want abc123", variables["sessionID"])
}
if nextPath, ok := variables["nextPath"]; !ok || nextPath != "" {
t.Fatalf("nextPath = %q, %t, want empty string, true", nextPath, ok)
}
if _, ok := variables["missingHeader"]; ok {
t.Fatalf("expected missing header variable to remain unset")
}
Expand Down