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

Function TestStripSlashesInRoute

middleware/strip_test.go:56–100  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

54}
55
56func TestStripSlashesInRoute(t *testing.T) {
57 r := chi.NewRouter()
58
59 r.NotFound(func(w http.ResponseWriter, r *http.Request) {
60 w.WriteHeader(404)
61 w.Write([]byte("nothing here"))
62 })
63
64 r.Get("/hi", func(w http.ResponseWriter, r *http.Request) {
65 w.Write([]byte("hi"))
66 })
67
68 r.Route("/accounts/{accountID}", func(r chi.Router) {
69 r.Use(StripSlashes)
70 r.Get("/", func(w http.ResponseWriter, r *http.Request) {
71 w.Write([]byte("accounts index"))
72 })
73 r.Get("/query", func(w http.ResponseWriter, r *http.Request) {
74 accountID := chi.URLParam(r, "accountID")
75 w.Write([]byte(accountID))
76 })
77 })
78
79 ts := httptest.NewServer(r)
80 defer ts.Close()
81
82 if _, resp := testRequest(t, ts, "GET", "/hi", nil); resp != "hi" {
83 t.Fatal(resp)
84 }
85 if _, resp := testRequest(t, ts, "GET", "/hi/", nil); resp != "nothing here" {
86 t.Fatal(resp)
87 }
88 if _, resp := testRequest(t, ts, "GET", "/accounts/admin", nil); resp != "accounts index" {
89 t.Fatal(resp)
90 }
91 if _, resp := testRequest(t, ts, "GET", "/accounts/admin/", nil); resp != "accounts index" {
92 t.Fatal(resp)
93 }
94 if _, resp := testRequest(t, ts, "GET", "/accounts/admin/query", nil); resp != "admin" {
95 t.Fatal(resp)
96 }
97 if _, resp := testRequest(t, ts, "GET", "/accounts/admin/query/", nil); resp != "admin" {
98 t.Fatal(resp)
99 }
100}
101
102func TestRedirectSlashes(t *testing.T) {
103 r := chi.NewRouter()

Callers

nothing calls this directly

Calls 9

NotFoundMethod · 0.95
GetMethod · 0.95
RouteMethod · 0.95
UseMethod · 0.95
URLParamMethod · 0.80
CloseMethod · 0.80
testRequestFunction · 0.70
WriteMethod · 0.65
WriteHeaderMethod · 0.45

Tested by

no test coverage detected