-
Notifications
You must be signed in to change notification settings - Fork 5
/
context_test.go
56 lines (51 loc) · 1.26 KB
/
context_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package vesper
import (
"context"
"reflect"
"testing"
"github.com/aws/aws-lambda-go/events"
"github.com/stretchr/testify/assert"
)
func TestPayloadFromContext(t *testing.T) {
in := []byte(`
{
"message": "hello world"
}
`)
called := false
v := New(func(ctx context.Context, i interface{}) (int, error) {
payload, ok := PayloadFromContext(ctx)
assert.True(t, ok)
assert.Equal(t, in, payload)
called = true
return 1, nil
})
_, _ = v.buildHandler().Invoke(context.Background(), in)
assert.True(t, called)
}
func TestTInFromContext(t *testing.T) {
t.Run("no TIn in handler signature", func(t *testing.T) {
called := false
v := New(func(ctx context.Context) error {
_, ok := TInFromContext(ctx)
assert.False(t, ok)
called = true
return nil
})
_, _ = v.buildHandler().Invoke(context.Background(), []byte("{}"))
assert.True(t, called)
})
t.Run("TIn in handler signature", func(t *testing.T) {
called := false
handler := func(ctx context.Context, evt events.SQSEvent) error {
tIn, ok := TInFromContext(ctx)
assert.True(t, ok)
assert.Equal(t, reflect.TypeOf(events.SQSEvent{}), tIn)
called = true
return nil
}
v := New(handler)
_, _ = v.buildHandler().Invoke(context.Background(), []byte("{}"))
assert.True(t, called)
})
}