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

Function TestMuxBasic

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

Source from the content-addressed store, hash-verified

14)
15
16func TestMuxBasic(t *testing.T) {
17 var count uint64
18 countermw := func(next http.Handler) http.Handler {
19 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
20 count++
21 next.ServeHTTP(w, r)
22 })
23 }
24
25 usermw := func(next http.Handler) http.Handler {
26 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
27 ctx := r.Context()
28 ctx = context.WithValue(ctx, ctxKey{"user"}, "peter")
29 r = r.WithContext(ctx)
30 next.ServeHTTP(w, r)
31 })
32 }
33
34 exmw := func(next http.Handler) http.Handler {
35 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
36 ctx := context.WithValue(r.Context(), ctxKey{"ex"}, "a")
37 r = r.WithContext(ctx)
38 next.ServeHTTP(w, r)
39 })
40 }
41
42 logbuf := bytes.NewBufferString("")
43 logmsg := "logmw test"
44 logmw := func(next http.Handler) http.Handler {
45 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
46 logbuf.WriteString(logmsg)
47 next.ServeHTTP(w, r)
48 })
49 }
50
51 cxindex := func(w http.ResponseWriter, r *http.Request) {
52 ctx := r.Context()
53 user := ctx.Value(ctxKey{"user"}).(string)
54 w.WriteHeader(200)
55 w.Write([]byte(fmt.Sprintf("hi %s", user)))
56 }
57
58 ping := func(w http.ResponseWriter, r *http.Request) {
59 w.WriteHeader(200)
60 w.Write([]byte("."))
61 }
62
63 headPing := func(w http.ResponseWriter, r *http.Request) {
64 w.Header().Set("X-Ping", "1")
65 w.WriteHeader(200)
66 }
67
68 createPing := func(w http.ResponseWriter, r *http.Request) {
69 // create ....
70 w.WriteHeader(201)
71 }
72
73 pingAll := func(w http.ResponseWriter, r *http.Request) {

Callers

nothing calls this directly

Calls 15

UseMethod · 0.95
GetMethod · 0.95
MethodMethod · 0.95
MethodFuncMethod · 0.95
HeadMethod · 0.95
PostMethod · 0.95
HandleFuncMethod · 0.95
URLParamFunction · 0.85
NewRouterFunction · 0.85
HandlerFuncMethod · 0.80
ValueMethod · 0.80
CloseMethod · 0.80

Tested by

no test coverage detected