homebox/backend/internal/mocks/chimocker/chimocker.go
2022-08-29 18:30:36 -08:00

30 lines
892 B
Go

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
}