Skip to content

Commit

Permalink
add test case for aws#377
Browse files Browse the repository at this point in the history
  • Loading branch information
shogo82148 committed Dec 21, 2022
1 parent 65f8ccd commit 68562a7
Showing 1 changed file with 66 additions and 1 deletion.
67 changes: 66 additions & 1 deletion lambda/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,29 @@ import (
"errors"
"fmt"
"testing"
"time"

"github.com/aws/aws-lambda-go/lambda/handlertrace"
"github.com/aws/aws-lambda-go/lambda/messages"
"github.com/stretchr/testify/assert"
)

func TestInvalidHandlers(t *testing.T) {
type valuer interface {
Value(key interface{}) interface{}
}

type customContext interface {
context.Context
MyCustomMethod()
}

type myContext interface {
Deadline() (deadline time.Time, ok bool)
Done() <-chan struct{}
Err() error
Value(key any) any
}

testCases := []struct {
name string
Expand Down Expand Up @@ -72,12 +88,61 @@ func TestInvalidHandlers(t *testing.T) {
handler: func() {
},
},
{
name: "the handler takes the empty interface",
expected: nil,
handler: func(v interface{}) error {
if _, ok := v.(context.Context); ok {
return errors.New("v should not be a Context")
}
return nil
},
},
// TODO: fix me
// {
// name: "the handler takes a subset of context.Context",
// expected: nil,
// handler: func(ctx valuer) {
// },
// },
{
name: "the handler takes a same interface with context.Context",
expected: nil,
handler: func(ctx myContext) {
},
},
// TODO: fix me
// {
// name: "the handler takes a superset of context.Context",
// expected: errors.New("handler takes an interface, but context.Context does not implement it: \"customContext\""),
// handler: func(ctx customContext) {
// },
// },
// {
// name: "the handler takes two arguments and first argument is a subset of context.Context",
// expected: nil,
// handler: func(ctx valuer, v interface{}) {
// },
// },
{
name: "the handler takes two arguments and first argument is a same interface with context.Context",
expected: nil,
handler: func(ctx myContext, v interface{}) {
},
},
// TODO: fix me
// {
// name: "the handler takes two arguments and first argument is a superset of context.Context",
// expected: errors.New("handler takes two arguments, but the first is not Context. got interface"),
// handler: func(ctx customContext, v interface{}) {
// },
// },
}
for i, testCase := range testCases {
testCase := testCase
t.Run(fmt.Sprintf("testCase[%d] %s", i, testCase.name), func(t *testing.T) {
lambdaHandler := NewHandler(testCase.handler)
_, err := lambdaHandler.Invoke(context.TODO(), make([]byte, 0))
_, err := lambdaHandler.Invoke(context.TODO(), []byte("{}"))
assert.Equal(t, testCase.expected, err)
})
}
Expand Down

0 comments on commit 68562a7

Please sign in to comment.