diff options
| author | mo khan <mo@mokhan.ca> | 2025-05-07 09:37:04 -0700 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2025-05-07 09:37:04 -0700 |
| commit | 61598cf8c8a2dbae368f3f8b15393c70d2e4fa9d (patch) | |
| tree | cb216b680a118c4fa122c96faecfb20141dae019 /pkg | |
| parent | be652dd283d8c42adddedc66d6ba9210d5bdb511 (diff) | |
refactor: move test server to oidc package
Diffstat (limited to 'pkg')
| -rw-r--r-- | pkg/oidc/oidc_test.go | 3 | ||||
| -rw-r--r-- | pkg/oidc/test_server.go (renamed from pkg/test/oidc_server.go) | 4 | ||||
| -rw-r--r-- | pkg/test/http.go | 68 |
3 files changed, 3 insertions, 72 deletions
diff --git a/pkg/oidc/oidc_test.go b/pkg/oidc/oidc_test.go index 3318f18..47a58ba 100644 --- a/pkg/oidc/oidc_test.go +++ b/pkg/oidc/oidc_test.go @@ -6,11 +6,10 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/test" ) func TestOpenID(t *testing.T) { - srv := test.NewOIDCServer(t) + srv := NewTestServer(t) defer srv.Close() t.Run("GET /.well-known/openid-configuration", func(t *testing.T) { diff --git a/pkg/test/oidc_server.go b/pkg/oidc/test_server.go index c635df0..5a25549 100644 --- a/pkg/test/oidc_server.go +++ b/pkg/oidc/test_server.go @@ -1,4 +1,4 @@ -package test +package oidc import ( "net/http" @@ -19,7 +19,7 @@ type TestServer struct { *testing.T } -func NewOIDCServer(t *testing.T) *TestServer { +func NewTestServer(t *testing.T) *TestServer { srv, err := mockoidc.Run() require.NoError(t, err) diff --git a/pkg/test/http.go b/pkg/test/http.go deleted file mode 100644 index b65ccc9..0000000 --- a/pkg/test/http.go +++ /dev/null @@ -1,68 +0,0 @@ -package test - -import ( - "bytes" - "context" - "io" - "net/http" - "net/http/httptest" - - xcontext "github.com/xlgmokha/x/pkg/context" - "github.com/xlgmokha/x/pkg/serde" - "github.com/xlgmokha/x/pkg/x" -) - -type RequestOption x.Option[*http.Request] - -func Request(method, target string, options ...RequestOption) *http.Request { - request := httptest.NewRequest(method, target, nil) - for _, option := range options { - request = option(request) - } - return request -} - -func RequestResponse(method, target string, options ...RequestOption) (*http.Request, *httptest.ResponseRecorder) { - return Request(method, target, options...), httptest.NewRecorder() -} - -func WithAcceptHeader(value serde.MediaType) RequestOption { - return WithRequestHeader("Accept", string(value)) -} - -func WithRequestHeader(key, value string) RequestOption { - return func(r *http.Request) *http.Request { - r.Header.Set(key, value) - return r - } -} - -func WithContentType[T any](item T, mediaType serde.MediaType) RequestOption { - body := bytes.NewBuffer(nil) - x.Check(serde.To[T](body, item, mediaType)) - return WithRequestBody(io.NopCloser(body)) -} - -func WithRequestBody(body io.ReadCloser) RequestOption { - return func(r *http.Request) *http.Request { - r.Body = body - return r - } -} - -func WithContext(ctx context.Context) RequestOption { - 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 { - return WithContext(key.With(ctx, item)) -} - -func WithCookie(cookie *http.Cookie) RequestOption { - return func(r *http.Request) *http.Request { - r.AddCookie(cookie) - return r - } -} |
