func TestWrapMethods(t *testing.T) { cases := []struct { Method string Register func(*ctxmux.Mux, string, ctxmux.Handler) }{ {Method: "HEAD", Register: (*ctxmux.Mux).HEAD}, {Method: "GET", Register: (*ctxmux.Mux).GET}, {Method: "POST", Register: (*ctxmux.Mux).POST}, {Method: "PUT", Register: (*ctxmux.Mux).PUT}, {Method: "DELETE", Register: (*ctxmux.Mux).DELETE}, {Method: "PATCH", Register: (*ctxmux.Mux).PATCH}, } const key = int(1) const val = int(2) body := []byte("body") for _, c := range cases { mux, err := ctxmux.New( ctxmux.MuxContextMaker(func(r *http.Request) (context.Context, error) { ctx := context.Background() return context.WithValue(ctx, key, val), nil }), ) ensure.Nil(t, err) hw := httptest.NewRecorder() hr := &http.Request{ Method: c.Method, URL: &url.URL{ Path: "/", }, } c.Register(mux, hr.URL.Path, func(ctx context.Context, w http.ResponseWriter, r *http.Request) error { ensure.DeepEqual(t, ctx.Value(key), val) w.Write(body) return nil }) mux.ServeHTTP(hw, hr) ensure.DeepEqual(t, hw.Body.Bytes(), body) } }
// WithConfig returns a new context with the specified Config. func WithConfig(ctx context.Context, c Config) context.Context { return context.WithValue(ctx, configKey, c) }
// WithEnv adds the given env to the context. func WithEnv(ctx context.Context, env *Env) context.Context { return context.WithValue(ctx, contextEnvKey, env) }
// WithParams returns a new context.Context instance with the params included. func WithParams(ctx context.Context, p httprouter.Params) context.Context { return context.WithValue(ctx, contextParamsKey, p) }