(t *testing.T)
| 105 | const fooKey ctxKey = iota |
| 106 | |
| 107 | func TestUseModule(t *testing.T) { |
| 108 | r := mux.NewRouter() |
| 109 | s := FuncServlet("/foo", func(res http.ResponseWriter, req *http.Request) { |
| 110 | ctx := req.Context() |
| 111 | foo := ctx.Value(fooKey) |
| 112 | res.Write([]byte(fmt.Sprintf("foo: %s", foo))) |
| 113 | }) |
| 114 | |
| 115 | s = UseServlet(s, func(next http.Handler) http.Handler { |
| 116 | return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { |
| 117 | ctx := req.Context() |
| 118 | ctx = context.WithValue(ctx, fooKey, "bar") |
| 119 | req = req.WithContext(ctx) |
| 120 | next.ServeHTTP(res, req) |
| 121 | }) |
| 122 | }) |
| 123 | s.RegisterRouting(r) |
| 124 | |
| 125 | res := httptest.NewRecorder() |
| 126 | req, err := http.NewRequest("GET", "/foo", nil) |
| 127 | assert.NoError(t, err) |
| 128 | r.ServeHTTP(res, req) |
| 129 | |
| 130 | assert.Equal(t, "foo: bar", res.Body.String()) |
| 131 | } |
| 132 | |
| 133 | func TestUseModuleCombine(t *testing.T) { |
| 134 | s1 := HandlerServlet("/ok", h) |
nothing calls this directly
no test coverage detected