@@ -18,102 +18,130 @@ func TestParse(t *testing.T) {
1818
1919 tests := map [string ]struct {
2020 event json.RawMessage
21- want [] ContentType
21+ want ContentType
2222 wantErr bool
2323 }{
2424 "cloudwatch logs" : {
2525 event : json .RawMessage (`{"awslogs":{"data":"dGVzdA=="}}` ),
26- want : [] ContentType { ContentTypeCloudwatchLogs } ,
26+ want : ContentTypeCloudwatchLogs ,
2727 },
2828 "s3" : {
2929 event : json .RawMessage (`{"Records":[{"eventSource":"aws:s3","s3":{"bucket":{"name":"b"},"object":{"key":"k"}}}]}` ),
30- want : [] ContentType { ContentTypeS3 } ,
30+ want : ContentTypeS3 ,
3131 },
3232 "kinesis" : {
3333 event : json .RawMessage (`{"Records":[{"eventSource":"aws:kinesis","kinesis":{"data":"dGVzdA=="}}]}` ),
34- want : [] ContentType { ContentTypeKinesis } ,
34+ want : ContentTypeKinesis ,
3535 },
3636 "eventbridge generic" : {
3737 event : json .RawMessage (`{"version":"0","id":"abc","detail-type":"Scheduled Event","source":"aws.events","detail":{}}` ),
38- want : [] ContentType { ContentTypeEventBridge } ,
38+ want : ContentTypeEventBridge ,
3939 },
4040 "eventbridge s3" : {
4141 event : json .RawMessage (`{"version":"0","detail-type":"Object Created","source":"aws.s3","detail":{"bucket":{"name":"my-bucket"},"object":{"key":"my-key"}}}` ),
42- want : [] ContentType { ContentTypeS3 } ,
42+ want : ContentTypeS3 ,
4343 },
4444 "eventbridge ec2" : {
4545 event : json .RawMessage (`{"version":"0","detail-type":"EC2 Instance State-change Notification","source":"aws.ec2","detail":{"instance-id":"i-123"}}` ),
46- want : [] ContentType { ContentTypeEventBridge } ,
46+ want : ContentTypeEventBridge ,
4747 },
4848 "eventbridge s3 without object created" : {
4949 event : json .RawMessage (`{"version":"0","detail-type":"Object Deleted","source":"aws.s3","detail":{}}` ),
50- want : [] ContentType { ContentTypeEventBridge } ,
50+ want : ContentTypeEventBridge ,
5151 },
5252 "sns with s3" : {
5353 event : json .RawMessage (`{"Records":[{"EventSource":"aws:sns","Sns":{"Type":"Notification","Message":"{\"Records\":[{\"eventSource\":\"aws:s3\",\"s3\":{\"bucket\":{\"name\":\"b\"},\"object\":{\"key\":\"k\"}}}]}"}}]}` ),
54- want : [] ContentType { ContentTypeS3 } ,
54+ want : ContentTypeS3 ,
5555 },
5656 "sns standalone" : {
5757 event : json .RawMessage (`{"Records":[{"EventSource":"aws:sns","Sns":{"Type":"Notification","Message":"hello world","TopicArn":"arn:aws:sns:us-east-1:123456789012:my-topic"}}]}` ),
58- want : []ContentType {ContentTypeSNS },
58+ want : ContentTypeSNS ,
59+ },
60+ "empty object" : {
61+ event : json .RawMessage (`{}` ),
62+ wantErr : true ,
63+ },
64+ "unsupported source" : {
65+ event : json .RawMessage (`{"Records":[{"eventSource":"aws:dynamodb"}]}` ),
66+ wantErr : true ,
67+ },
68+ "not JSON" : {
69+ event : json .RawMessage (`not json` ),
70+ wantErr : true ,
5971 },
60- "sqs with direct s3" : {
72+ }
73+
74+ for name , tc := range tests {
75+ t .Run (name , func (t * testing.T ) {
76+ t .Parallel ()
77+
78+ got , err := Parse (tc .event )
79+
80+ if tc .wantErr {
81+ require .Error (t , err )
82+ return
83+ }
84+
85+ require .NoError (t , err )
86+ assert .Equal (t , tc .want , got .ContentType )
87+ assert .NotEmpty (t , got .Payload )
88+ })
89+ }
90+ }
91+
92+ func TestSQS (t * testing.T ) {
93+ t .Parallel ()
94+
95+ tests := map [string ]struct {
96+ event json.RawMessage
97+ want []ContentType
98+ wantErr bool
99+ }{
100+ "direct s3" : {
61101 event : json .RawMessage (`{"Records":[{"eventSource":"aws:sqs","body":"{\"Records\":[{\"eventSource\":\"aws:s3\",\"s3\":{\"bucket\":{\"name\":\"b\"},\"object\":{\"key\":\"k\"}}}]}"}]}` ),
62102 want : []ContentType {ContentTypeS3 },
63103 },
64- "sqs with sns s3" : {
104+ "sns s3" : {
65105 event : json .RawMessage (`{"Records":[{"eventSource":"aws:sqs","body":"{\"Type\":\"Notification\",\"Message\":\"{\\\"Records\\\":[{\\\"eventSource\\\":\\\"aws:s3\\\",\\\"s3\\\":{\\\"bucket\\\":{\\\"name\\\":\\\"b\\\"},\\\"object\\\":{\\\"key\\\":\\\"k\\\"}}}]}\"}"}]}` ),
66106 want : []ContentType {ContentTypeS3 },
67107 },
68- "sqs with multiple records" : {
108+ "multiple records" : {
69109 event : json .RawMessage (`{"Records":[` +
70110 `{"eventSource":"aws:sqs","body":"{\"Records\":[{\"eventSource\":\"aws:s3\",\"s3\":{\"bucket\":{\"name\":\"b1\"},\"object\":{\"key\":\"k1\"}}}]}"},` +
71111 `{"eventSource":"aws:sqs","body":"{\"Records\":[{\"eventSource\":\"aws:s3\",\"s3\":{\"bucket\":{\"name\":\"b2\"},\"object\":{\"key\":\"k2\"}}}]}"}` +
72112 `]}` ),
73113 want : []ContentType {ContentTypeS3 , ContentTypeS3 },
74114 },
75- "sqs with sns standalone" : {
115+ "sns standalone" : {
76116 event : json .RawMessage (`{"Records":[{"eventSource":"aws:sqs","body":"{\"Type\":\"Notification\",\"Message\":\"hello world\"}"}]}` ),
77117 want : []ContentType {ContentTypeSNS },
78118 },
79- "sqs with subscription confirmation skipped" : {
119+ "subscription confirmation skipped" : {
80120 event : json .RawMessage (`{"Records":[{"eventSource":"aws:sqs","body":"{\"Type\":\"SubscriptionConfirmation\",\"Message\":\"confirm\"}"}]}` ),
81121 wantErr : true ,
82122 },
83- "sqs mixed valid and unrecognized" : {
123+ "mixed valid and unrecognized" : {
84124 event : json .RawMessage (`{"Records":[` +
85125 `{"eventSource":"aws:sqs","body":"{\"foo\":\"bar\"}"},` +
86126 `{"eventSource":"aws:sqs","body":"{\"Records\":[{\"eventSource\":\"aws:s3\",\"s3\":{\"bucket\":{\"name\":\"b\"},\"object\":{\"key\":\"k\"}}}]}"}` +
87127 `]}` ),
88128 wantErr : true ,
89129 },
90- "sqs with extra fields after body" : {
130+ "extra fields after body" : {
91131 event : json .RawMessage (`{"Records":[{"eventSource":"aws:sqs","body":"{\"Records\":[{\"eventSource\":\"aws:s3\",\"s3\":{\"bucket\":{\"name\":\"b\"},\"object\":{\"key\":\"k\"}}}]}","messageId":"abc","receiptHandle":"xyz","attributes":{"ApproximateReceiveCount":"1"}}]}` ),
92132 want : []ContentType {ContentTypeS3 },
93133 },
94- "sqs with malformed body json" : {
134+ "malformed body json" : {
95135 event : json .RawMessage (`{"Records":[{"eventSource":"aws:sqs","body":"not json"}]}` ),
96136 wantErr : true ,
97137 },
98- "empty object" : {
99- event : json .RawMessage (`{}` ),
100- wantErr : true ,
101- },
102- "unsupported source" : {
103- event : json .RawMessage (`{"Records":[{"eventSource":"aws:dynamodb"}]}` ),
104- wantErr : true ,
105- },
106- "not JSON" : {
107- event : json .RawMessage (`not json` ),
108- wantErr : true ,
109- },
110138 }
111139
112140 for name , tc := range tests {
113141 t .Run (name , func (t * testing.T ) {
114142 t .Parallel ()
115143
116- got , err := Parse (tc .event )
144+ got , err := SQS (tc .event )
117145
118146 if tc .wantErr {
119147 require .Error (t , err )
@@ -123,9 +151,9 @@ func TestParse(t *testing.T) {
123151 require .NoError (t , err )
124152 require .Len (t , got , len (tc .want ))
125153
126- for i , pe := range got {
127- assert .Equal (t , tc .want [i ], pe .ContentType )
128- assert .NotEmpty (t , pe .Payload )
154+ for i , se := range got {
155+ assert .Equal (t , tc .want [i ], se .ContentType )
156+ assert .NotEmpty (t , se .Payload )
129157 }
130158 })
131159 }
0 commit comments