homebox/backend/internal/mocks/chimocker/chimocker.go

31 lines
892 B
Go
Raw Permalink Normal View History

2022-08-30 02:30:36 +00:00
package chimocker
import (
"context"
"net/http"
"github.com/go-chi/chi/v5"
)
type Params map[string]string
// WithUrlParam returns a pointer to a request object with the given URL params
// added to a new chi.Context object.
func WithUrlParam(r *http.Request, key, value string) *http.Request {
chiCtx := chi.NewRouteContext()
req := r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, chiCtx))
chiCtx.URLParams.Add(key, value)
return req
}
// WithUrlParams returns a pointer to a request object with the given URL params
// added to a new chi.Context object. for single param assignment see WithUrlParam
func WithUrlParams(r *http.Request, params Params) *http.Request {
chiCtx := chi.NewRouteContext()
req := r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, chiCtx))
for key, value := range params {
chiCtx.URLParams.Add(key, value)
}
return req
}