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

Function TestStripSlashesWithNilContext

middleware/strip_test.go:203–239  ·  view source on GitHub ↗

This tests a http.Handler that is not chi.Router In these cases, the routeContext is nil

(t *testing.T)

Source from the content-addressed store, hash-verified

201// This tests a http.Handler that is not chi.Router
202// In these cases, the routeContext is nil
203func TestStripSlashesWithNilContext(t *testing.T) {
204 r := http.NewServeMux()
205
206 r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
207 w.Write([]byte("root"))
208 })
209
210 r.HandleFunc("/accounts", func(w http.ResponseWriter, r *http.Request) {
211 w.Write([]byte("accounts"))
212 })
213
214 r.HandleFunc("/accounts/admin", func(w http.ResponseWriter, r *http.Request) {
215 w.Write([]byte("admin"))
216 })
217
218 ts := httptest.NewServer(StripSlashes(r))
219 defer ts.Close()
220
221 if _, resp := testRequest(t, ts, "GET", "/", nil); resp != "root" {
222 t.Fatal(resp)
223 }
224 if _, resp := testRequest(t, ts, "GET", "//", nil); resp != "root" {
225 t.Fatal(resp)
226 }
227 if _, resp := testRequest(t, ts, "GET", "/accounts", nil); resp != "accounts" {
228 t.Fatal(resp)
229 }
230 if _, resp := testRequest(t, ts, "GET", "/accounts/", nil); resp != "accounts" {
231 t.Fatal(resp)
232 }
233 if _, resp := testRequest(t, ts, "GET", "/accounts/admin", nil); resp != "admin" {
234 t.Fatal(resp)
235 }
236 if _, resp := testRequest(t, ts, "GET", "/accounts/admin/", nil); resp != "admin" {
237 t.Fatal(resp)
238 }
239}
240
241func TestStripPrefix(t *testing.T) {
242 r := chi.NewRouter()

Callers

nothing calls this directly

Calls 5

StripSlashesFunction · 0.85
CloseMethod · 0.80
testRequestFunction · 0.70
HandleFuncMethod · 0.65
WriteMethod · 0.65

Tested by

no test coverage detected