1
1
const expect = require ( "chai" ) . expect ;
2
2
const Spec03 = require ( "../lib/specs/spec_0_3.js" ) ;
3
3
const { CloudEvent } = require ( "../index.js" ) ;
4
+ const {
5
+ MIME_JSON ,
6
+ ENCODING_BASE64 ,
7
+ SPEC_V03
8
+ } = require ( "../lib/bindings/http/constants.js" ) ;
4
9
5
10
const id = "97699ec2-a8d9-47c1-bfa0-ff7aa526f838" ;
6
11
const type = "com.github.pull.create" ;
7
12
const source = "urn:event:from:myapi/resourse/123" ;
8
13
const time = new Date ( ) ;
9
14
const schemaurl = "http://example.com/registry/myschema.json" ;
10
- const dataContentEncoding = "base64" ;
11
- const dataContentType = "application/json" ;
12
15
const data = {
13
16
much : "wow"
14
17
} ;
@@ -19,7 +22,7 @@ const cloudevent =
19
22
. id ( id )
20
23
. source ( source )
21
24
. type ( type )
22
- . dataContentType ( dataContentType )
25
+ . dataContentType ( MIME_JSON )
23
26
. schemaurl ( schemaurl )
24
27
. subject ( subject )
25
28
. time ( time )
@@ -36,7 +39,7 @@ describe("CloudEvents Spec v0.3", () => {
36
39
} ) ;
37
40
38
41
it ( "Should have 'specversion'" , ( ) => {
39
- expect ( cloudevent . getSpecversion ( ) ) . to . equal ( "0.3" ) ;
42
+ expect ( cloudevent . getSpecversion ( ) ) . to . equal ( SPEC_V03 ) ;
40
43
} ) ;
41
44
42
45
it ( "Should have 'type'" , ( ) => {
@@ -46,14 +49,14 @@ describe("CloudEvents Spec v0.3", () => {
46
49
47
50
describe ( "OPTIONAL Attributes" , ( ) => {
48
51
it ( "Should have 'datacontentencoding'" , ( ) => {
49
- cloudevent . dataContentEncoding ( dataContentEncoding ) ;
52
+ cloudevent . dataContentEncoding ( ENCODING_BASE64 ) ;
50
53
expect ( cloudevent . spec . payload . datacontentencoding )
51
- . to . equal ( dataContentEncoding ) ;
54
+ . to . equal ( ENCODING_BASE64 ) ;
52
55
delete cloudevent . spec . payload . datacontentencoding ;
53
56
} ) ;
54
57
55
58
it ( "Should have 'datacontenttype'" , ( ) => {
56
- expect ( cloudevent . getDataContentType ( ) ) . to . equal ( dataContentType ) ;
59
+ expect ( cloudevent . getDataContentType ( ) ) . to . equal ( MIME_JSON ) ;
57
60
} ) ;
58
61
59
62
it ( "Should have 'schemaurl'" , ( ) => {
@@ -119,15 +122,15 @@ describe("CloudEvents Spec v0.3", () => {
119
122
expect ( cloudevent . format . bind ( cloudevent ) )
120
123
. to
121
124
. throw ( "invalid payload" ) ;
122
- cloudevent . spec . payload . specversion <
10000
span class="pl-c1">= "0.3" ;
125
+ cloudevent . spec . payload . specversion = SPEC_V03 ;
123
126
} ) ;
124
127
125
128
it ( "should throw an error when is empty" , ( ) => {
126
129
cloudevent . spec . payload . specversion = "" ;
127
130
expect ( cloudevent . format . bind ( cloudevent ) )
128
131
. to
129
132
. throw ( "invalid payload" ) ;
130
- cloudevent . spec . payload . specversion = "0.3" ;
133
+ cloudevent . spec . payload . specversion = SPEC_V03 ;
131
134
} ) ;
132
135
} ) ;
133
136
@@ -170,7 +173,7 @@ describe("CloudEvents Spec v0.3", () => {
170
173
( ) => {
171
174
cloudevent
172
175
. data ( "no base 64 value" )
173
- . dataContentEncoding ( "base64" )
176
+ . dataContentEncoding ( ENCODING_BASE64 )
174
177
. dataContentType ( "text/plain" ) ;
175
178
176
179
expect ( cloudevent . format . bind ( cloudevent ) )
@@ -184,7 +187,7 @@ describe("CloudEvents Spec v0.3", () => {
184
187
it ( "should accept when 'data' is a string" , ( ) => {
185
188
cloudevent
186
189
. data ( "Y2xvdWRldmVudHMK" )
187
- . dataContentEncoding ( "base64" ) ;
190
+ . dataContentEncoding ( ENCODING_BASE64 ) ;
188
191
expect ( cloudevent . format ( ) ) . to . have . property ( "datacontentencoding" ) ;
189
192
delete cloudevent . spec . payload . datacontentencoding ;
190
193
cloudevent . data ( data ) ;
@@ -198,12 +201,12 @@ describe("CloudEvents Spec v0.3", () => {
198
201
. data ( JSON . stringify ( data ) ) ;
199
202
200
203
expect ( typeof cloudevent . getData ( ) ) . to . equal ( "string" ) ;
201
- cloudevent . dataContentType ( dataContentType ) ;
204
+ cloudevent . dataContentType ( MIME_JSON ) ;
202
205
} ) ;
203
206
204
207
it ( "should convert data with stringified json to a json object" , ( ) => {
205
208
cloudevent
206
- . dataContentType ( dataContentType )
209
+ . dataContentType ( MIME_JSON )
207
210
. data ( JSON . stringify ( data ) ) ;
208
211
expect ( cloudevent . getData ( ) ) . to . deep . equal ( data ) ;
209
212
} ) ;
0 commit comments