Skip to content
This repository was archived by the owner on Dec 17, 2025. It is now read-only.

Commit 971f372

Browse files
committed
re-implement ^%s$ in dynamic routes the-benchmarker/web-frameworks#1147 (comment)
1 parent 9b54dc8 commit 971f372

File tree

5 files changed

+42
-1
lines changed

5 files changed

+42
-1
lines changed

.circleci/config.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ workflows:
55
jobs:
66
- test-latest
77
- test-1.11
8+
- test-1.12
89
- test-1.10
910
- test-1.9
1011
- test-1.8
@@ -17,6 +18,10 @@ jobs:
1718
steps:
1819
- checkout
1920
- run: make test
21+
test-1.12:
22+
<<: *test-template
23+
docker:
24+
- image: circleci/golang:1.12
2025
test-1.11:
2126
<<: *test-template
2227
docker:

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ go:
99
- "1.9"
1010
- "1.10"
1111
- "1.11"
12+
- "1.12"
1213
- master
1314

1415
before_install:

dynamic.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package violetear
22

33
import (
44
"errors"
5+
"fmt"
56
"regexp"
67
"strings"
78
)
@@ -13,6 +14,11 @@ func (d dynamicSet) Set(name, regex string) error {
1314
return errors.New("dynamic route name must start with a colon ':'")
1415
}
1516

17+
// fix regex
18+
if !strings.HasPrefix(regex, "^") {
19+
regex = fmt.Sprintf("^%s$", regex)
20+
}
21+
1622
r := regexp.MustCompile(regex)
1723
d[name] = r
1824

dynamic_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,5 @@ func TestFixRegex(t *testing.T) {
3838
s := make(dynamicSet)
3939
s.Set(":name", "az")
4040
rx := s[":name"]
41-
expect(t, rx.String(), "az")
41+
expect(t, rx.String(), "^az$")
4242
}

violetear_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ type testDynamicRoutes struct {
6060
var dynamicRoutes = []testDynamicRoutes{
6161
{":uuid", `^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$`},
6262
{":ip", `^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$`},
63+
{":id", `\d+`},
6364
}
6465

6566
var routes = []testRouter{
@@ -461,6 +462,34 @@ func TestContextNamedParams(t *testing.T) {
461462
expect(t, w.Code, 200)
462463
}
463464

465+
func TestContextNamedParamsFixRegex(t *testing.T) {
466+
router := New()
467+
468+
for _, v := range dynamicRoutes {
469+
router.AddRegex(v.name, v.regex)
470+
}
471+
472+
handler := func(w http.ResponseWriter, r *http.Request) {
473+
params := r.Context().Value(ParamsKey).(Params)
474+
if r.Method == "GET" {
475+
expect(t, params[":id"], "123")
476+
}
477+
w.Write([]byte("fix regex ^...$"))
478+
}
479+
480+
router.HandleFunc("/test/:id", handler, "GET")
481+
482+
w := httptest.NewRecorder()
483+
req, _ := http.NewRequest("GET", "/test/foo123bar", nil)
484+
router.ServeHTTP(w, req)
485+
expect(t, w.Code, 404)
486+
487+
w = httptest.NewRecorder()
488+
req, _ = http.NewRequest("GET", "/test/123", nil)
489+
router.ServeHTTP(w, req)
490+
expect(t, w.Code, 200)
491+
}
492+
464493
type contextKey string
465494

466495
func (c contextKey) String() string {

0 commit comments

Comments
 (0)