MCPcopy Index your code
hub / github.com/smallstep/cli / TestInjectContextWithMiddleware

Function TestInjectContextWithMiddleware

internal/command/inject_test.go:55–94  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

53}
54
55func TestInjectContextWithMiddleware(t *testing.T) {
56 t.Parallel()
57
58 fnCalled := false
59
60 type contextKey struct{}
61 type firstMiddlewareContextKey struct{}
62 type secondMiddlewareContextKey struct{}
63
64 ctx := context.Background()
65 ctx = context.WithValue(ctx, contextKey{}, true)
66
67 fn := func(ctx context.Context) error {
68 assert.True(t, ctx.Value(contextKey{}).(bool))
69 clictx := CLIContextFromContext(ctx)
70 assert.NotNil(t, clictx)
71 assert.Equal(t, 42, ctx.Value(firstMiddlewareContextKey{}).(int))
72 assert.Equal(t, "value", ctx.Value(secondMiddlewareContextKey{}).(string))
73 fnCalled = true
74 return nil
75 }
76
77 middleware := []func(context.Context) (context.Context, error){
78 func(ctx context.Context) (context.Context, error) {
79 return context.WithValue(ctx, firstMiddlewareContextKey{}, 42), nil
80 },
81 func(ctx context.Context) (context.Context, error) {
82 return context.WithValue(ctx, secondMiddlewareContextKey{}, "value"), nil
83 },
84 }
85
86 got := InjectContext(ctx, fn, middleware...)
87
88 assert.NotNil(t, got)
89
90 // execute the function to ensure injected context is OK
91 err := got(new(cli.Context))
92 assert.NoError(t, err)
93 assert.True(t, fnCalled)
94}
95
96func TestInjectContextWithMiddlewareError(t *testing.T) {
97 t.Parallel()

Callers

nothing calls this directly

Calls 2

CLIContextFromContextFunction · 0.85
InjectContextFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…