-
Notifications
You must be signed in to change notification settings - Fork 15
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,9 @@ import ( | |
"fmt" | ||
"net/http" | ||
"net/url" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/ipfs/gateway-conformance/tooling" | ||
|
@@ -162,6 +165,34 @@ func (e ExpectBuilder) Status(statusCode int) ExpectBuilder { | |
return e | ||
} | ||
|
||
func (e ExpectBuilder) StatusMatch(pattern string) ExpectBuilder { | ||
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 commentThe 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." |
||
} | ||
|
||
// Extract the leading digits and the number of 'x' characters | ||
leadingDigits := matches[1] | ||
numXs := len(matches[2]) | ||
|
||
// 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. ditto on panic and more explicit error message. |
||
} | ||
|
||
e.StatusCodeFrom_ = from | ||
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 commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
e.StatusCodeFrom_ = from | ||
e.StatusCodeTo_ = to | ||
|
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"