| 818 | } |
| 819 | |
| 820 | func Test_App_Use_Params(t *testing.T) { |
| 821 | t.Parallel() |
| 822 | app := New() |
| 823 | |
| 824 | app.Use("/prefix/:param", func(c Ctx) error { |
| 825 | require.Equal(t, "john", c.Params("param")) |
| 826 | return nil |
| 827 | }) |
| 828 | |
| 829 | app.Use("/foo/:bar?", func(c Ctx) error { |
| 830 | require.Equal(t, "foobar", c.Params("bar", "foobar")) |
| 831 | return nil |
| 832 | }) |
| 833 | |
| 834 | app.Use("/:param/*", func(c Ctx) error { |
| 835 | require.Equal(t, "john", c.Params("param")) |
| 836 | require.Equal(t, "doe", c.Params("*")) |
| 837 | return nil |
| 838 | }) |
| 839 | |
| 840 | resp, err := app.Test(httptest.NewRequest(MethodGet, "/prefix/john", http.NoBody)) |
| 841 | require.NoError(t, err, "app.Test(req)") |
| 842 | require.Equal(t, 200, resp.StatusCode, "Status code") |
| 843 | |
| 844 | resp, err = app.Test(httptest.NewRequest(MethodGet, "/john/doe", http.NoBody)) |
| 845 | require.NoError(t, err, "app.Test(req)") |
| 846 | require.Equal(t, 200, resp.StatusCode, "Status code") |
| 847 | |
| 848 | resp, err = app.Test(httptest.NewRequest(MethodGet, "/foo", http.NoBody)) |
| 849 | require.NoError(t, err, "app.Test(req)") |
| 850 | require.Equal(t, 200, resp.StatusCode, "Status code") |
| 851 | |
| 852 | require.PanicsWithValue(t, "use: invalid handler func()\n", func() { |
| 853 | app.Use("/:param/*", func() { |
| 854 | // this should panic |
| 855 | }) |
| 856 | }) |
| 857 | } |
| 858 | |
| 859 | func Test_App_Use_UnescapedPath(t *testing.T) { |
| 860 | t.Parallel() |