(t *testing.T)
| 740 | } |
| 741 | |
| 742 | func TestGroup_RouteNotFoundWithMiddleware(t *testing.T) { |
| 743 | var testCases = []struct { |
| 744 | expectBody any |
| 745 | name string |
| 746 | whenURL string |
| 747 | expectCode int |
| 748 | givenCustom404 bool |
| 749 | expectMiddlewareCalled bool |
| 750 | }{ |
| 751 | { |
| 752 | name: "ok, custom 404 handler is called with middleware", |
| 753 | givenCustom404: true, |
| 754 | whenURL: "/group/test3", |
| 755 | expectBody: "404 GET /group/*", |
| 756 | expectCode: http.StatusNotFound, |
| 757 | expectMiddlewareCalled: true, // because RouteNotFound is added after middleware is added |
| 758 | }, |
| 759 | { |
| 760 | name: "ok, default group 404 handler is not called with middleware", |
| 761 | givenCustom404: false, |
| 762 | whenURL: "/group/test3", |
| 763 | expectBody: "404 GET /*", |
| 764 | expectCode: http.StatusNotFound, |
| 765 | expectMiddlewareCalled: false, // because RouteNotFound is added before middleware is added |
| 766 | }, |
| 767 | { |
| 768 | name: "ok, (no slash) default group 404 handler is called with middleware", |
| 769 | givenCustom404: false, |
| 770 | whenURL: "/group", |
| 771 | expectBody: "404 GET /*", |
| 772 | expectCode: http.StatusNotFound, |
| 773 | expectMiddlewareCalled: false, // because RouteNotFound is added before middleware is added |
| 774 | }, |
| 775 | } |
| 776 | for _, tc := range testCases { |
| 777 | t.Run(tc.name, func(t *testing.T) { |
| 778 | |
| 779 | okHandler := func(c *Context) error { |
| 780 | return c.String(http.StatusOK, c.Request().Method+" "+c.Path()) |
| 781 | } |
| 782 | notFoundHandler := func(c *Context) error { |
| 783 | return c.String(http.StatusNotFound, "404 "+c.Request().Method+" "+c.Path()) |
| 784 | } |
| 785 | |
| 786 | e := New() |
| 787 | e.GET("/test1", okHandler) |
| 788 | e.RouteNotFound("/*", notFoundHandler) |
| 789 | |
| 790 | g := e.Group("/group") |
| 791 | g.GET("/test1", okHandler) |
| 792 | |
| 793 | middlewareCalled := false |
| 794 | g.Use(func(next HandlerFunc) HandlerFunc { |
| 795 | return func(c *Context) error { |
| 796 | middlewareCalled = true |
| 797 | return next(c) |
| 798 | } |
| 799 | }) |
nothing calls this directly
no test coverage detected
searching dependent graphs…