@@ -7,24 +7,39 @@ import path from "path";
77import fs from "fs" ;
88
99import { expect } from "chai" ;
10- import { CloudEvent , ValidationError , Version } from "../../src" ;
10+ import { CloudEvent , CloudEventV1 , ValidationError , Version } from "../../src" ;
1111import { asBase64 } from "../../src/event/validation" ;
1212
1313const type = "org.cncf.cloudevents.example" ;
1414const source = "http://unit.test" ;
1515const id = "b46cf653-d48a-4b90-8dfa-355c01061361" ;
1616
17- const fixture = {
17+ const fixture = Object . freeze ( {
1818 id,
1919 specversion : Version . V1 ,
2020 source,
2121 type,
22- data : `"some data"` ,
23- } ;
22+ data : `"some data"`
23+ } ) ;
2424
2525const imageData = new Uint32Array ( fs . readFileSync ( path . join ( process . cwd ( ) , "test" , "integration" , "ce.png" ) ) ) ;
2626const image_base64 = asBase64 ( imageData ) ;
2727
28+ // Do not replace this with the assignment of a class instance
29+ // as we just want to test if we can enumerate all explicitly defined fields!
30+ const cloudEventV1InterfaceFields : ( keyof CloudEventV1 < unknown > ) [ ] = Object . keys ( {
31+ id : "" ,
32+ type : "" ,
33+ data : undefined ,
34+ data_base64 : "" ,
35+ source : "" ,
36+ time : "" ,
37+ datacontenttype : "" ,
38+ dataschema : "" ,
39+ specversion : "" ,
40+ subject : ""
41+ } as Required < CloudEventV1 < unknown > > ) ;
42+
2843describe ( "A CloudEvent" , ( ) => {
2944 it ( "Can be constructed with a typed Message" , ( ) => {
3045 const ce = new CloudEvent ( fixture ) ;
@@ -78,6 +93,58 @@ describe("A CloudEvent", () => {
7893 new CloudEvent ( { ExtensionWithCaps : "extension value" , ...fixture } ) ;
7994 } ) . throw ( "invalid extension name" ) ;
8095 } ) ;
96+
97+ it ( "CloudEventV1 interface fields should be enumerable" , ( ) => {
98+ const classInstanceKeys = Object . keys ( new CloudEvent ( { ...fixture } ) ) ;
99+
100+ for ( const key of cloudEventV1InterfaceFields ) {
101+ expect ( classInstanceKeys ) . to . contain ( key ) ;
102+ }
103+ } ) ;
104+
105+ it ( "throws TypeError on trying to set any field value" , ( ) => {
106+ const ce = new CloudEvent ( {
107+ ...fixture ,
108+ mycustomfield : "initialValue"
109+ } ) ;
110+
111+ const keySet = new Set ( [ ...cloudEventV1InterfaceFields , ...Object . keys ( ce ) ] ) ;
112+
113+ expect ( keySet ) . not . to . be . empty ;
114+
115+ for ( const cloudEventKey of keySet ) {
116+ let threw = false ;
117+
118+ try {
119+ ce [ cloudEventKey ] = "newValue" ;
120+ } catch ( err ) {
121+ threw = true ;
122+ expect ( err ) . to . be . instanceOf ( TypeError ) ;
123+ expect ( ( err as TypeError ) . message ) . to . include ( "Cannot assign to read only property" ) ;
124+ }
125+
126+ if ( ! threw ) {
127+ expect . fail ( `Assigning a value to ${ cloudEventKey } did not throw` ) ;
128+ }
129+ }
130+ } ) ;
131+
132+ describe ( "toJSON()" , ( ) => {
133+ it ( "does not return data field if data_base64 field is set to comply with JSON format spec 3.1.1" , ( ) => {
134+ const binaryData = new Uint8Array ( [ 1 , 2 , 3 ] ) ;
135+
136+ const ce = new CloudEvent ( {
137+ ...fixture ,
138+ data : binaryData
139+ } ) ;
140+
141+ expect ( ce . data ) . to . be . equal ( binaryData ) ;
142+
143+ const json = ce . toJSON ( ) ;
144+ expect ( json . data ) . to . not . exist ;
145+ expect ( json . data_base64 ) . to . be . equal ( "AQID" ) ;
146+ } ) ;
147+ } ) ;
81148} ) ;
82149
83150describe ( "A 1.0 CloudEvent" , ( ) => {
0 commit comments