-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add: StatusMatch function for elegantly handingling Status Checks #220
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should also add some tests for this function to ensure it works as expected.
re := regexp.MustCompile(`^(\d+)(x+)$`) | ||
matches := re.FindStringSubmatch(pattern) | ||
if len(matches) != 3 { | ||
panic("invalid status pattern") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not a gopher here. Will this be an error when building or when the test runs?
If the former, a panic is appropriate. If the latter, we should not panic.
Either way, can we be more explicit with the error message to indicate the action to be taken? For example: "The StatusMatch pattern must be a string representing a three-digit status code. Either 5xx, 50x, 502, etc."
// Compute the lower bound | ||
from, err := strconv.Atoi(leadingDigits + strings.Repeat("0", numXs)) | ||
if err != nil { | ||
panic(fmt.Sprintf("invalid status pattern: %v", err)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto on panic and more explicit error message.
// Compute the upper bound | ||
to, err := strconv.Atoi(leadingDigits + strings.Repeat("9", numXs)) | ||
if err != nil { | ||
panic(fmt.Sprintf("invalid status pattern: %v", err)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto on panic and more explicit error message.
@@ -162,6 +165,34 @@ func (e ExpectBuilder) Status(statusCode int) ExpectBuilder { | |||
return e | |||
} | |||
|
|||
func (e ExpectBuilder) StatusMatch(pattern string) ExpectBuilder { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Maybe we should go with Status
since it's BDD style testing expectation language. "expect status X" instead of "expect StatusMatch X"
e.StatusCodeTo_ = to | ||
return e | ||
} | ||
|
||
func (e ExpectBuilder) StatusBetween(from, to int) ExpectBuilder { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we using this function elsewhere? @lidel Are there any cases where we would want to do 501-503
or other?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so, in practice we care about specific code(s), or 4XX or 5XX.
This PR adds a function
StatusMatch
to handle status checks for ranges of HTTP status codes.Aims to resolve the issue #164