Skip to content
This repository has been archived by the owner on Mar 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #107 from tylerslaton/ctx-for-tests
Browse files Browse the repository at this point in the history
Add NewRequestWithContext, InvokeWithContext and InvokeFuncWithContext
  • Loading branch information
tylerslaton committed Oct 23, 2023
2 parents 6bdcd96 + 1dcdb08 commit 33b4d20
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions pkg/router/tester/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,17 @@ func (b *Harness) InvokeFunc(t *testing.T, input kclient.Object, handler router.
return b.Invoke(t, input, handler)
}

func (b *Harness) InvokeFuncWithContext(t *testing.T, ctx context.Context, input kclient.Object, handler router.HandlerFunc) (*Response, error) {
t.Helper()
return b.InvokeWithContext(t, ctx, input, handler)
}

func NewRequest(t *testing.T, scheme *runtime.Scheme, input kclient.Object, existing ...kclient.Object) router.Request {
t.Helper()
return NewRequestWithContext(t, context.TODO(), scheme, input, existing...)
}

func NewRequestWithContext(t *testing.T, ctx context.Context, scheme *runtime.Scheme, input kclient.Object, existing ...kclient.Object) router.Request {
t.Helper()
gvk, err := apiutil.GVKForObject(input, scheme)
if err != nil {
Expand All @@ -164,7 +174,7 @@ func NewRequest(t *testing.T, scheme *runtime.Scheme, input kclient.Object, exis
SchemeObj: scheme,
},
Object: input,
Ctx: context.TODO(),
Ctx: ctx,
GVK: gvk,
Namespace: input.GetNamespace(),
Name: input.GetName(),
Expand All @@ -174,9 +184,14 @@ func NewRequest(t *testing.T, scheme *runtime.Scheme, input kclient.Object, exis
}

func (b *Harness) Invoke(t *testing.T, input kclient.Object, handler router.Handler) (*Response, error) {
t.Helper()
return b.InvokeWithContext(t, context.TODO(), input, handler)
}

func (b *Harness) InvokeWithContext(t *testing.T, ctx context.Context, input kclient.Object, handler router.Handler) (*Response, error) {
t.Helper()
var (
req = NewRequest(t, b.Scheme, input, b.Existing...)
req = NewRequestWithContext(t, ctx, b.Scheme, input, b.Existing...)
resp = Response{
Client: req.Client.(*Client),
}
Expand Down

0 comments on commit 33b4d20

Please sign in to comment.