-
Notifications
You must be signed in to change notification settings - Fork 0
/
stub_test.go
96 lines (91 loc) · 2.82 KB
/
stub_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
package lambdag_test
import (
"context"
"encoding/json"
"errors"
"net/http/httptest"
"testing"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go-v2/aws"
lambdasdk "github.com/aws/aws-sdk-go-v2/service/lambda"
"github.com/mashiike/lambdag"
"github.com/stretchr/testify/require"
)
func TestStubInvokeSuccess(t *testing.T) {
type TestResponse struct {
Name string
Success bool
}
expectedPayload := map[string]interface{}{
"Name": "Sample",
"Success": true,
}
mux := lambdag.NewLambdaAPIStubMux("HelloWorldFunction", lambda.NewHandler(func(payload json.RawMessage) (*TestResponse, error) {
var p interface{}
err := json.Unmarshal(payload, &p)
t.Logf("payload: %#v", payload)
require.NoError(t, err)
require.EqualValues(t, expectedPayload, p)
return &TestResponse{
Name: "name",
Success: true,
}, nil
}))
server := httptest.NewServer(mux)
cfg := aws.NewConfig()
client := lambdasdk.NewFromConfig(*cfg, func(opts *lambdasdk.Options) {
opts.EndpointResolver = lambdasdk.EndpointResolverFunc(func(_ string, _ lambdasdk.EndpointResolverOptions) (aws.Endpoint, error) {
return aws.Endpoint{
URL: server.URL,
SigningRegion: "us-east-1",
}, nil
})
})
output, err := client.Invoke(context.Background(), &lambdasdk.InvokeInput{
FunctionName: aws.String("HelloWorldFunction"),
Payload: Must(json.Marshal(expectedPayload)),
})
require.NoError(t, err)
require.JSONEq(t, `{"Name":"name", "Success": true}`, string(output.Payload))
}
func TestStubInvokeError(t *testing.T) {
type TestResponse struct {
Name string
Success bool
}
expectedPayload := map[string]interface{}{
"Name": "Sample",
"Success": true,
}
mux := lambdag.NewLambdaAPIStubMux("HelloWorldFunction", lambda.NewHandler(func(payload json.RawMessage) (*TestResponse, error) {
var p interface{}
err := json.Unmarshal(payload, &p)
t.Logf("payload: %#v", payload)
require.NoError(t, err)
require.EqualValues(t, expectedPayload, p)
return nil, errors.New("test error")
}))
server := httptest.NewServer(mux)
cfg := aws.NewConfig()
client := lambdasdk.NewFromConfig(*cfg, func(opts *lambdasdk.Options) {
opts.EndpointResolver = lambdasdk.EndpointResolverFunc(func(_ string, _ lambdasdk.EndpointResolverOptions) (aws.Endpoint, error) {
return aws.Endpoint{
URL: server.URL,
SigningRegion: "us-east-1",
}, nil
})
})
output, err := client.Invoke(context.Background(), &lambdasdk.InvokeInput{
FunctionName: aws.String("HelloWorldFunction"),
Payload: Must(json.Marshal(expectedPayload)),
})
require.NoError(t, err)
require.JSONEq(t, `{"errorMessage":"test error", "errorType":"errorString"}`, string(output.Payload))
require.EqualValues(t, "errorString", *output.FunctionError)
}
func Must[T any](t T, err error) T {
if err != nil {
panic(err)
}
return t
}