Skip to content

Commit

Permalink
Add TestRoutesValidationTraditionalCompatible and make assert robust
Browse files Browse the repository at this point in the history
  • Loading branch information
programmer04 committed Aug 28, 2023
1 parent 286097f commit 001cd2f
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@

## [v0.47.0]

> Release date: 2023/08/28
> Release date: 2023/08/29
- Added method `Validate` to `RouteService`
[#368](https://github.com/Kong/go-kong/pull/368)
Expand Down
76 changes: 74 additions & 2 deletions kong/route_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ func TestRouteWithHeaders(T *testing.T) {
assert.NoError(err)
}

func TestRoutesValidation(T *testing.T) {
func TestRoutesValidationExpressions(T *testing.T) {
RunWhenKong(T, ">=3.0.0")
SkipWhenKongRouterFlavor(T, Traditional, TraditionalCompatible)

Expand Down Expand Up @@ -354,7 +354,79 @@ func TestRoutesValidation(T *testing.T) {
ok, msg, err := client.Routes.Validate(defaultCtx, tC.route)
require.NoError(err)
require.Equal(tC.valid, ok)
require.True(strings.HasPrefix(msg, tC.msgStartWith))
if !ok {
require.NotEmpty(tC.msgStartWith)
require.True(strings.HasPrefix(msg, tC.msgStartWith))
}
})
}
}

func TestRoutesValidationTraditionalCompatible(T *testing.T) {
RunWhenKong(T, ">=3.0.0")
SkipWhenKongRouterFlavor(T, Traditional, Expressions)

require := require.New(T)

client, err := NewTestClient(nil, nil)
require.NoError(err)
require.NotNil(client)

var (
validPath = String("/prefix/")
validRegex = String("~/payment/(docs|health)$")
invalidRegex = String("~/payment/(docs|health))")
)
for _, tC := range []struct {
name string
route *Route
valid bool
msgStartWith string
}{
{
name: "valid path - prefix",
route: &Route{
Paths: []*string{validPath},
},
valid: true,
},
{
name: "valid path - regex",
route: &Route{
Paths: []*string{validRegex},
},
valid: true,
},
{
name: "multiple valid paths - prefix and regex",
route: &Route{
Paths: []*string{validPath, validRegex},
},
valid: true,
},
{
name: "invalid path - invalid regex (unmatched parentheses)",
route: &Route{
Paths: []*string{invalidRegex},
},
msgStartWith: "schema violation (paths.1: invalid regex:",
},
{
name: "multiple paths - one path with invalid regex",
route: &Route{
Paths: []*string{validPath, invalidRegex, String("/foo")},
},
msgStartWith: "schema violation (paths.2: invalid regex:",
},
} {
T.Run(tC.name, func(t *testing.T) {
ok, msg, err := client.Routes.Validate(defaultCtx, tC.route)
require.NoError(err)
require.Equal(tC.valid, ok)
if !ok {
require.NotEmpty(tC.msgStartWith)
require.True(strings.HasPrefix(msg, tC.msgStartWith))
}
})
}
}

0 comments on commit 001cd2f

Please sign in to comment.