@@ -13,7 +13,7 @@ const data = {
13
13
} ;
14
14
const subject = "subject-x0" ;
15
15
16
- const cloudevent = new CloudEvent ( {
16
+ let cloudevent = new CloudEvent ( {
17
17
specversion : Version . V03 ,
18
18
id,
19
19
source,
@@ -46,8 +46,13 @@ describe("CloudEvents Spec v0.3", () => {
46
46
47
47
describe ( "OPTIONAL Attributes" , ( ) => {
48
48
it ( "Should have 'datacontentencoding'" , ( ) => {
49
- cloudevent . datacontentencoding = Constants . ENCODING_BASE64 ;
49
+ cloudevent = cloudevent . cloneWith ( {
50
+ datacontentencoding : Constants . ENCODING_BASE64 ,
51
+ data : "SSB3YXMgZnVubnkg8J+Ygg==" ,
52
+ } ) ;
50
53
expect ( cloudevent . datacontentencoding ) . to . equal ( Constants . ENCODING_BASE64 ) ;
54
+
55
+ cloudevent = cloudevent . cloneWith ( { datacontentencoding : undefined , data : data } ) ;
51
56
} ) ;
52
57
53
58
it ( "Should have 'datacontenttype'" , ( ) => {
@@ -71,116 +76,117 @@ describe("CloudEvents Spec v0.3", () => {
71
76
} ) ;
72
77
73
78
it ( "Should have the 'extension1'" , ( ) => {
74
- cloudevent . extension1 = "value1" ;
79
+ cloudevent = cloudevent . cloneWith ( { extension1 : "value1" } ) ;
75
80
expect ( cloudevent . extension1 ) . to . equal ( "value1" ) ;
76
81
} ) ;
77
82
} ) ;
78
83
79
84
describe ( "The Constraints check" , ( ) => {
80
85
describe ( "'id'" , ( ) => {
81
- it ( "should throw an error when is absent " , ( ) => {
82
- delete cloudevent . id ;
83
- expect ( cloudevent . validate . bind ( cloudevent ) ) . to . throw ( ValidationError , "invalid payload" ) ;
84
- cloudevent . id = id ;
86
+ it ( "should throw an error when trying to remove " , ( ) => {
87
+ expect ( ( ) => {
88
+ delete cloudevent . id ;
89
+ } ) . to . throw ( TypeError ) ;
85
90
} ) ;
86
91
87
- it ( "should throw an error when is empty" , ( ) => {
88
- cloudevent . id = "" ;
89
- expect ( cloudevent . validate . bind ( cloudevent ) ) . to . throw ( ValidationError , "invalid payload" ) ;
90
- cloudevent . id = id ;
92
+ it ( "defaut ID create when an empty string" , ( ) => {
93
+ cloudevent = cloudevent . cloneWith ( { id : "" } ) ;
94
+ expect ( cloudevent . id . length ) . to . be . greaterThan ( 0 ) ;
91
95
} ) ;
92
96
} ) ;
93
97
94
98
describe ( "'source'" , ( ) => {
95
- it ( "should throw an error when is absent " , ( ) => {
96
- delete cloudevent . source ;
97
- expect ( cloudevent . validate . bind ( cloudevent ) ) . to . throw ( ValidationError , "invalid payload" ) ;
98
- cloudevent . source = source ;
99
+ it ( "should throw an error when trying to remove " , ( ) => {
100
+ expect ( ( ) => {
101
+ delete cloudevent . source ;
102
+ } ) . to . throw ( TypeError ) ;
99
103
} ) ;
100
104
} ) ;
101
105
102
106
describe ( "'specversion'" , ( ) => {
103
- it ( "should throw an error when is absent " , ( ) => {
104
- delete cloudevent . specversion ;
105
- expect ( cloudevent . validate . bind ( cloudevent ) ) . to . throw ( ValidationError , "invalid payload" ) ;
106
- cloudevent . specversion = Version . V03 ;
107
+ it ( "should throw an error when trying to remove " , ( ) => {
108
+ expect ( ( ) => {
109
+ delete cloudevent . specversion ;
110
+ } ) . to . throw ( TypeError ) ;
107
111
} ) ;
108
112
} ) ;
109
113
110
114
describe ( "'type'" , ( ) => {
111
- it ( "should throw an error when is absent " , ( ) => {
112
- delete cloudevent . type ;
113
- expect ( cloudevent . validate . bind ( cloudevent ) ) . to . throw ( ValidationError , "invalid payload" ) ;
114
- cloudevent . type = type ;
115
+ it ( "should throw an error when trying to remove " , ( ) => {
116
+ expect ( ( ) => {
117
+ delete cloudevent . type ;
118
+ } ) . to . throw ( TypeError ) ;
115
119
} ) ;
116
120
117
121
it ( "should throw an error when is an empty string" , ( ) => {
118
- cloudevent . type = "" ;
119
- expect ( cloudevent . validate . bind ( cloudevent ) ) . to . throw ( ValidationError , "invalid payload" ) ;
120
- cloudevent . type = type ;
122
+ expect ( ( ) => {
123
+ cloudevent . cloneWith ( { type : "" } ) ;
124
+ } ) . to . throw ( ValidationError , "invalid payload" ) ;
121
125
} ) ;
122
126
123
127
it ( "must be a non-empty string" , ( ) => {
124
- cloudevent . type = type ;
128
+ cloudevent . cloneWith ( { type : type } ) ;
125
129
expect ( cloudevent . type ) . to . equal ( type ) ;
126
130
} ) ;
127
131
} ) ;
128
132
129
133
describe ( "'datacontentencoding'" , ( ) => {
130
134
it ( "should throw an error when is a unsupported encoding" , ( ) => {
131
- cloudevent . data = "Y2xvdWRldmVudHMK" ;
132
- cloudevent . datacontentencoding = Mode . BINARY ;
133
- expect ( cloudevent . validate . bind ( cloudevent ) ) . to . throw ( ValidationError , "invalid payload" ) ;
134
- delete cloudevent . datacontentencoding ;
135
- cloudevent . data = data ;
135
+ expect ( ( ) => {
136
+ cloudevent . cloneWith ( { data : "Y2xvdWRldmVudHMK" , datacontentencoding : Mode . BINARY } ) ;
137
+ } ) . to . throw ( ValidationError , "invalid payload" ) ;
138
+
139
+ cloudevent . cloneWith ( { data : data , datacontentencoding : undefined } ) ;
136
140
} ) ;
137
141
138
142
it ( "should throw an error when 'data' does not carry base64" , ( ) => {
139
- cloudevent . data = "no base 64 value" ;
140
- cloudevent . datacontentencoding = Constants . ENCODING_BASE64 ;
141
- cloudevent . datacontenttype = "text/plain" ;
142
-
143
- expect ( cloudevent . validate . bind ( cloudevent ) ) . to . throw ( ValidationError , "invalid payload" ) ;
144
-
145
- delete cloudevent . datacontentencoding ;
146
- cloudevent . data = data ;
143
+ expect ( ( ) => {
144
+ cloudevent . cloneWith ( {
145
+ data : "no base 64 value" ,
146
+ datacontentencoding : Constants . ENCODING_BASE64 ,
147
+ datacontenttype : "text/plain" ,
148
+ } ) ;
149
+ } ) . to . throw ( ValidationError , "invalid payload" ) ;
150
+
151
+ cloudevent . cloneWith ( {
152
+ data : data ,
153
+ datacontentencoding : undefined ,
154
+ } ) ;
147
155
} ) ;
148
156
149
157
it ( "should accept when 'data' is a string" , ( ) => {
150
- cloudevent . data = "Y2xvdWRldmVudHMK" ;
151
- cloudevent . datacontentencoding = Constants . ENCODING_BASE64 ;
158
+ cloudevent . cloneWith ( { data : "Y2xvdWRldmVudHMK" , datacontentencoding : Constants . ENCODING_BASE64 } ) ;
152
159
expect ( cloudevent . validate ( ) ) . to . be . true ;
153
- delete cloudevent . datacontentencoding ;
154
- cloudevent . data = data ;
160
+ cloudevent . cloneWith ( { data : data , datacontentencoding : undefined } ) ;
155
161
} ) ;
156
162
} ) ;
157
163
158
164
describe ( "'data'" , ( ) => {
159
165
it ( "should maintain the type of data when no data content type" , ( ) => {
160
- delete cloudevent . datacontenttype ;
166
+ cloudevent = cloudevent . cloneWith ( { datacontenttype : undefined } ) ;
161
167
cloudevent . data = JSON . stringify ( data ) ;
162
168
163
169
expect ( typeof cloudevent . data ) . to . equal ( "string" ) ;
164
- cloudevent . datacontenttype = Constants . MIME_JSON ;
165
170
} ) ;
166
171
167
172
it ( "should convert data with stringified json to a json object" , ( ) => {
168
- cloudevent . datacontenttype = Constants . MIME_JSON ;
173
+ cloudevent = cloudevent . cloneWith ( { datacontenttype : Constants . MIME_JSON } ) ;
169
174
cloudevent . data = JSON . stringify ( data ) ;
170
175
expect ( cloudevent . data ) . to . deep . equal ( data ) ;
171
176
} ) ;
172
177
} ) ;
173
178
174
179
describe ( "'subject'" , ( ) => {
175
180
it ( "should throw an error when is an empty string" , ( ) => {
176
- cloudevent . subject = "" ;
177
- expect ( cloudevent . validate . bind ( cloudevent ) ) . to . throw ( ValidationError , "invalid payload" ) ;
178
- cloudevent . subject = subject ;
181
+ expect ( ( ) => {
182
+ cloudevent . cloneWith ( { subject : "" } ) ;
183
+ } ) . to . throw ( ValidationError ) ;
179
184
} ) ;
180
185
} ) ;
181
186
182
187
describe ( "'time'" , ( ) => {
183
188
it ( "must adhere to the format specified in RFC 3339" , ( ) => {
189
+ cloudevent = cloudevent . cloneWith ( { time : time } ) ;
184
190
expect ( cloudevent . time ) . to . equal ( time . toISOString ( ) ) ;
185
191
} ) ;
186
192
} ) ;
0 commit comments