Commit 02953c9
Changed files (2)
pkg
test
pkg/test/http.go
@@ -12,9 +12,7 @@ import (
"github.com/xlgmokha/x/pkg/x"
)
-type RequestOption x.Option[*http.Request]
-
-func Request(method, target string, options ...RequestOption) *http.Request {
+func Request(method, target string, options ...x.Option[*http.Request]) *http.Request {
request := httptest.NewRequest(method, target, nil)
for _, option := range options {
request = option(request)
@@ -22,45 +20,45 @@ func Request(method, target string, options ...RequestOption) *http.Request {
return request
}
-func RequestResponse(method, target string, options ...RequestOption) (*http.Request, *httptest.ResponseRecorder) {
+func RequestResponse(method, target string, options ...x.Option[*http.Request]) (*http.Request, *httptest.ResponseRecorder) {
return Request(method, target, options...), httptest.NewRecorder()
}
-func WithAcceptHeader(value serde.MediaType) RequestOption {
+func WithAcceptHeader(value serde.MediaType) x.Option[*http.Request] {
return WithRequestHeader("Accept", string(value))
}
-func WithRequestHeader(key, value string) RequestOption {
+func WithRequestHeader(key, value string) x.Option[*http.Request] {
return func(r *http.Request) *http.Request {
r.Header.Set(key, value)
return r
}
}
-func WithContentType[T any](item T, mediaType serde.MediaType) RequestOption {
+func WithContentType[T any](item T, mediaType serde.MediaType) x.Option[*http.Request] {
body := bytes.NewBuffer(nil)
x.Check(serde.To[T](body, item, mediaType))
return WithRequestBody(io.NopCloser(body))
}
-func WithRequestBody(body io.ReadCloser) RequestOption {
+func WithRequestBody(body io.ReadCloser) x.Option[*http.Request] {
return func(r *http.Request) *http.Request {
r.Body = body
return r
}
}
-func WithContext(ctx context.Context) RequestOption {
+func WithContext(ctx context.Context) x.Option[*http.Request] {
return func(r *http.Request) *http.Request {
return r.WithContext(ctx)
}
}
-func WithContextKeyValue[T any](ctx context.Context, key xcontext.Key[T], item T) RequestOption {
+func WithContextKeyValue[T any](ctx context.Context, key xcontext.Key[T], item T) x.Option[*http.Request] {
return WithContext(key.With(ctx, item))
}
-func WithCookie(cookie *http.Cookie) RequestOption {
+func WithCookie(cookie *http.Cookie) x.Option[*http.Request] {
return func(r *http.Request) *http.Request {
r.AddCookie(cookie)
return r
pkg/test/http_test.go
@@ -11,14 +11,15 @@ import (
"github.com/stretchr/testify/require"
"github.com/xlgmokha/x/pkg/context"
"github.com/xlgmokha/x/pkg/serde"
+ "github.com/xlgmokha/x/pkg/x"
)
-var exampleHeader RequestOption = RequestOption(func(r *http.Request) *http.Request {
+var exampleHeader x.Option[*http.Request] = x.Option[*http.Request](func(r *http.Request) *http.Request {
r.Header.Add("X-Example", "example")
return r
})
-var withHost RequestOption = RequestOption(func(r *http.Request) *http.Request {
+var withHost x.Option[*http.Request] = x.Option[*http.Request](func(r *http.Request) *http.Request {
r.Host = "example.com"
return r
})