1
1
var expect = require ( "chai" ) . expect ;
2
2
var Cloudevent = require ( "../index.js" ) ;
3
3
4
+ const type = "com.github.pull.create" ;
5
+ const source = "urn:event:from:myapi/resourse/123" ;
6
+ const time = new Date ( ) ;
7
+ const schemaurl = "http://example.com/registry/myschema.json" ;
8
+ const contenttype = "application/json" ;
9
+ const data = { } ;
10
+ const extensions = { } ;
11
+
4
12
var cloudevent = new Cloudevent ( Cloudevent . specs [ '0.2' ] )
5
- . type ( "com.github.pull.create" )
6
- . source ( "urn:event:from:myapi/resourse/123" ) ;
13
+ . type ( type )
14
+ . source ( source ) ;
7
15
8
16
describe ( "CloudEvents Spec 0.2 - JavaScript SDK" , ( ) => {
9
17
@@ -27,6 +35,91 @@ describe("CloudEvents Spec 0.2 - JavaScript SDK", () => {
27
35
} ) ;
28
36
} ) ;
29
37
38
+ describe ( "Optional context attributes" , ( ) => {
39
+ it ( "contains 'time'" , ( ) => {
40
+ cloudevent . time ( time ) ;
41
+ expect ( cloudevent . format ( ) ) . to . have . property ( 'time' ) ;
42
+ } ) ;
43
+
44
+ it ( "contains 'schemaurl'" , ( ) => {
45
+ cloudevent . schemaurl ( schemaurl ) ;
46
+ expect ( cloudevent . format ( ) ) . to . have . property ( 'schemaurl' ) ;
47
+ } ) ;
48
+
49
+ it ( "contains 'contenttype'" , ( ) => {
50
+ cloudevent . contenttype ( contenttype ) ;
51
+ expect ( cloudevent . format ( ) ) . to . have . property ( 'contenttype' ) ;
52
+ } ) ;
53
+
54
+ it ( "contains 'data'" , ( ) => {
55
+ cloudevent . data ( data ) ;
56
+ expect ( cloudevent . format ( ) ) . to . have . property ( 'data' ) ;
57
+ } ) ;
58
+
59
+ it ( "contains 'extension1'" , ( ) => {
60
+ cloudevent . addExtension ( "extension1" , "value1" ) ;
61
+ expect ( cloudevent . format ( ) ) . to . have . property ( 'extension1' ) ;
62
+ } ) ;
63
+
64
+ it ( "'extension2' should have value equals to 'value1'" , ( ) => {
65
+ cloudevent . addExtension ( "extension2" , "value2" ) ;
66
+ expect ( cloudevent . format ( ) [ 'extension2' ] ) . to . equal ( 'value2' ) ;
67
+ } ) ;
68
+ } ) ;
69
+
70
+ describe ( "The Constraints check" , ( ) => {
71
+ describe ( "'type'" , ( ) => {
72
+ it ( "should throw an error when is an empty string" , ( ) => {
73
+ cloudevent . type ( "" ) ;
74
+ expect ( cloudevent . format . bind ( cloudevent ) )
75
+ . to
76
+ . throw ( "'type' is invalid" ) ;
77
+ } ) ;
78
+
79
+ it ( "must be a non-empty string" , ( ) => {
80
+ cloudevent . type ( type ) ;
81
+ cloudevent . format ( ) ;
82
+ } ) ;
83
+
84
+ it ( "should be prefixed with a reverse-DNS name" , ( ) => {
85
+ //TODO how to assert it?
86
+ } ) ;
87
+ } ) ;
88
+
89
+ describe ( "'specversion'" , ( ) => {
90
+ it ( "compliant event producers must use a value of '0.2'" , ( ) => {
91
+ expect ( cloudevent . format ( ) [ 'specversion' ] ) . to . equal ( "0.2" ) ;
92
+ } ) ;
93
+
94
+ it ( "should throw an error when is an empty string" , ( ) => {
95
+ cloudevent . spec . payload . specversion = "" ;
96
+ expect ( cloudevent . format . bind ( cloudevent ) )
97
+ . to
98
+ . throw ( "'specversion' is invalid" ) ;
99
+ cloudevent . spec . payload . specversion = "0.2" ;
100
+ } ) ;
101
+ } ) ;
102
+
103
+ describe ( "'id'" , ( ) => {
104
+ it ( "should throw an error when is an empty string" , ( ) => {
105
+ cloudevent . id ( "" ) ;
106
+ expect ( cloudevent . format . bind ( cloudevent ) )
107
+ . to
108
+ . throw ( "'id' is invalid" ) ;
109
+ } ) ;
110
+ it ( "must be a non-empty string" , ( ) => {
111
+ cloudevent . id ( "my.id-0x0090" ) ;
112
+ cloudevent . format ( ) ;
113
+ } ) ;
114
+ } ) ;
115
+
116
+ describe ( "'time'" , ( ) => {
117
+ it ( "must adhere to the format specified in RFC 3339" , ( ) => {
118
+ cloudevent . time ( time ) ;
119
+ expect ( cloudevent . format ( ) [ 'time' ] ) . to . equal ( time . toISOString ( ) ) ;
120
+ } ) ;
121
+ } ) ;
122
+ } ) ;
30
123
} ) ;
31
124
32
125
} ) ;
0 commit comments