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

Closed
wants to merge 1 commit into from
Closed
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 tests/path_gateway_ipns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestGatewayIPNSPath(t *testing.T) {
Request: Request().
Path("/ipns/{{name}}", ipnsV1V2BrokenValueV1),
Response: Expect().
StatusBetween(500, 599),
StatusMatch("5xx"),
},
{
Name: "GET for /ipns/name with valid V2 and broken V1 signature succeeds",
Expand Down Expand Up @@ -120,7 +120,7 @@ func TestGatewayIPNSPath(t *testing.T) {
Request: Request().
Path("/ipns/{{name}}", ipnsV1V2BrokenSigV2),
Response: Expect().
StatusBetween(500, 599),
StatusMatch("5xx"),
},
}

Expand Down
31 changes: 31 additions & 0 deletions tooling/test/sugar.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"fmt"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
"testing"

"github.com/ipfs/gateway-conformance/tooling"
Expand Down Expand Up @@ -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"

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."

}

// 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))
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.

}

e.StatusCodeFrom_ = from
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.

e.StatusCodeFrom_ = from
e.StatusCodeTo_ = to
Expand Down
Loading