Skip to content

Commit

Permalink
And find_pattern middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
jriddle committed Nov 7, 2023
1 parent 5cf9bc3 commit f6c1df8
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
29 changes: 29 additions & 0 deletions middleware/find_pattern.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package middleware

import (
"net/http"

"github.com/go-chi/chi/v5"
)

// Find the route pattern for the request path.
//
// This middleware does not need to be the last middleware to resolve the
// route pattern. The pattern is fully resolved before the request has been
// handled.
func FindPattern(routes chi.Routes, callback func(pattern string)) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
// Find mutates the context so always make a new one
rctx := chi.NewRouteContext()
path := r.URL.Path
op := r.Method
pattern := routes.Find(rctx, op, path)
callback(pattern)

next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}

}
67 changes: 67 additions & 0 deletions middleware/find_pattern_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package middleware

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/go-chi/chi/v5"
)

func TestFindPattern(t *testing.T) {
t.Parallel()

var tests = []struct {
pattern string
path string
}{
{
"/",
"/",
},
{
"/hi",
"/hi",
},
{
"/{id}",
"/123",
},
{
"/{id}/hello",
"/123/hello",
},
{
"/users/*",
"/users/123",
},
{
"/users/*",
"/users/123/hello",
},
}

for _, tt := range tests {
var tt = tt
t.Run(tt.pattern, func(t *testing.T) {
t.Parallel()

recorder := httptest.NewRecorder()

r := chi.NewRouter()
r.Use(FindPattern(r, func(pattern string) {
if pattern != tt.pattern {
t.Errorf("actual pattern \"%s\" does not equal expected pattern \"%s\"", pattern, tt.pattern)
}
}))

r.Get(tt.pattern, func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(""))
})

req := httptest.NewRequest("GET", tt.path, nil)
r.ServeHTTP(recorder, req)
recorder.Result()
})
}
}

0 comments on commit f6c1df8

Please sign in to comment.