2020-06-14 12:37:09 +02:00
|
|
|
// ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
|
2025-10-22 09:47:12 -04:00
|
|
|
// 📝 GitHub Repository: https://github.com/gofiber/fiber
|
2020-06-14 12:37:09 +02:00
|
|
|
// 📌 API Documentation: https://docs.gofiber.io
|
|
|
|
|
|
|
|
|
|
package fiber
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"testing"
|
|
|
|
|
|
2022-08-22 13:57:10 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-06-14 12:37:09 +02:00
|
|
|
)
|
|
|
|
|
|
2020-08-10 09:50:53 +02:00
|
|
|
// go test -race -run Test_Path_parseRoute
|
|
|
|
|
func Test_Path_parseRoute(t *testing.T) {
|
2023-01-15 18:21:37 +03:00
|
|
|
t.Parallel()
|
2020-08-10 09:50:53 +02:00
|
|
|
var rp routeParser
|
|
|
|
|
|
|
|
|
|
rp = parseRoute("/shop/product/::filter/color::color/size::size")
|
2022-08-22 13:57:10 +08:00
|
|
|
require.Equal(t, routeParser{
|
2020-09-13 11:20:11 +02:00
|
|
|
segs: []*routeSegment{
|
|
|
|
|
{Const: "/shop/product/:", Length: 15},
|
2020-08-10 09:50:53 +02:00
|
|
|
{IsParam: true, ParamName: "filter", ComparePart: "/color:", PartCount: 1},
|
2020-09-13 11:20:11 +02:00
|
|
|
{Const: "/color:", Length: 7},
|
2020-08-10 09:50:53 +02:00
|
|
|
{IsParam: true, ParamName: "color", ComparePart: "/size:", PartCount: 1},
|
2020-09-13 11:20:11 +02:00
|
|
|
{Const: "/size:", Length: 6},
|
2020-08-10 09:50:53 +02:00
|
|
|
{IsParam: true, ParamName: "size", IsLast: true},
|
|
|
|
|
},
|
2020-08-10 10:14:17 +02:00
|
|
|
params: []string{"filter", "color", "size"},
|
2020-08-10 09:50:53 +02:00
|
|
|
}, rp)
|
|
|
|
|
|
|
|
|
|
rp = parseRoute("/api/v1/:param/abc/*")
|
2022-08-22 13:57:10 +08:00
|
|
|
require.Equal(t, routeParser{
|
2020-09-13 11:20:11 +02:00
|
|
|
segs: []*routeSegment{
|
|
|
|
|
{Const: "/api/v1/", Length: 8},
|
2020-08-10 09:50:53 +02:00
|
|
|
{IsParam: true, ParamName: "param", ComparePart: "/abc", PartCount: 1},
|
2020-09-13 11:20:11 +02:00
|
|
|
{Const: "/abc/", Length: 5, HasOptionalSlash: true},
|
2020-08-10 09:50:53 +02:00
|
|
|
{IsParam: true, ParamName: "*1", IsGreedy: true, IsOptional: true, IsLast: true},
|
|
|
|
|
},
|
|
|
|
|
params: []string{"param", "*1"},
|
|
|
|
|
wildCardCount: 1,
|
|
|
|
|
}, rp)
|
|
|
|
|
|
2021-04-12 21:45:24 +02:00
|
|
|
rp = parseRoute("/v1/some/resource/name\\:customVerb")
|
2022-08-22 13:57:10 +08:00
|
|
|
require.Equal(t, routeParser{
|
2021-04-12 21:45:24 +02:00
|
|
|
segs: []*routeSegment{
|
|
|
|
|
{Const: "/v1/some/resource/name:customVerb", Length: 33, IsLast: true},
|
|
|
|
|
},
|
|
|
|
|
params: nil,
|
|
|
|
|
}, rp)
|
2022-07-27 08:37:03 +02:00
|
|
|
|
|
|
|
|
rp = parseRoute("/v1/some/resource/:name\\:customVerb")
|
2022-08-22 13:57:10 +08:00
|
|
|
require.Equal(t, routeParser{
|
2022-07-27 08:37:03 +02:00
|
|
|
segs: []*routeSegment{
|
|
|
|
|
{Const: "/v1/some/resource/", Length: 18},
|
|
|
|
|
{IsParam: true, ParamName: "name", ComparePart: ":customVerb", PartCount: 1},
|
|
|
|
|
{Const: ":customVerb", Length: 11, IsLast: true},
|
|
|
|
|
},
|
|
|
|
|
params: []string{"name"},
|
|
|
|
|
}, rp)
|
|
|
|
|
|
2025-06-07 16:34:44 -04:00
|
|
|
// heavy test with escaped characters
|
2021-04-12 21:45:24 +02:00
|
|
|
rp = parseRoute("/v1/some/resource/name\\\\:customVerb?\\?/:param/*")
|
2022-08-22 13:57:10 +08:00
|
|
|
require.Equal(t, routeParser{
|
2021-04-12 21:45:24 +02:00
|
|
|
segs: []*routeSegment{
|
|
|
|
|
{Const: "/v1/some/resource/name:customVerb??/", Length: 36},
|
|
|
|
|
{IsParam: true, ParamName: "param", ComparePart: "/", PartCount: 1},
|
|
|
|
|
{Const: "/", Length: 1, HasOptionalSlash: true},
|
|
|
|
|
{IsParam: true, ParamName: "*1", IsGreedy: true, IsOptional: true, IsLast: true},
|
|
|
|
|
},
|
|
|
|
|
params: []string{"param", "*1"},
|
|
|
|
|
wildCardCount: 1,
|
|
|
|
|
}, rp)
|
|
|
|
|
|
2020-08-10 09:50:53 +02:00
|
|
|
rp = parseRoute("/api/*/:param/:param2")
|
2022-08-22 13:57:10 +08:00
|
|
|
require.Equal(t, routeParser{
|
2020-09-13 11:20:11 +02:00
|
|
|
segs: []*routeSegment{
|
|
|
|
|
{Const: "/api/", Length: 5, HasOptionalSlash: true},
|
2020-08-10 09:50:53 +02:00
|
|
|
{IsParam: true, ParamName: "*1", IsGreedy: true, IsOptional: true, ComparePart: "/", PartCount: 2},
|
2020-09-13 11:20:11 +02:00
|
|
|
{Const: "/", Length: 1},
|
2020-08-10 09:50:53 +02:00
|
|
|
{IsParam: true, ParamName: "param", ComparePart: "/", PartCount: 1},
|
2020-09-13 11:20:11 +02:00
|
|
|
{Const: "/", Length: 1},
|
2020-08-10 09:50:53 +02:00
|
|
|
{IsParam: true, ParamName: "param2", IsLast: true},
|
|
|
|
|
},
|
|
|
|
|
params: []string{"*1", "param", "param2"},
|
|
|
|
|
wildCardCount: 1,
|
|
|
|
|
}, rp)
|
|
|
|
|
|
|
|
|
|
rp = parseRoute("/test:optional?:optional2?")
|
2022-08-22 13:57:10 +08:00
|
|
|
require.Equal(t, routeParser{
|
2020-09-13 11:20:11 +02:00
|
|
|
segs: []*routeSegment{
|
|
|
|
|
{Const: "/test", Length: 5},
|
|
|
|
|
{IsParam: true, ParamName: "optional", IsOptional: true, Length: 1},
|
2020-08-10 09:50:53 +02:00
|
|
|
{IsParam: true, ParamName: "optional2", IsOptional: true, IsLast: true},
|
|
|
|
|
},
|
2020-08-10 10:14:17 +02:00
|
|
|
params: []string{"optional", "optional2"},
|
2020-08-10 09:50:53 +02:00
|
|
|
}, rp)
|
|
|
|
|
|
|
|
|
|
rp = parseRoute("/config/+.json")
|
2022-08-22 13:57:10 +08:00
|
|
|
require.Equal(t, routeParser{
|
2020-09-13 11:20:11 +02:00
|
|
|
segs: []*routeSegment{
|
|
|
|
|
{Const: "/config/", Length: 8},
|
2020-08-10 10:14:17 +02:00
|
|
|
{IsParam: true, ParamName: "+1", IsGreedy: true, IsOptional: false, ComparePart: ".json", PartCount: 1},
|
2020-09-13 11:20:11 +02:00
|
|
|
{Const: ".json", Length: 5, IsLast: true},
|
2020-08-10 09:50:53 +02:00
|
|
|
},
|
2020-08-10 10:14:17 +02:00
|
|
|
params: []string{"+1"},
|
|
|
|
|
plusCount: 1,
|
2020-08-10 09:50:53 +02:00
|
|
|
}, rp)
|
|
|
|
|
|
|
|
|
|
rp = parseRoute("/api/:day.:month?.:year?")
|
2022-08-22 13:57:10 +08:00
|
|
|
require.Equal(t, routeParser{
|
2020-09-13 11:20:11 +02:00
|
|
|
segs: []*routeSegment{
|
|
|
|
|
{Const: "/api/", Length: 5},
|
2020-08-10 09:50:53 +02:00
|
|
|
{IsParam: true, ParamName: "day", IsOptional: false, ComparePart: ".", PartCount: 2},
|
2020-09-13 11:20:11 +02:00
|
|
|
{Const: ".", Length: 1},
|
2020-08-10 09:50:53 +02:00
|
|
|
{IsParam: true, ParamName: "month", IsOptional: true, ComparePart: ".", PartCount: 1},
|
2020-09-13 11:20:11 +02:00
|
|
|
{Const: ".", Length: 1},
|
2020-08-10 09:50:53 +02:00
|
|
|
{IsParam: true, ParamName: "year", IsOptional: true, IsLast: true},
|
|
|
|
|
},
|
2020-08-10 10:14:17 +02:00
|
|
|
params: []string{"day", "month", "year"},
|
|
|
|
|
}, rp)
|
|
|
|
|
|
|
|
|
|
rp = parseRoute("/*v1*/proxy")
|
2022-08-22 13:57:10 +08:00
|
|
|
require.Equal(t, routeParser{
|
2020-09-13 11:20:11 +02:00
|
|
|
segs: []*routeSegment{
|
|
|
|
|
{Const: "/", Length: 1, HasOptionalSlash: true},
|
2020-08-10 10:14:17 +02:00
|
|
|
{IsParam: true, ParamName: "*1", IsGreedy: true, IsOptional: true, ComparePart: "v1", PartCount: 1},
|
2020-09-13 11:20:11 +02:00
|
|
|
{Const: "v1", Length: 2},
|
2020-08-10 10:14:17 +02:00
|
|
|
{IsParam: true, ParamName: "*2", IsGreedy: true, IsOptional: true, ComparePart: "/proxy", PartCount: 1},
|
2020-09-13 11:20:11 +02:00
|
|
|
{Const: "/proxy", Length: 6, IsLast: true},
|
2020-08-10 10:14:17 +02:00
|
|
|
},
|
|
|
|
|
params: []string{"*1", "*2"},
|
|
|
|
|
wildCardCount: 2,
|
2020-08-10 09:50:53 +02:00
|
|
|
}, rp)
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-14 12:37:09 +02:00
|
|
|
// go test -race -run Test_Path_matchParams
|
|
|
|
|
func Test_Path_matchParams(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
2020-09-13 11:20:11 +02:00
|
|
|
var ctxParams [maxParams]string
|
2023-05-17 15:45:38 +02:00
|
|
|
testCaseFn := func(testCollection routeCaseCollection) {
|
|
|
|
|
parser := parseRoute(testCollection.pattern)
|
|
|
|
|
for _, c := range testCollection.testCases {
|
2020-09-13 11:20:11 +02:00
|
|
|
match := parser.getMatch(c.url, c.url, &ctxParams, c.partialCheck)
|
2024-08-14 03:14:04 -04:00
|
|
|
require.Equal(t, c.match, match, "route: '%s', url: '%s'", testCollection.pattern, c.url)
|
2020-09-13 11:20:11 +02:00
|
|
|
if match && len(c.params) > 0 {
|
2024-08-14 03:14:04 -04:00
|
|
|
require.Equal(t, c.params[0:len(c.params)], ctxParams[0:len(c.params)], "route: '%s', url: '%s'", testCollection.pattern, c.url)
|
2020-06-14 12:37:09 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-17 15:45:38 +02:00
|
|
|
for _, testCaseCollection := range routeTestCases {
|
|
|
|
|
testCaseFn(testCaseCollection)
|
|
|
|
|
}
|
2020-06-14 12:37:09 +02:00
|
|
|
}
|
2020-07-09 15:51:04 +08:00
|
|
|
|
2022-12-12 03:37:35 +11:00
|
|
|
// go test -race -run Test_RoutePatternMatch
|
|
|
|
|
func Test_RoutePatternMatch(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
2023-05-17 15:45:38 +02:00
|
|
|
testCaseFn := func(pattern string, cases []routeTestCase) {
|
2022-12-12 03:37:35 +11:00
|
|
|
for _, c := range cases {
|
2023-05-17 15:45:38 +02:00
|
|
|
// skip all cases for partial checks
|
|
|
|
|
if c.partialCheck {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2022-12-12 03:37:35 +11:00
|
|
|
match := RoutePatternMatch(c.url, pattern)
|
2024-08-14 03:14:04 -04:00
|
|
|
require.Equal(t, c.match, match, "route: '%s', url: '%s'", pattern, c.url)
|
2022-12-12 03:37:35 +11:00
|
|
|
}
|
|
|
|
|
}
|
2023-05-17 15:45:38 +02:00
|
|
|
for _, testCase := range routeTestCases {
|
|
|
|
|
testCaseFn(testCase.pattern, testCase.testCases)
|
|
|
|
|
}
|
2022-12-12 03:37:35 +11:00
|
|
|
}
|
|
|
|
|
|
2025-09-22 12:24:01 +02:00
|
|
|
func TestHasPartialMatchBoundary(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
|
name string
|
|
|
|
|
path string
|
|
|
|
|
matchedLength int
|
|
|
|
|
expected bool
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
name: "negative length",
|
|
|
|
|
path: "/demo",
|
|
|
|
|
matchedLength: -1,
|
|
|
|
|
expected: false,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "greater than length",
|
|
|
|
|
path: "/demo",
|
|
|
|
|
matchedLength: 6,
|
|
|
|
|
expected: false,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "exact match",
|
|
|
|
|
path: "/demo",
|
|
|
|
|
matchedLength: len("/demo"),
|
|
|
|
|
expected: true,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "zero length",
|
|
|
|
|
path: "/demo",
|
|
|
|
|
matchedLength: 0,
|
|
|
|
|
expected: false,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "previous rune slash",
|
|
|
|
|
path: "/demo/child",
|
|
|
|
|
matchedLength: len("/demo/"),
|
|
|
|
|
expected: true,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "next rune slash",
|
|
|
|
|
path: "/demo/child",
|
|
|
|
|
matchedLength: len("/demo"),
|
|
|
|
|
expected: true,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "no boundary",
|
|
|
|
|
path: "/demo/child",
|
|
|
|
|
matchedLength: len("/dem"),
|
|
|
|
|
expected: false,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
|
t.Run(testCase.name, func(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
require.Equal(t, testCase.expected, hasPartialMatchBoundary(testCase.path, testCase.matchedLength))
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-14 10:23:22 +02:00
|
|
|
func Test_Utils_GetTrimmedParam(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
2021-10-25 07:25:31 +02:00
|
|
|
res := GetTrimmedParam("")
|
2025-10-20 12:46:58 +05:30
|
|
|
require.Empty(t, res)
|
2021-10-25 07:25:31 +02:00
|
|
|
res = GetTrimmedParam("*")
|
2022-08-22 13:57:10 +08:00
|
|
|
require.Equal(t, "*", res)
|
2020-09-14 10:23:22 +02:00
|
|
|
res = GetTrimmedParam(":param")
|
2022-08-22 13:57:10 +08:00
|
|
|
require.Equal(t, "param", res)
|
2020-09-14 10:23:22 +02:00
|
|
|
res = GetTrimmedParam(":param1?")
|
2022-08-22 13:57:10 +08:00
|
|
|
require.Equal(t, "param1", res)
|
2020-09-14 10:23:22 +02:00
|
|
|
res = GetTrimmedParam("noParam")
|
2022-08-22 13:57:10 +08:00
|
|
|
require.Equal(t, "noParam", res)
|
2020-09-14 10:23:22 +02:00
|
|
|
}
|
|
|
|
|
|
2021-04-12 21:45:24 +02:00
|
|
|
func Test_Utils_RemoveEscapeChar(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
res := RemoveEscapeChar(":test\\:bla")
|
2022-08-22 13:57:10 +08:00
|
|
|
require.Equal(t, ":test:bla", res)
|
2021-04-12 21:45:24 +02:00
|
|
|
res = RemoveEscapeChar("\\abc")
|
2022-08-22 13:57:10 +08:00
|
|
|
require.Equal(t, "abc", res)
|
2021-04-12 21:45:24 +02:00
|
|
|
res = RemoveEscapeChar("noEscapeChar")
|
2022-08-22 13:57:10 +08:00
|
|
|
require.Equal(t, "noEscapeChar", res)
|
2021-04-12 21:45:24 +02:00
|
|
|
}
|
|
|
|
|
|
2023-08-21 10:44:02 +03:00
|
|
|
func Benchmark_Utils_RemoveEscapeChar(b *testing.B) {
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
var res string
|
2025-05-27 13:23:02 +02:00
|
|
|
for b.Loop() {
|
2023-08-21 10:44:02 +03:00
|
|
|
res = RemoveEscapeChar(":test\\:bla")
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-07 20:22:31 +03:00
|
|
|
require.Equal(b, ":test:bla", res)
|
2023-08-21 10:44:02 +03:00
|
|
|
}
|
|
|
|
|
|
2020-08-09 19:50:10 +02:00
|
|
|
// go test -race -run Test_Path_matchParams
|
|
|
|
|
func Benchmark_Path_matchParams(t *testing.B) {
|
2020-09-13 11:20:11 +02:00
|
|
|
var ctxParams [maxParams]string
|
2023-05-17 15:45:38 +02:00
|
|
|
benchCaseFn := func(testCollection routeCaseCollection) {
|
|
|
|
|
parser := parseRoute(testCollection.pattern)
|
|
|
|
|
for _, c := range testCollection.testCases {
|
2020-08-09 19:50:10 +02:00
|
|
|
var matchRes bool
|
2020-09-13 11:20:11 +02:00
|
|
|
state := "match"
|
|
|
|
|
if !c.match {
|
|
|
|
|
state = "not match"
|
|
|
|
|
}
|
2023-05-17 15:45:38 +02:00
|
|
|
t.Run(testCollection.pattern+" | "+state+" | "+c.url, func(b *testing.B) {
|
2025-05-27 13:23:02 +02:00
|
|
|
for b.Loop() {
|
2020-09-13 11:20:11 +02:00
|
|
|
if match := parser.getMatch(c.url, c.url, &ctxParams, c.partialCheck); match {
|
2023-05-17 15:45:38 +02:00
|
|
|
// Get testCases from the original path
|
2020-08-09 19:50:10 +02:00
|
|
|
matchRes = true
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-14 03:14:04 -04:00
|
|
|
require.Equal(t, c.match, matchRes, "route: '%s', url: '%s'", testCollection.pattern, c.url)
|
2020-09-13 11:20:11 +02:00
|
|
|
if matchRes && len(c.params) > 0 {
|
2024-08-14 03:14:04 -04:00
|
|
|
require.Equal(t, c.params[0:len(c.params)-1], ctxParams[0:len(c.params)-1], "route: '%s', url: '%s'", testCollection.pattern, c.url)
|
2020-08-09 19:50:10 +02:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-17 15:45:38 +02:00
|
|
|
|
|
|
|
|
for _, testCollection := range benchmarkCases {
|
|
|
|
|
benchCaseFn(testCollection)
|
|
|
|
|
}
|
2020-07-09 15:51:04 +08:00
|
|
|
}
|
2022-12-12 03:37:35 +11:00
|
|
|
|
|
|
|
|
// go test -race -run Test_RoutePatternMatch
|
|
|
|
|
func Benchmark_RoutePatternMatch(t *testing.B) {
|
2023-05-17 15:45:38 +02:00
|
|
|
benchCaseFn := func(testCollection routeCaseCollection) {
|
|
|
|
|
for _, c := range testCollection.testCases {
|
|
|
|
|
// skip all cases for partial checks
|
|
|
|
|
if c.partialCheck {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2022-12-12 03:37:35 +11:00
|
|
|
var matchRes bool
|
|
|
|
|
state := "match"
|
|
|
|
|
if !c.match {
|
|
|
|
|
state = "not match"
|
|
|
|
|
}
|
2023-05-17 15:45:38 +02:00
|
|
|
t.Run(testCollection.pattern+" | "+state+" | "+c.url, func(b *testing.B) {
|
2025-05-27 13:23:02 +02:00
|
|
|
for b.Loop() {
|
2023-05-17 15:45:38 +02:00
|
|
|
if match := RoutePatternMatch(c.url, testCollection.pattern); match {
|
|
|
|
|
// Get testCases from the original path
|
2022-12-12 03:37:35 +11:00
|
|
|
matchRes = true
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-14 03:14:04 -04:00
|
|
|
require.Equal(t, c.match, matchRes, "route: '%s', url: '%s'", testCollection.pattern, c.url)
|
2022-12-12 03:37:35 +11:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-17 15:45:38 +02:00
|
|
|
|
|
|
|
|
for _, testCollection := range benchmarkCases {
|
|
|
|
|
benchCaseFn(testCollection)
|
|
|
|
|
}
|
2022-12-12 03:37:35 +11:00
|
|
|
}
|
2025-12-25 17:51:12 -04:00
|
|
|
|
|
|
|
|
func Test_Route_TooManyParams_Panic(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
|
|
// Test with exactly maxParams (30) - should work
|
|
|
|
|
t.Run("exactly_maxParams", func(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
route := "/:p1/:p2/:p3/:p4/:p5/:p6/:p7/:p8/:p9/:p10/:p11/:p12/:p13/:p14/:p15/:p16/:p17/:p18/:p19/:p20/:p21/:p22/:p23/:p24/:p25/:p26/:p27/:p28/:p29/:p30"
|
|
|
|
|
require.NotPanics(t, func() {
|
|
|
|
|
parseRoute(route)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Test with maxParams + 1 (31) - should panic
|
|
|
|
|
t.Run("maxParams_plus_one", func(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
route := "/:p1/:p2/:p3/:p4/:p5/:p6/:p7/:p8/:p9/:p10/:p11/:p12/:p13/:p14/:p15/:p16/:p17/:p18/:p19/:p20/:p21/:p22/:p23/:p24/:p25/:p26/:p27/:p28/:p29/:p30/:p31"
|
|
|
|
|
require.PanicsWithValue(t, "Route '"+route+"' has 31 parameters, which exceeds the maximum of 30", func() {
|
|
|
|
|
parseRoute(route)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Test with 35 params - should panic
|
|
|
|
|
t.Run("35_params", func(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
route := "/:p1/:p2/:p3/:p4/:p5/:p6/:p7/:p8/:p9/:p10/:p11/:p12/:p13/:p14/:p15/:p16/:p17/:p18/:p19/:p20/:p21/:p22/:p23/:p24/:p25/:p26/:p27/:p28/:p29/:p30/:p31/:p32/:p33/:p34/:p35"
|
|
|
|
|
require.PanicsWithValue(t, "Route '"+route+"' has 35 parameters, which exceeds the maximum of 30", func() {
|
|
|
|
|
parseRoute(route)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Test_App_Register_TooManyParams_Panic(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
|
|
// Test registering a route with too many params via app
|
|
|
|
|
t.Run("register_via_Get", func(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
app := New()
|
|
|
|
|
route := "/:p1/:p2/:p3/:p4/:p5/:p6/:p7/:p8/:p9/:p10/:p11/:p12/:p13/:p14/:p15/:p16/:p17/:p18/:p19/:p20/:p21/:p22/:p23/:p24/:p25/:p26/:p27/:p28/:p29/:p30/:p31"
|
|
|
|
|
|
|
|
|
|
require.PanicsWithValue(t, "Route '"+route+"' has 31 parameters, which exceeds the maximum of 30", func() {
|
|
|
|
|
app.Get(route, func(c Ctx) error {
|
|
|
|
|
return c.SendString("test")
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Test registering a route with maxParams works
|
|
|
|
|
t.Run("register_maxParams_works", func(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
app := New()
|
|
|
|
|
route := "/:p1/:p2/:p3/:p4/:p5/:p6/:p7/:p8/:p9/:p10/:p11/:p12/:p13/:p14/:p15/:p16/:p17/:p18/:p19/:p20/:p21/:p22/:p23/:p24/:p25/:p26/:p27/:p28/:p29/:p30"
|
|
|
|
|
|
|
|
|
|
require.NotPanics(t, func() {
|
|
|
|
|
app.Get(route, func(c Ctx) error {
|
|
|
|
|
return c.SendString("test")
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|