You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
gock.MatchHeader accepts two strings (key, value), but the underlying function that matches the mock request Headers against the real request headers is regexp.MatchString. (File: matchers.go)
The issue is that regexp.MatchString takes a regex pattern expression and a string.
That causes the problem of matching headers that have parentheses or other special characters.
Example:
// main.goreq, err:=http.NewRequest("GET", "http://example.com", nil)
req.Header.Add("User-Agent", "Agent (version1.0)")
// main_test.gogock.New("http://example.com").
MatchHeader("User-Agent", "Agent (version1.0)"). // Will never match the requestGet("/").
Reply(200).
BodyString("Success")
A way of fixing the issue is that the key and value are escaped first using regexp.QuoteMeta(key) & regexp.QuoteMeta(value), before being passed to the regexp.MatchString function.
for _, field := range req.Header[key] {
escapedValue := regexp.QuoteMeta(value[0]) // Something like this
escapedField := regexp.QuoteMeta(field)
match, err = regexp.MatchString(escapedValue, escapedField)
if err != nil {
return false, err
}
if match {
break
}
}
The text was updated successfully, but these errors were encountered:
gock.MatchHeader
accepts two strings (key, value), but the underlying function that matches the mock request Headers against the real request headers isregexp.MatchString
. (File: matchers.go)The issue is that
regexp.MatchString
takes a regex pattern expression and a string.That causes the problem of matching headers that have parentheses or other special characters.
Example:
A way of fixing the issue is that the key and value are escaped first using
regexp.QuoteMeta(key) & regexp.QuoteMeta(value)
, before being passed to the regexp.MatchString function.matchers.go
The text was updated successfully, but these errors were encountered: