Skip to content

Commit a2406aa

Browse files
Implemented streaming writer (#621)
* Implemented streaming writer Signed-off-by: Francesco Guardiani <[email protected]> * Little doc fix Signed-off-by: Francesco Guardiani <[email protected]> * Move is base 64 check Signed-off-by: Francesco Guardiani <[email protected]> * Test Signed-off-by: Francesco Guardiani <[email protected]> * Stream from the pool Signed-off-by: Francesco Guardiani <[email protected]>
1 parent 30f0adf commit a2406aa

File tree

4 files changed

+262
-149
lines changed

4 files changed

+262
-149
lines changed

v2/binding/format/format_test.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package format_test
22

33
import (
4+
"encoding/json"
45
"testing"
56

67
"github.com/stretchr/testify/require"
@@ -23,7 +24,15 @@ func TestJSON(t *testing.T) {
2324
require.NoError(e.SetData(event.ApplicationJSON, "foo"))
2425
b, err := format.JSON.Marshal(&e)
2526
require.NoError(err)
26-
require.Equal(`{"data":"foo","datacontenttype":"application/json","ex":"val","id":"id","source":"source","specversion":"0.3","type":"type"}`, string(b))
27+
assertJsonEquals(t, map[string]interface{}{
28+
"data": "foo",
29+
"datacontenttype": "application/json",
30+
"ex": "val",
31+
"id": "id",
32+
"source": "source",
33+
"specversion": "0.3",
34+
"type": "type",
35+
}, b)
2736

2837
var e2 event.Event
2938
require.NoError(format.JSON.Unmarshal(b, &e2))
@@ -65,7 +74,14 @@ func TestMarshalUnmarshal(t *testing.T) {
6574
require.NoError(e.SetData(event.ApplicationJSON, "foo"))
6675
b, err := format.Marshal(format.JSON.MediaType(), &e)
6776
require.NoError(err)
68-
require.Equal(`{"data":"foo","datacontenttype":"application/json","id":"id","source":"source","specversion":"0.3","type":"type"}`, string(b))
77+
assertJsonEquals(t, map[string]interface{}{
78+
"data": "foo",
79+
"datacontenttype": "application/json",
80+
"id": "id",
81+
"source": "source",
82+
"specversion": "0.3",
83+
"type": "type",
84+
}, b)
6985

7086
var e2 event.Event
7187
require.NoError(format.Unmarshal(format.JSON.MediaType(), b, &e2))
@@ -99,3 +115,16 @@ func TestAdd(t *testing.T) {
99115
require.NoError(err)
100116
require.Equal([]byte("undummy!"), e.Data())
101117
}
118+
119+
func assertJsonEquals(t *testing.T, want map[string]interface{}, got []byte) {
120+
var gotToCompare map[string]interface{}
121+
require.NoError(t, json.Unmarshal(got, &gotToCompare))
122+
123+
// Marshal and unmarshal want to make sure the types are correct
124+
wantBytes, err := json.Marshal(want)
125+
require.NoError(t, err)
126+
var wantToCompare map[string]interface{}
127+
require.NoError(t, json.Unmarshal(wantBytes, &wantToCompare))
128+
129+
require.Equal(t, wantToCompare, gotToCompare)
130+
}

v2/client/client_test.go

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package client_test
33
import (
44
"bytes"
55
"context"
6+
"encoding/json"
67
"fmt"
78
"io/ioutil"
89
"log"
@@ -81,6 +82,19 @@ func simpleStructuredClient(target string) client.Client {
8182
func TestClientSend(t *testing.T) {
8283
now := time.Now()
8384

85+
structuredEvent := event.Event{
86+
Context: event.EventContextV03{
87+
Type: "unit.test.client",
88+
Source: *types.ParseURIRef("/unit/test/client"),
89+
Time: &types.Timestamp{Time: now},
90+
ID: "AABBCCDDEE",
91+
}.AsV03(),
92+
}
93+
_ = structuredEvent.SetData(event.ApplicationJSON, &map[string]interface{}{
94+
"sq": 42,
95+
"msg": "hello",
96+
})
97+
8498
testCases := map[string]struct {
8599
c func(target string) client.Client
86100
event event.Event
@@ -121,32 +135,19 @@ func TestClientSend(t *testing.T) {
121135
},
122136
},
123137
"structured simple v0.3": {
124-
c: simpleStructuredClient,
125-
event: func() event.Event {
126-
e := event.Event{
127-
Context: event.EventContextV03{
128-
Type: "unit.test.client",
129-
Source: *types.ParseURIRef("/unit/test/client"),
130-
Time: &types.Timestamp{Time: now},
131-
ID: "AABBCCDDEE",
132-
}.AsV03(),
133-
}
134-
_ = e.SetData(event.ApplicationJSON, &map[string]interface{}{
135-
"sq": 42,
136-
"msg": "hello",
137-
})
138-
return e
139-
}(),
138+
c: simpleStructuredClient,
139+
event: structuredEvent,
140140
resp: &http.Response{
141141
StatusCode: http.StatusAccepted,
142142
},
143143
want: &requestValidation{
144144
Headers: map[string][]string{
145145
"content-type": {"application/cloudevents+json"},
146146
},
147-
Body: []byte(fmt.Sprintf(`{"data":{"msg":"hello","sq":42},"datacontenttype":"application/json","id":"AABBCCDDEE","source":"/unit/test/client","specversion":"0.3","time":%q,"type":"unit.test.client"}`,
148-
now.UTC().Format(time.RFC3339Nano)),
149-
),
147+
Body: func() []byte {
148+
b, _ := json.Marshal(structuredEvent)
149+
return b
150+
}(),
150151
},
151152
},
152153
}

0 commit comments

Comments
 (0)