@@ -19,128 +19,75 @@ const data = {
19
19
foo : "bar"
20
20
} ;
21
21
22
+ const un = new Unmarshaller ( ) ;
23
+
22
24
describe ( "HTTP Transport Binding Unmarshaller for CloudEvents v0.3" , ( ) => {
23
25
it ( "Throw error when payload is null" , ( ) => {
24
- // setup
25
- const payload = null ;
26
- const un = new Unmarshaller ( ) ;
27
-
28
- // act and assert
29
- return un . unmarshall ( payload )
30
- . then ( ( ) => { throw new Error ( "failed" ) ; } )
31
- . catch ( ( err ) =>
32
- expect ( err . message ) . to . equal ( "payload is null or undefined" ) ) ;
26
+ expect ( ( ) => un . unmarshall ( null ) ) . to . throw ( "payload is null or undefined" ) ;
33
27
} ) ;
34
28
35
29
it ( "Throw error when headers is null" , ( ) => {
36
- // setup
37
- const payload = { } ;
38
- const headers = null ;
39
- const un = new Unmarshaller ( ) ;
40
-
41
- // act and assert
42
- return un . unmarshall ( payload , headers )
43
- . then ( ( ) => { throw new Error ( "failed" ) ; } )
44
- . catch ( ( err ) =>
45
- expect ( err . message ) . to . equal ( "headers is null or undefined" ) ) ;
30
+ expect ( ( ) => un . unmarshall ( { } ) ) . to . throw ( "headers is null or undefined" ) ;
31
+ expect ( ( ) => un . unmarshall ( { } , null ) ) . to
32
+ . throw ( "headers is null or undefined" ) ;
46
33
} ) ;
47
34
48
35
it ( "Throw error when there is no content-type header" , ( ) => {
49
- // setup
50
- const payload = { } ;
51
- const headers = { } ;
52
- const un = new Unmarshaller ( ) ;
53
-
54
- // act and assert
55
- un . unmarshall ( payload , headers )
56
- . then ( ( ) => { throw new Error ( "failed" ) ; } )
57
- . catch ( ( err ) =>
58
- expect ( err . message ) . to . equal ( "content-type header not found" ) ) ;
36
+ expect ( ( ) => un . unmarshall ( { } , { } ) ) . to
37
+ . throw ( "content-type header not found" ) ;
59
38
} ) ;
60
39
61
40
it ( "Throw error when content-type is not allowed" , ( ) => {
62
- // setup
63
- const payload = { } ;
64
41
const headers = {
65
42
"content-type" : "text/xml"
66
43
} ;
67
- const un = new Unmarshaller ( ) ;
68
-
69
- // act and assert
70
- un . unmarshall ( payload , headers )
71
- . then ( ( ) => { throw new Error ( "failed" ) ; } )
72
- . catch ( ( err ) =>
73
- expect ( err . message ) . to . equal ( "content type not allowed" ) ) ;
44
+ expect ( ( ) => un . unmarshall ( { } , headers ) ) . to
45
+ . throw ( "content type not allowed" ) ;
74
46
} ) ;
75
47
76
48
describe ( "Structured" , ( ) => {
77
49
it ( "Throw error when has not allowed mime" , ( ) => {
78
50
// setup
79
- const payload = { } ;
80
51
const headers = {
81
52
"content-type" : "application/cloudevents+zip"
82
53
} ;
83
- const un = new Unmarshaller ( ) ;
84
54
85
55
// act and assert
86
- un . unmarshall ( payload , headers )
87
- . then ( ( ) => { throw new Error ( "failed" ) ; } )
88
- . catch ( ( err ) =>
89
- expect ( err . message ) . to . equal ( "structured+type not allowed" ) ) ;
56
+ expect ( ( ) => un . unmarshall ( { } , headers ) ) . to
57
+ . throw ( "structured+type not allowed" ) ;
90
58
} ) ;
91
59
92
60
it ( "Throw error when the event does not follow the spec 0.3" , ( ) => {
93
- // setup
94
61
const payload =
95
- new v03 . CloudEvent ( v03 . Spec )
96
- . type ( type )
97
- . source ( source )
98
- . dataContentType ( ceContentType )
62
+ new CloudEvent ( v03 . Spec )
99
63
. time ( now )
100
- . schemaurl ( schemaurl )
101
- . data ( data )
102
64
. toString ( ) ;
103
65
104
66
const headers = {
105
67
"content-type" : "application/cloudevents+json"
106
68
} ;
107
69
108
- const un = new Unmarshaller ( ) ;
109
-
110
- // act and assert
111
- un . unmarshall ( payload , headers )
112
- . then ( ( ) => { throw new Error ( "failed" ) ; } )
113
- . catch ( ( err ) =>
114
- expect ( err . message ) . to . equal ( "invalid payload" ) ) ;
70
+ expect ( ( ) => un . unmarshall ( payload , headers ) ) . to
71
+ . throw ( TypeError ) ;
115
72
} ) ;
116
73
117
74
it ( "Should accept event that follow the spec 0.3" , ( ) => {
118
- // setup
119
75
const payload =
120
76
new CloudEvent ( v03 . Spec )
121
77
. type ( type )
78
+ . data ( data )
122
79
. source ( source )
123
80
. dataContentType ( ceContentType )
124
81
. time ( now )
125
82
. schemaurl ( schemaurl )
126
83
. subject ( subject )
127
- . data ( data )
128
- . toString ( ) ;
84
+ . format ( ) ;
129
85
130
86
const headers = {
131
87
"content-type" : "application/cloudevents+json"
132
88
} ;
133
-
134
- const un = new Unmarshaller ( ) ;
135
-
136
- // act and assert
137
- return un . unmarshall ( payload , headers )
138
- . then ( ( actual ) =>
139
- expect ( actual ) . to . be . an ( "object" ) )
140
- . catch ( ( err ) => {
141
- console . error ( err ) ;
142
- throw err ;
143
- } ) ;
89
+ const event = un . unmarshall ( payload , headers ) ;
90
+ expect ( event instanceof CloudEvent ) . to . equal ( true ) ;
144
91
} ) ;
145
92
146
93
it ( "Should parse 'data' stringfied json to json object" , ( ) => {
@@ -160,17 +107,8 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
160
107
"content-type" : "application/cloudevents+json"
161
108
} ;
162
109
163
- const un = new Unmarshaller ( ) ;
164
-
165
- // act and assert
166
- return un . unmarshall ( payload , headers )
167
- . then ( ( actual ) => {
168
- expect ( actual . getData ( ) ) . to . deep . equal ( data ) ;
169
- } )
170
- . catch ( ( err ) => {
171
- console . error ( err ) ;
172
- throw err ;
173
- } ) ;
110
+ const event = un . unmarshall ( payload , headers ) ;
111
+ expect ( event . getData ( ) ) . to . deep . equal ( data ) ;
174
112
} ) ;
175
113
} ) ;
176
114
@@ -190,13 +128,8 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
190
128
[ HEADER_CONTENT_TYPE ] : "text/html"
191
129
} ;
192
130
193
- const un = new Unmarshaller ( ) ;
194
-
195
- // act and assert
196
- un . unmarshall ( payload , attributes )
197
- . then ( ( ) => { throw new Error ( "failed" ) ; } )
198
- . catch ( ( err ) =>
199
- expect ( err . message ) . to . equal ( "content type not allowed" ) ) ;
131
+ expect ( ( ) => un . unmarshall ( payload , attributes ) ) . to
132
+ . throw ( "content type not allowed" ) ;
200
133
} ) ;
201
134
202
135
it ( "Throw error when the event does not follow the spec 0.3" , ( ) => {
@@ -214,13 +147,8 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
214
147
[ HEADER_CONTENT_TYPE ] : "application/json"
215
148
} ;
216
149
217
- const un = new Unmarshaller ( ) ;
218
-
219
- // act and assert
220
- un . unmarshall ( payload , attributes )
221
- . then ( ( ) => { throw new Error ( "failed" ) ; } )
222
- . catch ( ( err ) =>
223
- expect ( err . message ) . to . not . empty ) ;
150
+ expect ( ( ) => un . unmarshall ( payload , attributes ) ) . to
151
+ . throw ( "header 'ce-specversion' not found" ) ;
224
152
} ) ;
225
153
226
154
it ( "No error when all attributes are in place" , ( ) => {
@@ -238,11 +166,8 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
238
166
[ HEADER_CONTENT_TYPE ] : "application/json"
239
167
} ;
240
168
241
- const un = new Unmarshaller ( ) ;
242
-
243
- // act and assert
244
- un . unmarshall ( payload , attributes )
245
- . then ( ( actual ) => expect ( actual ) . to . be . an ( "object" ) ) ;
169
+ const event = un . unmarshall ( payload , attributes ) ;
170
+ expect ( event instanceof CloudEvent ) . to . equal ( true ) ;
246
171
} ) ;
247
172
248
173
it ( "Throw error when 'ce-datacontentencoding' is not allowed" , ( ) => {
@@ -260,14 +185,8 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
260
185
[ BINARY_HEADERS_03 . CONTENT_ENCONDING ] : "binary"
261
186
} ;
262
187
263
- const un = new Unmarshaller ( ) ;
264
-
265
- // act and assert
266
- return un . unmarshall ( payload , attributes )
267
- . then ( ( ) => { throw new Error ( "failed" ) ; } )
268
- . catch ( ( err ) => {
269
- expect ( err . message ) . to . equal ( "unsupported datacontentencoding" ) ;
270
- } ) ;
188
+ expect ( ( ) => un . unmarshall ( payload , attributes ) ) . to
189
+ . throw ( "unsupported datacontentencoding" ) ;
271
190
} ) ;
272
191
273
192
it ( "No error when 'ce-datacontentencoding' is base64" , ( ) => {
@@ -288,15 +207,8 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
288
207
[ BINARY_HEADERS_03 . CONTENT_ENCONDING ] : "base64"
289
208
} ;
290
209
291
- const un = new Unmarshaller ( ) ;
292
-
293
- // act and assert
294
- return un . unmarshall ( payload , attributes )
295
- . then ( ( actual ) => expect ( actual . getData ( ) ) . to . deep . equal ( expected ) )
296
- . catch ( ( err ) => {
297
- console . error ( err ) ;
298
- throw err ;
299
- } ) ;
210
+ const event = un . unmarshall ( payload , attributes ) ;
211
+ expect ( event . getData ( ) ) . to . deep . equal ( expected ) ;
300
212
} ) ;
301
213
} ) ;
302
214
} ) ;
0 commit comments