@@ -3,6 +3,7 @@ package restful
33import  (
44	"io" 
55	"net/http" 
6+ 	"sync" 
67	"testing" 
78)
89
@@ -262,3 +263,49 @@ func TestCurly_ISSUE_137_2(t *testing.T) {
262263}
263264
264265func  curlyDummy (req  * Request , resp  * Response ) { io .WriteString (resp .ResponseWriter , "curlyDummy" ) }
266+ 
267+ func  TestRegexCaching (t  * testing.T ) {
268+ 	// Clear cache before test 
269+ 	regexCache  =  sync.Map {}
270+ 	
271+ 	router  :=  CurlyRouter {}
272+ 	
273+ 	// Test with regex pattern 
274+ 	routeToken  :=  "{id:[0-9]+}" 
275+ 	requestToken  :=  "123" 
276+ 	
277+ 	// First call should cache the regex 
278+ 	matches1 , _  :=  router .regularMatchesPathToken (routeToken , 3 , requestToken )
279+ 	if  ! matches1  {
280+ 		t .Error ("Expected first call to match" )
281+ 	}
282+ 	
283+ 	// Verify cache contains the pattern 
284+ 	pattern  :=  "[0-9]+" 
285+ 	_ , found  :=  regexCache .Load (pattern )
286+ 	if  ! found  {
287+ 		t .Error ("Expected pattern to be cached" )
288+ 	}
289+ 	
290+ 	// Second call should use cached regex 
291+ 	matches2 , _  :=  router .regularMatchesPathToken (routeToken , 3 , requestToken )
292+ 	if  ! matches2  {
293+ 		t .Error ("Expected second call to match using cache" )
294+ 	}
295+ 	
296+ 	// Test with different pattern to ensure separate caching 
297+ 	routeToken2  :=  "{name:[a-z]+}" 
298+ 	requestToken2  :=  "john" 
299+ 	
300+ 	matches3 , _  :=  router .regularMatchesPathToken (routeToken2 , 5 , requestToken2 )
301+ 	if  ! matches3  {
302+ 		t .Error ("Expected name pattern to match" )
303+ 	}
304+ 	
305+ 	// Verify both patterns are cached 
306+ 	pattern2  :=  "[a-z]+" 
307+ 	_ , found2  :=  regexCache .Load (pattern2 )
308+ 	if  ! found2  {
309+ 		t .Error ("Expected name pattern to be cached" )
310+ 	}
311+ }
0 commit comments