Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

abhiraj-mengade
Copy link

This PR adds a function StatusMatch to handle status checks for ranges of HTTP status codes.

Aims to resolve the issue #164

@gammazero gammazero requested a review from lidel August 20, 2024 16:42
Copy link
Member

@SgtPooki SgtPooki left a 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")
Copy link
Member

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))
Copy link
Member

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))
Copy link
Member

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 {
Copy link
Member

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 {
Copy link
Member

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?

Copy link
Member

@lidel lidel Sep 3, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants