Skip to content

Commit 7522090

Browse files
LegNeatobmoffatt
authored andcommitted
Add S3 test event (#163)
* Add S3 test event As seen in https://docs.aws.amazon.com/AmazonS3/latest/dev/notification-content-structure.html * add Bucket sample event: ``` {"Service":"Amazon S3","Event":"s3:TestEvent","Time":"2019-02-04T19:34:46.985Z","Bucket":"bmoffatt","RequestId":"7BA1940DC6AF888B","HostId":"q1YDbiaMjllP0m+Lcy6cKKgxNrMLFJ9zCrZUFBqHGTG++0nXvnTDIGC7q2/QPAsJg86E8gI7y9U="} ``` * add test data for S3TestEvent * Fix test typo Marchaling -> Marshaling
1 parent dcf76fe commit 7522090

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

events/s3.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,12 @@ type S3Object struct {
5151
ETag string `json:"eTag"`
5252
Sequencer string `json:"sequencer"`
5353
}
54+
55+
type S3TestEvent struct {
56+
Service string `json:"Service"`
57+
Bucket string `json:"Bucket"`
58+
Event string `json:"Event"`
59+
Time time.Time `json:"Time"`
60+
RequestID string `json:"RequestId"`
61+
HostID string `json:"HostId"`
62+
}

events/s3_test.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,44 @@ import (
1212
func TestS3EventMarshaling(t *testing.T) {
1313

1414
// 1. read JSON from file
15-
inputJson := readJsonFromFile(t, "./testdata/s3-event.json")
15+
inputJSON := readJsonFromFile(t, "./testdata/s3-event.json")
1616

1717
// 2. de-serialize into Go object
1818
var inputEvent S3Event
19-
if err := json.Unmarshal(inputJson, &inputEvent); err != nil {
19+
if err := json.Unmarshal(inputJSON, &inputEvent); err != nil {
2020
t.Errorf("could not unmarshal event. details: %v", err)
2121
}
2222

2323
// 3. serialize to JSON
24-
outputJson, err := json.Marshal(inputEvent)
24+
outputJSON, err := json.Marshal(inputEvent)
2525
if err != nil {
2626
t.Errorf("could not marshal event. details: %v", err)
2727
}
2828

2929
// 4. check result
30-
assert.JSONEq(t, string(inputJson), string(outputJson))
30+
assert.JSONEq(t, string(inputJSON), string(outputJSON))
3131
}
3232

33-
func TestS3MarshalingMalformedJson(t *testing.T) {
33+
func TestS3TestEventMarshaling(t *testing.T) {
34+
inputJSON := []byte(`{
35+
"Service" :"Amazon S3",
36+
"Event": "s3:TestEvent",
37+
"Time": "2019-02-04T19:34:46.985Z",
38+
"Bucket": "bmoffatt",
39+
"RequestId": "7BA1940DC6AF888B",
40+
"HostId": "q1YDbiaMjllP0m+Lcy6cKKgxNrMLFJ9zCrZUFBqHGTG++0nXvnTDIGC7q2/QPAsJg86E8gI7y9U="
41+
}`)
42+
var inputEvent S3TestEvent
43+
if err := json.Unmarshal(inputJSON, &inputEvent); err != nil {
44+
t.Errorf("could not marshal event. details: %v", err)
45+
}
46+
outputJSON, err := json.Marshal(inputEvent)
47+
if err != nil {
48+
t.Errorf("could not marshal event. details: %v", err)
49+
}
50+
assert.JSONEq(t, string(inputJSON), string(outputJSON))
51+
}
52+
53+
func TestS3MarshalingMalformedJSON(t *testing.T) {
3454
test.TestMalformedJson(t, S3Event{})
3555
}

0 commit comments

Comments
 (0)