-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
88 lines (82 loc) · 2.05 KB
/
main_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
package main
import (
"net/http"
"testing"
"github.com/stretchr/testify/require"
)
func TestHandleRequest(t *testing.T) {
cases := map[string]struct {
statusCode int
input interface{}
}{
"payload passthrough = true, object": {
statusCode: http.StatusOK,
input: map[string]interface{}{
"id": "payload-passthrough",
"message": "payload passthrough is true",
},
},
"payload passthrough = false": {
statusCode: http.StatusOK,
input: map[string]interface{}{
"is_base64_encoded": false,
"body": map[string]interface{}{
"id": "id1",
"message": "message",
},
},
},
"base64-encoded": {
statusCode: http.StatusOK,
input: map[string]interface{}{
"is_base64_encoded": true,
"body": "eyJpZCI6InRlc3QtaWQiLCJtZXNzYWdlIjoiSGVsbG8sIHdvcmxkIn0=",
},
},
"string": {
statusCode: http.StatusOK,
input: "testing",
},
"bool": {
statusCode: http.StatusOK,
input: true,
},
"int": {
statusCode: http.StatusOK,
input: 69,
},
"string-body": {
statusCode: http.StatusOK,
input: map[string]interface{}{"body": "testing"},
},
"bool-body": {
statusCode: http.StatusOK,
input: map[string]interface{}{"body": true},
},
"int-body": {
statusCode: http.StatusOK,
input: map[string]interface{}{"body": 69},
},
}
t.Parallel()
for name, c := range cases {
t.Run(name, func(t *testing.T) {
c := c
// If the type is a primitive (not an object), or we're simulating payload_passthrough = true
// then the expected response is the input for the test case.
expected := c.input
input, inputIsMap := c.input.(map[string]interface{})
if inputIsMap {
body, inputHasBody := input["body"]
if inputHasBody {
// payload_passthrough = false so the expected response is the input body field.
expected = body
}
}
response, err := HandleRequest(c.input)
require.NoError(t, err)
require.Equal(t, c.statusCode, response["statusCode"])
require.Equal(t, expected, response["body"])
})
}
}