Skip to content

Commit

Permalink
Add: StatusMatch function for elegantly handingling Status Checks
Browse files Browse the repository at this point in the history
  • Loading branch information
abhiraj-mengade committed Aug 1, 2024
1 parent f1d01d3 commit 3e80496
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
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 {
re := regexp.MustCompile(`^(\d+)(x+)$`)
matches := re.FindStringSubmatch(pattern)
if len(matches) != 3 {
panic("invalid status pattern")
}

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

// Compute the upper bound
to, err := strconv.Atoi(leadingDigits + strings.Repeat("9", numXs))
if err != nil {
panic(fmt.Sprintf("invalid status pattern: %v", err))
}

e.StatusCodeFrom_ = from
e.StatusCodeTo_ = to
return e
}

func (e ExpectBuilder) StatusBetween(from, to int) ExpectBuilder {
e.StatusCodeFrom_ = from
e.StatusCodeTo_ = to
Expand Down

0 comments on commit 3e80496

Please sign in to comment.