@@ -7,13 +7,29 @@ import (
77 "errors"
88 "fmt"
99 "testing"
10+ "time"
1011
1112 "github.com/aws/aws-lambda-go/lambda/handlertrace"
1213 "github.com/aws/aws-lambda-go/lambda/messages"
1314 "github.com/stretchr/testify/assert"
1415)
1516
1617func TestInvalidHandlers (t * testing.T ) {
18+ type valuer interface {
19+ Value (key interface {}) interface {}
20+ }
21+
22+ type customContext interface {
23+ context.Context
24+ MyCustomMethod ()
25+ }
26+
27+ type myContext interface {
28+ Deadline () (deadline time.Time , ok bool )
29+ Done () <- chan struct {}
30+ Err () error
31+ Value (key interface {}) interface {}
32+ }
1733
1834 testCases := []struct {
1935 name string
@@ -72,12 +88,58 @@ func TestInvalidHandlers(t *testing.T) {
7288 handler : func () {
7389 },
7490 },
91+ {
92+ name : "the handler takes the empty interface" ,
93+ expected : nil ,
94+ handler : func (v interface {}) error {
95+ if _ , ok := v .(context.Context ); ok {
96+ return errors .New ("v should not be a Context" )
97+ }
98+ return nil
99+ },
100+ },
101+ {
102+ name : "the handler takes a subset of context.Context" ,
103+ expected : errors .New ("handler takes an interface, but it is not context.Context: \" valuer\" " ),
104+ handler : func (ctx valuer ) {
105+ },
106+ },
107+ {
108+ name : "the handler takes a same interface with context.Context" ,
109+ expected : nil ,
110+ handler : func (ctx myContext ) {
111+ },
112+ },
113+ {
114+ name : "the handler takes a superset of context.Context" ,
115+ expected : errors .New ("handler takes an interface, but it is not context.Context: \" customContext\" " ),
116+ handler : func (ctx customContext ) {
117+ },
118+ },
119+ {
120+ name : "the handler takes two arguments and first argument is a subset of context.Context" ,
121+ expected : errors .New ("handler takes two arguments, but the first is not Context. got interface" ),
122+ handler : func (ctx valuer , v interface {}) {
123+ },
124+ },
125+ {
126+ name : "the handler takes two arguments and first argument is a same interface with context.Context" ,
127+ expected : nil ,
128+ handler : func (ctx myContext , v interface {}) {
129+ },
130+ },
131+ {
132+ name : "the handler takes two arguments and first argument is a superset of context.Context" ,
133+ expected : errors .New ("handler takes two arguments, but the first is not Context. got interface" ),
134+ handler : func (ctx customContext , v interface {}) {
135+ },
136+ },
75137 }
76138 for i , testCase := range testCases {
77139 testCase := testCase
78140 t .Run (fmt .Sprintf ("testCase[%d] %s" , i , testCase .name ), func (t * testing.T ) {
79141 lambdaHandler := NewHandler (testCase .handler )
80- _ , err := lambdaHandler .Invoke (context .TODO (), make ( []byte , 0 ))
142+ _ , err := lambdaHandler .Invoke (context .TODO (), []byte ( "{}" ))
81143 assert .Equal (t , testCase .expected , err )
82144 })
83145 }
0 commit comments