-
Notifications
You must be signed in to change notification settings - Fork 48
/
apigateway_test.go
108 lines (95 loc) · 2.5 KB
/
apigateway_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
package sparta
import (
"context"
"fmt"
"net/http"
"testing"
"time"
spartaAPIGateway "github.com/mweagle/Sparta/v3/aws/apigateway"
spartaAWSEvents "github.com/mweagle/Sparta/v3/aws/events"
"github.com/rs/zerolog"
)
var randVal string
func init() {
randVal = time.Now().UTC().String()
}
type testRequest struct {
Message string
Request spartaAWSEvents.APIGatewayRequest
}
func testAPIGatewayLambda(ctx context.Context,
gatewayEvent spartaAWSEvents.APIGatewayRequest) (interface{}, error) {
logger, loggerOk := ctx.Value(ContextKeyLogger).(*zerolog.Logger)
if loggerOk {
logger.Info().Msg("Hello world structured log message")
}
// Return a message, together with the incoming input...
return spartaAPIGateway.NewResponse(http.StatusOK, &testRequest{
Message: fmt.Sprintf("Test %s", randVal),
Request: gatewayEvent,
}), nil
}
func TestAPIGatewayRequest(t *testing.T) {
requestBody := &testRequest{
Message: randVal,
}
mockRequest, mockRequestErr := spartaAWSEvents.NewAPIGatewayMockRequest("helloWorld",
http.MethodGet,
nil,
requestBody)
if mockRequestErr != nil {
t.Fatal(mockRequestErr)
}
resp, respErr := testAPIGatewayLambda(context.Background(), *mockRequest)
if respErr != nil {
t.Fatal(respErr)
} else {
t.Log(fmt.Sprintf("%#v", resp))
}
}
func TestAPIGateway(t *testing.T) {
stage := NewStage("v1")
apiGateway := NewAPIGateway("SpartaAPIGateway", stage)
lambdaFn, _ := NewAWSLambda(LambdaName(mockLambda1),
mockLambda1,
IAMRoleDefinition{})
// Register the function with the API Gateway
apiGatewayResource, _ := apiGateway.NewResource("/test", lambdaFn)
_, resErr := apiGatewayResource.NewMethod("GET", http.StatusOK)
if resErr != nil {
t.Fatalf("Failed to create new resource: %s", resErr.Error())
}
testProvisionEx(t,
[]*LambdaAWSInfo{lambdaFn},
apiGateway,
nil,
nil,
false,
nil)
}
func TestAPIV2Gateway(t *testing.T) {
stage, _ := NewAPIV2Stage("v1")
apiGateway, _ := NewAPIV2(Websocket,
"sample",
"$request.body.message",
stage)
lambdaFn, _ := NewAWSLambda(LambdaName(mockLambda1),
mockLambda1,
IAMRoleDefinition{})
apiv2Route, _ := apiGateway.NewAPIV2Route("$connect",
lambdaFn)
apiv2Route.OperationName = "ConnectRoute"
lambdaFn2, _ := NewAWSLambda(LambdaName(mockLambda2),
mockLambda2,
IAMRoleDefinition{})
apiv2Route2, _ := apiGateway.NewAPIV2Route("$disconnect",
lambdaFn2)
apiv2Route2.OperationName = "DisconnectRoute"
testProvisionEx(t,
[]*LambdaAWSInfo{lambdaFn, lambdaFn2},
apiGateway,
nil,
nil,
false,
nil)
}