MCPcopy
hub / github.com/go-chi/chi / TestMuxFind

Function TestMuxFind

mux_test.go:1871–1953  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1869}
1870
1871func TestMuxFind(t *testing.T) {
1872 r := NewRouter()
1873 r.Get("/", func(w http.ResponseWriter, r *http.Request) {
1874 w.Header().Set("X-Test", "yes")
1875 w.Write([]byte(""))
1876 })
1877 r.Get("/hi", func(w http.ResponseWriter, r *http.Request) {
1878 w.Header().Set("X-Test", "yes")
1879 w.Write([]byte("bye"))
1880 })
1881 r.Route("/yo", func(r Router) {
1882 r.Get("/sup", func(w http.ResponseWriter, r *http.Request) {
1883 w.Write([]byte("sup"))
1884 })
1885 })
1886 r.Route("/articles", func(r Router) {
1887 r.Get("/{id}", func(w http.ResponseWriter, r *http.Request) {
1888 id := URLParam(r, "id")
1889 w.Header().Set("X-Article", id)
1890 w.Write([]byte("article:" + id))
1891 })
1892 })
1893 r.Route("/users", func(r Router) {
1894 r.Head("/{id}", func(w http.ResponseWriter, r *http.Request) {
1895 w.Header().Set("X-User", "-")
1896 w.Write([]byte("user"))
1897 })
1898 r.Get("/{id}", func(w http.ResponseWriter, r *http.Request) {
1899 id := URLParam(r, "id")
1900 w.Header().Set("X-User", id)
1901 w.Write([]byte("user:" + id))
1902 })
1903 })
1904 r.Route("/api", func(r Router) {
1905 r.Route("/groups", func(r Router) {
1906 r.Route("/v2", func(r Router) {
1907 r.Get("/", func(w http.ResponseWriter, r *http.Request) {
1908 w.Write([]byte("groups"))
1909 })
1910 r.Post("/{id}", func(w http.ResponseWriter, r *http.Request) {
1911 w.Write([]byte("POST groups"))
1912 })
1913 })
1914 })
1915 })
1916
1917 tctx := NewRouteContext()
1918
1919 tctx.Reset()
1920 if r.Find(tctx, "GET", "") == "/" {
1921 t.Fatal("expecting to find pattern / for route: GET")
1922 }
1923
1924 tctx.Reset()
1925 if r.Find(tctx, "GET", "/nope") != "" {
1926 t.Fatal("not expecting to find pattern for route: GET /nope")
1927 }
1928

Callers

nothing calls this directly

Calls 10

GetMethod · 0.95
RouteMethod · 0.95
HeadMethod · 0.95
PostMethod · 0.95
NewRouterFunction · 0.85
URLParamFunction · 0.85
NewRouteContextFunction · 0.85
WriteMethod · 0.65
ResetMethod · 0.65
FindMethod · 0.65

Tested by

no test coverage detected