From 971f3729520176a408b28de36d6f283f13b8e6d3 Mon Sep 17 00:00:00 2001 From: nbari Date: Sun, 31 Mar 2019 21:49:14 +0200 Subject: [PATCH] re-implement ^%s$ in dynamic routes https://github.com/the-benchmarker/web-frameworks/pull/1147#issuecomment-478371934 --- .circleci/config.yml | 5 +++++ .travis.yml | 1 + dynamic.go | 6 ++++++ dynamic_test.go | 2 +- violetear_test.go | 29 +++++++++++++++++++++++++++++ 5 files changed, 42 insertions(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 11419c4..b1620ed 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -5,6 +5,7 @@ workflows: jobs: - test-latest - test-1.11 + - test-1.12 - test-1.10 - test-1.9 - test-1.8 @@ -17,6 +18,10 @@ jobs: steps: - checkout - run: make test + test-1.12: + <<: *test-template + docker: + - image: circleci/golang:1.12 test-1.11: <<: *test-template docker: diff --git a/.travis.yml b/.travis.yml index 757399e..b15d45b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,6 +9,7 @@ go: - "1.9" - "1.10" - "1.11" + - "1.12" - master before_install: diff --git a/dynamic.go b/dynamic.go index 64f36b1..ffdb51d 100644 --- a/dynamic.go +++ b/dynamic.go @@ -2,6 +2,7 @@ package violetear import ( "errors" + "fmt" "regexp" "strings" ) @@ -13,6 +14,11 @@ func (d dynamicSet) Set(name, regex string) error { return errors.New("dynamic route name must start with a colon ':'") } + // fix regex + if !strings.HasPrefix(regex, "^") { + regex = fmt.Sprintf("^%s$", regex) + } + r := regexp.MustCompile(regex) d[name] = r diff --git a/dynamic_test.go b/dynamic_test.go index 52027ac..b0162f5 100644 --- a/dynamic_test.go +++ b/dynamic_test.go @@ -38,5 +38,5 @@ func TestFixRegex(t *testing.T) { s := make(dynamicSet) s.Set(":name", "az") rx := s[":name"] - expect(t, rx.String(), "az") + expect(t, rx.String(), "^az$") } diff --git a/violetear_test.go b/violetear_test.go index baa3899..2efb5fd 100644 --- a/violetear_test.go +++ b/violetear_test.go @@ -60,6 +60,7 @@ type testDynamicRoutes struct { var dynamicRoutes = []testDynamicRoutes{ {":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}$`}, {":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]?)$`}, + {":id", `\d+`}, } var routes = []testRouter{ @@ -461,6 +462,34 @@ func TestContextNamedParams(t *testing.T) { expect(t, w.Code, 200) } +func TestContextNamedParamsFixRegex(t *testing.T) { + router := New() + + for _, v := range dynamicRoutes { + router.AddRegex(v.name, v.regex) + } + + handler := func(w http.ResponseWriter, r *http.Request) { + params := r.Context().Value(ParamsKey).(Params) + if r.Method == "GET" { + expect(t, params[":id"], "123") + } + w.Write([]byte("fix regex ^...$")) + } + + router.HandleFunc("/test/:id", handler, "GET") + + w := httptest.NewRecorder() + req, _ := http.NewRequest("GET", "/test/foo123bar", nil) + router.ServeHTTP(w, req) + expect(t, w.Code, 404) + + w = httptest.NewRecorder() + req, _ = http.NewRequest("GET", "/test/123", nil) + router.ServeHTTP(w, req) + expect(t, w.Code, 200) +} + type contextKey string func (c contextKey) String() string {