This repository has been archived by the owner on Apr 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy_request_test.go
77 lines (66 loc) · 1.71 KB
/
proxy_request_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
package xlambda_test
import (
"errors"
"net/http"
"testing"
"github.com/aws/aws-lambda-go/events"
"github.com/stretchr/testify/require"
"github.com/gofor-little/xlambda"
)
func TestProxyRequest(t *testing.T) {
request, err := xlambda.ProxyRequest(http.MethodGet, map[string]string{
"test-key": "test-value",
}, struct {
Key string `json:"key"`
}{"value"})
require.NoError(t, err)
require.Equal(t, &events.APIGatewayProxyRequest{
HTTPMethod: http.MethodGet,
QueryStringParameters: map[string]string{
"test-key": "test-value",
},
Body: `{"key":"value"}`,
}, request)
}
func TestParseAndValidate(t *testing.T) {
require.NoError(t, xlambda.ParseAndValidate(&events.APIGatewayProxyRequest{
QueryStringParameters: map[string]string{
"string": "test-string",
},
}, &parseAndValidate{}))
}
type parseAndValidate struct {
String string `mapstructure:"string"`
}
func (v parseAndValidate) Validate() error {
if v.String == "" {
return errors.New("string cannot be empty")
}
return nil
}
func TestUnmarshalAndValidate(t *testing.T) {
require.NoError(t, xlambda.UnmarshalAndValidate(&events.APIGatewayProxyRequest{
Body: `{"string": "test-string", "int": 1, "float": 1.1, "error": null}`,
}, &unmarshalAndValidate{}))
}
type unmarshalAndValidate struct {
String string `json:"string"`
Int int `json:"int"`
Float float32 `json:"float"`
Error error `json:"error"`
}
func (u *unmarshalAndValidate) Validate() error {
if u.String == "" {
return errors.New("string cannot be empty")
}
if u.Int == 0 {
return errors.New("int cannot be 0")
}
if u.Float == 0 {
return errors.New("float cannot be 0")
}
if u.Error != nil {
return errors.New("error must be nil")
}
return nil
}