@@ -7,13 +7,29 @@ import (
7
7
"errors"
8
8
"fmt"
9
9
"testing"
10
+ "time"
10
11
11
12
"github.com/aws/aws-lambda-go/lambda/handlertrace"
12
13
"github.com/aws/aws-lambda-go/lambda/messages"
13
14
"github.com/stretchr/testify/assert"
14
15
)
15
16
16
17
func 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
+ }
17
33
18
34
testCases := []struct {
19
35
name string
@@ -72,12 +88,58 @@ func TestInvalidHandlers(t *testing.T) {
72
88
handler : func () {
73
89
},
74
90
},
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
+ },
75
137
}
76
138
for i , testCase := range testCases {
77
139
testCase := testCase
78
140
t .Run (fmt .Sprintf ("testCase[%d] %s" , i , testCase .name ), func (t * testing.T ) {
79
141
lambdaHandler := NewHandler (testCase .handler )
80
- _ , err := lambdaHandler .Invoke (context .TODO (), make ( []byte , 0 ))
142
+ _ , err := lambdaHandler .Invoke (context .TODO (), []byte ( "{}" ))
81
143
assert .Equal (t , testCase .expected , err )
82
144
})
83
145
}
0 commit comments