-
Notifications
You must be signed in to change notification settings - Fork 4
/
lambda_test.go
107 lines (92 loc) · 2.27 KB
/
lambda_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
package awslambda
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/aws/aws-sdk-go/service/lambda"
)
func TestInvokeOK(t *testing.T) {
replyPayload := `{ "name": "bob"}`
invoker := &FakeInvoker{
Calls: []*lambda.InvokeInput{},
Reply: &lambda.InvokeOutput{
Payload: []byte(replyPayload),
},
}
h := initHandler(invoker)
r, err := http.NewRequest("POST", "/lambda-test/foo", bytes.NewBufferString("hi"))
if err != nil {
t.Fatalf("Failed to create request: %v", err)
}
w := httptest.NewRecorder()
status, err := h.ServeHTTP(w, r)
if err != nil {
t.Errorf("ServeHTTP returned err: %v", err)
}
if status != 200 {
t.Errorf("Expected 200 status, got: %d", status)
}
if len(invoker.Calls) != 1 {
t.Errorf("Expected 1 Invoke call, but got: %+v", invoker.Calls)
}
expected := replyPayload
actual := w.Body.String()
if expected != actual {
t.Errorf("\nResponse body did not match\nExpected: %s\n Actual: %s", expected, actual)
}
}
func TestInvokeInvalidFunc(t *testing.T) {
h := initHandler(nil)
h.Configs[0].Include = []string{"blah"}
r, err := http.NewRequest("POST", "/lambda-test/invalid", bytes.NewBufferString("hi"))
if err != nil {
t.Fatalf("Failed to create request: %v", err)
}
w := httptest.NewRecorder()
status, err := h.ServeHTTP(w, r)
if err != nil {
t.Errorf("ServeHTTP returned err: %v", err)
}
if status != 202 {
t.Errorf("Expected 202 status, got: %d", status)
}
}
////////////////////////////////////////
func marshalJSON(i interface{}) []byte {
j, err := json.Marshal(i)
if err != nil {
panic(err)
}
return j
}
func initHandler(invoker Invoker) Handler {
return Handler{
Next: &FakeHandler{ReplyStatus: 202},
Configs: []*Config{
&Config{
Path: "/lambda-test/",
invoker: invoker,
},
},
}
}
type FakeInvoker struct {
Calls []*lambda.InvokeInput
Reply *lambda.InvokeOutput
ReplyError error
}
func (i *FakeInvoker) Invoke(input *lambda.InvokeInput) (*lambda.InvokeOutput, error) {
i.Calls = append(i.Calls, input)
return i.Reply, i.ReplyError
}
type FakeHandler struct {
ReplyStatus int
ReplyError error
Calls int
}
func (h *FakeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
h.Calls++
return h.ReplyStatus, h.ReplyError
}