-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathhandler_test.go
More file actions
97 lines (74 loc) · 2.29 KB
/
handler_test.go
File metadata and controls
97 lines (74 loc) · 2.29 KB
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
package handler
import (
"testing"
"github.com/aws-cloudformation/cloudformation-cli-go-plugin/cfn/cfnerr"
"github.com/aws/aws-sdk-go/aws"
)
type Props struct {
Color *string `json:"color"`
}
func TestNewRequest(t *testing.T) {
rctx := RequestContext{}
t.Run("Happy Path", func(t *testing.T) {
prev := Props{}
curr := Props{}
req := NewRequest("foo", nil, rctx, nil, []byte(`{"color": "red"}`), []byte(`{"color": "green"}`), []byte(``))
if err := req.UnmarshalPrevious(&prev); err != nil {
t.Fatalf("Unable to unmarshal props: %v", err)
}
if aws.StringValue(prev.Color) != "red" {
t.Fatalf("Previous Properties don't match: %v", prev.Color)
}
if err := req.Unmarshal(&curr); err != nil {
t.Fatalf("Unable to unmarshal props: %v", err)
}
if aws.StringValue(curr.Color) != "green" {
t.Fatalf("Properties don't match: %v", curr.Color)
}
if req.LogicalResourceID != "foo" {
t.Fatalf("Invalid Logical Resource ID: %v", req.LogicalResourceID)
}
})
t.Run("ResourceProps", func(t *testing.T) {
t.Run("Invalid Body", func(t *testing.T) {
req := NewRequest("foo", nil, rctx, nil, []byte(``), []byte(``), []byte(``))
invalid := struct {
Color *int `json:"color"`
}{}
err := req.Unmarshal(&invalid)
if err == nil {
t.Fatalf("Didn't throw an error")
}
cfnErr := err.(cfnerr.Error)
if cfnErr.Code() != bodyEmptyError {
t.Fatalf("Wrong error returned: %v", err)
}
})
t.Run("Invalid Marshal", func(t *testing.T) {
req := NewRequest("foo", nil, rctx, nil, []byte(`{"color": "ref"}`), []byte(`---BAD JSON---`), []byte(``))
var invalid Props
err := req.Unmarshal(&invalid)
if err == nil {
t.Fatalf("Didn't throw an error")
}
cfnErr := err.(cfnerr.Error)
if cfnErr.Code() != marshalingError {
t.Fatalf("Wrong error returned: %v", err)
}
})
})
t.Run("PreviousResourceProps", func(t *testing.T) {
t.Run("Invalid Marshal", func(t *testing.T) {
req := NewRequest("foo", nil, rctx, nil, []byte(`---BAD JSON---`), []byte(`{"color": "green"}`), []byte(``))
var invalid Props
err := req.UnmarshalPrevious(&invalid)
if err == nil {
t.Fatalf("Didn't throw an error")
}
cfnErr := err.(cfnerr.Error)
if cfnErr.Code() != marshalingError {
t.Fatalf("Wrong error returned: %v", err)
}
})
})
}