@@ -7,24 +7,39 @@ import path from "path";
7
7
import fs from "fs" ;
8
8
9
9
import { expect } from "chai" ;
10
- import { CloudEvent , ValidationError , Version } from "../../src" ;
10
+ import { CloudEvent , CloudEventV1 , ValidationError , Version } from "../../src" ;
11
11
import { asBase64 } from "../../src/event/validation" ;
12
12
13
13
const type = "org.cncf.cloudevents.example" ;
14
14
const source = "http://unit.test" ;
15
15
const id = "b46cf653-d48a-4b90-8dfa-355c01061361" ;
16
16
17
- const fixture = {
17
+ const fixture = Object . freeze ( {
18
18
id,
19
19
specversion : Version . V1 ,
20
20
source,
21
21
type,
22
- data : `"some data"` ,
23
- } ;
22
+ data : `"some data"`
23
+ } ) ;
24
24
25
25
const imageData = new Uint32Array ( fs . readFileSync ( path . join ( process . cwd ( ) , "test" , "integration" , "ce.png" ) ) ) ;
26
26
const image_base64 = asBase64 ( imageData ) ;
27
27
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
+
28
43
describe ( "A CloudEvent" , ( ) => {
29
44
it ( "Can be constructed with a typed Message" , ( ) => {
30
45
const ce = new CloudEvent ( fixture ) ;
@@ -78,6 +93,58 @@ describe("A CloudEvent", () => {
78
93
new CloudEvent ( { ExtensionWithCaps : "extension value" , ...fixture } ) ;
79
94
} ) . throw ( "invalid extension name" ) ;
80
95
} ) ;
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
+ } ) ;
81
148
} ) ;
82
149
83
150
describe ( "A 1.0 CloudEvent" , ( ) => {
0 commit comments