-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_test.go
125 lines (119 loc) · 3.31 KB
/
handler_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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package lambdapb_test
import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"testing"
"github.com/golang/protobuf/proto"
"github.com/newlix/lambdapb"
"github.com/newlix/lambdapb/testdata"
"github.com/stretchr/testify/assert"
)
func TestInvalidHandlers(t *testing.T) {
testCases := []struct {
name string
handler interface{}
expected error
}{
{
name: "nil handler",
expected: errors.New("handler is nil"),
handler: nil,
},
{
name: "handler is not a function",
expected: errors.New("handler is not function"),
handler: struct{}{},
},
{
name: "handler declares too many arguments",
expected: errors.New("handler takes two arguments, but got 3"),
handler: func(n context.Context, x string, y string) error {
return nil
},
},
{
name: "handler first argument is not Context",
expected: errors.New("handler first argument should implement Context"),
handler: func(a string, x context.Context) error {
return nil
},
},
{
name: "handler second argument is not proto.Message",
expected: errors.New("handler second argument should implement proto.Message"),
handler: func(ctx context.Context, in string) error {
return nil
},
},
{
name: "handler returns too many values",
expected: errors.New("handler returns two values, but got 3"),
handler: func(ctx context.Context, in *testdata.Input) (error, error, error) {
return nil, nil, nil
},
},
{
name: "handler first return value should implement proto.Message",
expected: errors.New("handler first return value should implement proto.Message"),
handler: func(ctx context.Context, in *testdata.Input) (error, error) {
return nil, nil
},
},
{
name: "handler returning a single value does not implement error",
expected: errors.New("handler second return value should implement error"),
handler: func(ctx context.Context, in *testdata.Input) (*testdata.Output, string) {
return nil, ""
},
},
}
for _, testCase := range testCases {
lambdaHandler := lambdapb.NewHandler(testCase.handler)
_, err := lambdaHandler.Invoke(context.TODO(), make([]byte, 0))
assert.Equal(t, testCase.expected, err)
}
}
func TestNewHandlerInvoke(t *testing.T) {
in := &testdata.Input{
S: "s",
D: 3.14,
I: 1,
B: true,
}
h := lambdapb.NewHandler(testdata.Echo)
bIn, err := proto.Marshal(in)
assert.NoError(t, err)
base64In := base64.StdEncoding.EncodeToString(bIn)
jsonOut, err := h.Invoke(context.Background(), []byte("\""+base64In+"\""))
var base64Out string
if err := json.Unmarshal(jsonOut, &base64Out); err != nil {
t.Error(err)
}
bOut, err := base64.StdEncoding.DecodeString(base64Out)
assert.NoError(t, err)
var out testdata.Output
if err := proto.Unmarshal(bOut, &out); err != nil {
t.Error(err)
}
assert.Equal(t, in.S, out.S)
assert.InDelta(t, in.D, out.D, 0.001)
assert.Equal(t, in.I, out.I)
assert.Equal(t, in.B, out.B)
}
func TestNewHandlerInvokeErr(t *testing.T) {
in := &testdata.Input{
S: "s",
D: 3.14,
I: 1,
B: true,
}
h := lambdapb.NewHandler(testdata.Err)
bIn, err := proto.Marshal(in)
assert.NoError(t, err)
base64In := base64.StdEncoding.EncodeToString(bIn)
jsonOut, err := h.Invoke(context.Background(), []byte("\""+base64In+"\""))
assert.Nil(t, jsonOut)
assert.Error(t, err, "err")
}