-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgateway_test.go
102 lines (78 loc) · 2.98 KB
/
gateway_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
package lamway
import (
"context"
"encoding/json"
"fmt"
"net/http"
"testing"
"github.com/aws/aws-lambda-go/events"
"github.com/stretchr/testify/assert"
"github.com/danteay/lamway/request"
)
const testPath = "/pets/luna"
func hello(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Custom-Header", "custom-value")
_, _ = fmt.Fprintln(w, "Hello World from Go")
}
func TestGateway_invoke(t *testing.T) {
t.Run("should execute api gateway v1", func(t *testing.T) {
evt := events.APIGatewayProxyRequest{
Path: testPath,
HTTPMethod: http.MethodPost,
}
gw := New[events.APIGatewayProxyRequest](WithHTTPHandler(http.HandlerFunc(hello)))
payload, err := gw.invoke(context.Background(), evt)
res, err := json.Marshal(payload)
if err != nil {
assert.Fail(t, "can't marshal payload", err)
}
assert.NoError(t, err)
assert.JSONEq(t, `{"body":"Hello World from Go\n", "headers":{"Content-Type":"text/plain; charset=utf8", "Custom-Header":"custom-value"}, "isBase64Encoded":false, "multiValueHeaders":{}, "statusCode":200}`, string(res))
})
t.Run("should execute api gateway v2", func(t *testing.T) {
evt := events.APIGatewayV2HTTPRequest{
RawPath: testPath,
RequestContext: events.APIGatewayV2HTTPRequestContext{
HTTP: events.APIGatewayV2HTTPRequestContextHTTPDescription{
Method: http.MethodPost,
Path: testPath,
},
},
}
gw := New[events.APIGatewayV2HTTPRequest](WithHTTPHandler(http.HandlerFunc(hello)))
payload, err := gw.invoke(context.Background(), evt)
res, err := json.Marshal(payload)
if err != nil {
assert.Fail(t, "can't marshal payload", err)
}
assert.NoError(t, err)
assert.JSONEq(t, `{"body":"Hello World from Go\n", "cookies":null, "headers":{"Content-Type":"text/plain; charset=utf8", "Custom-Header":"custom-value"}, "isBase64Encoded":false, "multiValueHeaders":{}, "statusCode":200}`, string(res))
})
t.Run("should get error bay error parsing path on v1", func(t *testing.T) {
evt := events.APIGatewayProxyRequest{
Path: testPath + string(rune(0x7f)),
HTTPMethod: http.MethodPost,
}
gw := New[events.APIGatewayProxyRequest](WithHTTPHandler(http.HandlerFunc(hello)))
res, err := gw.invoke(context.Background(), evt)
assert.Error(t, err)
assert.ErrorIs(t, err, request.ErrParsingPathFailed)
assert.Equal(t, gw.defaultResponse.ToV1Map(), res)
})
t.Run("should get error bay error parsing path on v2", func(t *testing.T) {
evt := events.APIGatewayV2HTTPRequest{
RawPath: testPath + string(rune(0x7f)),
RequestContext: events.APIGatewayV2HTTPRequestContext{
HTTP: events.APIGatewayV2HTTPRequestContextHTTPDescription{
Method: http.MethodPost,
Path: testPath + string(rune(0x7f)),
},
},
}
gw := New[events.APIGatewayV2HTTPRequest](WithHTTPHandler(http.HandlerFunc(hello)))
res, err := gw.invoke(context.Background(), evt)
assert.Error(t, err)
assert.ErrorIs(t, err, request.ErrParsingPathFailed)
assert.Equal(t, gw.defaultResponse.ToV2Map(), res)
})
}