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

Function TestSingleHandler

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

Source from the content-addressed store, hash-verified

1290}
1291
1292func TestSingleHandler(t *testing.T) {
1293 h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1294 name := URLParam(r, "name")
1295 w.Write([]byte("hi " + name))
1296 })
1297
1298 r, _ := http.NewRequest("GET", "/", nil)
1299 rctx := NewRouteContext()
1300 r = r.WithContext(context.WithValue(r.Context(), RouteCtxKey, rctx))
1301 rctx.URLParams.Add("name", "joe")
1302
1303 w := httptest.NewRecorder()
1304 h.ServeHTTP(w, r)
1305
1306 body := w.Body.String()
1307 expected := "hi joe"
1308 if body != expected {
1309 t.Fatalf("expected:%s got:%s", expected, body)
1310 }
1311}
1312
1313// TODO: a Router wrapper test..
1314//

Callers

nothing calls this directly

Calls 7

URLParamFunction · 0.85
NewRouteContextFunction · 0.85
HandlerFuncMethod · 0.80
AddMethod · 0.80
WriteMethod · 0.65
ServeHTTPMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected