-
-
Notifications
You must be signed in to change notification settings - Fork 998
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
jriddle
committed
Nov 7, 2023
1 parent
5cf9bc3
commit f6c1df8
Showing
2 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
} | ||
} |