@@ -640,6 +640,81 @@ func TestMuxWith(t *testing.T) {
640
640
}
641
641
}
642
642
643
+ func TestMuxHandlePatternValidation (t * testing.T ) {
644
+ testCases := []struct {
645
+ name string
646
+ pattern string
647
+ shouldPanic bool
648
+ method string // Method to be used for the test request
649
+ path string // Path to be used for the test request
650
+ expectedBody string // Expected response body
651
+ expectedStatus int // Expected HTTP status code
652
+ }{
653
+ // Valid patterns
654
+ {
655
+ name : "Valid pattern without HTTP GET" ,
656
+ pattern : "/user/{id}" ,
657
+ shouldPanic : false ,
658
+ method : "GET" ,
659
+ path : "/user/123" ,
660
+ expectedBody : "without-prefix GET" ,
661
+ expectedStatus : http .StatusOK ,
662
+ },
663
+ {
664
+ name : "Valid pattern with HTTP POST" ,
665
+ pattern : "POST /products/{id}" ,
666
+ shouldPanic : false ,
667
+ method : "POST" ,
668
+ path : "/products/456" ,
669
+ expectedBody : "with-prefix POST" ,
670
+ expectedStatus : http .StatusOK ,
671
+ },
672
+ // Invalid patterns
673
+ {
674
+ name : "Invalid pattern with no method" ,
675
+ pattern : "INVALID/user/{id}" ,
676
+ shouldPanic : true ,
677
+ },
678
+ {
679
+ name : "Invalid pattern with supported method" ,
680
+ pattern : "GET/user/{id}" ,
681
+ shouldPanic : true ,
682
+ },
683
+ {
684
+ name : "Invalid pattern with unsupported method" ,
685
+ pattern : "UNSUPPORTED /unsupported-method" ,
686
+ shouldPanic : true ,
687
+ },
688
+ }
689
+
690
+ for _ , tc := range testCases {
691
+ t .Run (tc .name , func (t * testing.T ) {
692
+ defer func () {
693
+ if r := recover (); r != nil && ! tc .shouldPanic {
694
+ t .Errorf ("Unexpected panic for pattern %s" , tc .pattern )
695
+ }
696
+ }()
697
+
698
+ r := NewRouter ()
699
+ r .Handle (tc .pattern , http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
700
+ w .Write ([]byte (tc .expectedBody ))
701
+ }))
702
+
703
+ if ! tc .shouldPanic {
704
+ // Use testRequest for valid patterns
705
+ ts := httptest .NewServer (r )
706
+ defer ts .Close ()
707
+
708
+ resp , body := testRequest (t , ts , tc .method , tc .path , nil )
709
+ if body != tc .expectedBody || resp .StatusCode != tc .expectedStatus {
710
+ t .Errorf ("Expected status %d and body %s; got status %d and body %s for pattern %s" ,
711
+ tc .expectedStatus , tc .expectedBody , resp .StatusCode , body , tc .pattern )
712
+ }
713
+ }
714
+ })
715
+ }
716
+ }
717
+
643
718
func TestRouterFromMuxWith (t * testing.T ) {
644
719
t .Parallel ()
645
720
0 commit comments