1
+ import path from "path" ;
2
+ import fs from "fs" ;
3
+
1
4
import { expect } from "chai" ;
2
5
import { CloudEvent , CONSTANTS , Version } from "../../src" ;
3
6
import { asBase64 } from "../../src/event/validation" ;
@@ -16,7 +19,6 @@ const data = {
16
19
17
20
// Attributes for v03 events
18
21
const schemaurl = "https://cloudevents.io/schema.json" ;
19
- const datacontentencoding = "base64" ;
20
22
21
23
const ext1Name = "extension1" ;
22
24
const ext1Value = "foobar" ;
@@ -25,7 +27,11 @@ const ext2Value = "acme";
25
27
26
28
// Binary data as base64
27
29
const dataBinary = Uint32Array . from ( JSON . stringify ( data ) , ( c ) => c . codePointAt ( 0 ) as number ) ;
28
- const data_base64 = asBase64 ( dataBinary ) ;
30
+
31
+ // Since the above is a special case (string as binary), let's test
32
+ // with a real binary file one is likely to encounter in the wild
33
+ const imageData = new Uint32Array ( fs . readFileSync ( path . join ( process . cwd ( ) , "test" , "integration" , "ce.png" ) ) ) ;
34
+ const image_base64 = asBase64 ( imageData ) ;
29
35
30
36
describe ( "HTTP transport" , ( ) => {
31
37
it ( "Can detect invalid CloudEvent Messages" , ( ) => {
@@ -45,6 +51,7 @@ describe("HTTP transport", () => {
45
51
new CloudEvent ( {
46
52
source : "/message-test" ,
47
53
type : "example" ,
54
+ data,
48
55
} ) ,
49
56
) ;
50
57
expect ( HTTP . isEvent ( message ) ) . to . be . true ;
@@ -144,23 +151,47 @@ describe("HTTP transport", () => {
144
151
expect ( event ) . to . deep . equal ( fixture ) ;
145
152
} ) ;
146
153
147
- it ( "Supports Base-64 encoded data in structured messages" , ( ) => {
148
- const event = fixture . cloneWith ( { data : dataBinary } ) ;
149
- expect ( event . data_base64 ) . to . equal ( data_base64 ) ;
150
- expect ( event . data ) . to . equal ( dataBinary ) ;
154
+ it ( "Converts binary data to base64 when serializing structured messages" , ( ) => {
155
+ const event = fixture . cloneWith ( { data : imageData , datacontenttype : "image/png" } ) ;
156
+ expect ( event . data ) . to . equal ( imageData ) ;
151
157
const message = HTTP . structured ( event ) ;
158
+ const messageBody = JSON . parse ( message . body as string ) ;
159
+ expect ( messageBody . data_base64 ) . to . equal ( image_base64 ) ;
160
+ } ) ;
161
+
162
+ it ( "Converts base64 encoded data to binary when deserializing structured messages" , ( ) => {
163
+ const message = HTTP . structured ( fixture . cloneWith ( { data : imageData , datacontenttype : "image/png" } ) ) ;
164
+ const eventDeserialized = HTTP . toEvent ( message ) ;
165
+ expect ( eventDeserialized . data ) . to . deep . equal ( imageData ) ;
166
+ expect ( eventDeserialized . data_base64 ) . to . equal ( image_base64 ) ;
167
+ } ) ;
168
+
169
+ it ( "Parses binary data from structured messages with content type application/json" , ( ) => {
170
+ const message = HTTP . structured ( fixture . cloneWith ( { data : dataBinary } ) ) ;
152
171
const eventDeserialized = HTTP . toEvent ( message ) ;
153
172
expect ( eventDeserialized . data ) . to . deep . equal ( { foo : "bar" } ) ;
173
+ expect ( eventDeserialized . data_base64 ) . to . be . undefined ;
174
+ } ) ;
175
+
176
+ it ( "Converts base64 encoded data to binary when deserializing binary messages" , ( ) => {
177
+ const message = HTTP . binary ( fixture . cloneWith ( { data : imageData , datacontenttype : "image/png" } ) ) ;
178
+ const eventDeserialized = HTTP . toEvent ( message ) ;
179
+ expect ( eventDeserialized . data ) . to . deep . equal ( imageData ) ;
180
+ expect ( eventDeserialized . data_base64 ) . to . equal ( image_base64 ) ;
154
181
} ) ;
155
182
156
- it ( "Supports Base-64 encoded data in binary messages" , ( ) => {
183
+ it ( "Keeps binary data binary when serializing binary messages" , ( ) => {
157
184
const event = fixture . cloneWith ( { data : dataBinary } ) ;
158
- expect ( event . data_base64 ) . to . equal ( data_base64 ) ;
159
185
expect ( event . data ) . to . equal ( dataBinary ) ;
160
186
const message = HTTP . binary ( event ) ;
161
187
expect ( message . body ) . to . equal ( dataBinary ) ;
188
+ } ) ;
189
+
190
+ it ( "Parses binary data from binary messages with content type application/json" , ( ) => {
191
+ const message = HTTP . binary ( fixture . cloneWith ( { data : dataBinary } ) ) ;
162
192
const eventDeserialized = HTTP . toEvent ( message ) ;
163
- expect ( eventDeserialized . data ) . to . equal ( dataBinary ) ;
193
+ expect ( eventDeserialized . data ) . to . deep . equal ( { foo : "bar" } ) ;
194
+ expect ( eventDeserialized . data_base64 ) . to . be . undefined ;
164
195
} ) ;
165
196
} ) ;
166
197
@@ -223,28 +254,35 @@ describe("HTTP transport", () => {
223
254
expect ( event ) . to . deep . equal ( fixture ) ;
224
255
} ) ;
225
256
226
- it ( "Supports Base-64 encoded data in structured messages" , ( ) => {
227
- const event = fixture . cloneWith ( { data : data_base64 , datacontentencoding } ) ;
257
+ it ( "Converts binary data to base64 when serializing structured messages" , ( ) => {
258
+ const event = fixture . cloneWith ( { data : imageData , datacontenttype : "image/png" } ) ;
259
+ expect ( event . data ) . to . equal ( imageData ) ;
228
260
const message = HTTP . structured ( event ) ;
229
- expect ( JSON . parse ( message . body as string ) . data ) . to . equal ( data_base64 ) ;
230
- // An incoming event with datacontentencoding set to base64,
231
- // and encoded data, should decode the data before setting
232
- // the .data property on the event
261
+ const messageBody = JSON . parse ( message . body as string ) ;
262
+ expect ( messageBody . data_base64 ) . to . equal ( image_base64 ) ;
263
+ } ) ;
264
+
265
+ it ( "Converts base64 encoded data to binary when deserializing structured messages" , ( ) => {
266
+ // Creating an event with binary data automatically produces base64 encoded data
267
+ // which is then set as the 'data' attribute on the message body
268
+ const message = HTTP . structured ( fixture . cloneWith ( { data : imageData , datacontenttype : "image/png" } ) ) ;
233
269
const eventDeserialized = HTTP . toEvent ( message ) ;
234
- expect ( eventDeserialized . data ) . to . deep . equal ( { foo : "bar" } ) ;
235
- expect ( eventDeserialized . datacontentencoding ) . to . be . undefined ;
270
+ expect ( eventDeserialized . data ) . to . deep . equal ( imageData ) ;
271
+ expect ( eventDeserialized . data_base64 ) . to . equal ( image_base64 ) ;
236
272
} ) ;
237
273
238
- it ( "Supports Base-64 encoded data in binary messages" , ( ) => {
239
- const event = fixture . cloneWith ( { data : data_base64 , datacontentencoding } ) ;
240
- const message = HTTP . binary ( event ) ;
241
- expect ( message . body ) . to . equal ( data_base64 ) ;
242
- // An incoming event with datacontentencoding set to base64,
243
- // and encoded data, should decode the data before setting
244
- // the .data property on the event
274
+ it ( "Converts base64 encoded data to binary when deserializing binary messages" , ( ) => {
275
+ const message = HTTP . binary ( fixture . cloneWith ( { data : imageData , datacontenttype : "image/png" } ) ) ;
245
276
const eventDeserialized = HTTP . toEvent ( message ) ;
246
- expect ( eventDeserialized . data ) . to . deep . equal ( { foo : "bar" } ) ;
247
- expect ( eventDeserialized . datacontentencoding ) . to . be . undefined ;
277
+ expect ( eventDeserialized . data ) . to . deep . equal ( imageData ) ;
278
+ expect ( eventDeserialized . data_base64 ) . to . equal ( image_base64 ) ;
279
+ } ) ;
280
+
281
+ it ( "Keeps binary data binary when serializing binary messages" , ( ) => {
282
+ const event = fixture . cloneWith ( { data : dataBinary } ) ;
283
+ expect ( event . data ) . to . equal ( dataBinary ) ;
284
+ const message = HTTP . binary ( event ) ;
285
+ expect ( message . body ) . to . equal ( dataBinary ) ;
248
286
} ) ;
249
287
} ) ;
250
288
} ) ;
0 commit comments