8000 Merge pull request #6 from cloudevents/develop · rinormaloku/sdk-javascript@2cc5999 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2cc5999

Browse files
authored
Merge pull request cloudevents#6 from cloudevents/develop
Develop
2 parents 14eacd5 + c8ccd85 commit 2cc5999

File tree

6 files changed

+136
-24
lines changed

6 files changed

+136
-24
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,6 @@ typings/
7575
# FuseBox cache
7676
.fusebox/
7777

78+
# Vim
79+
*.swp
80+

ext/spec_0_2.json

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
{
2+
"$ref": "#/definitions/event",
3+
"definitions": {
4+
"specversion": {
5+
"type": "string",
6+
"minLength": 1,
7+
"const": "0.2"
8+
},
9+
"contenttype": {
10+
"type": "string"
11+
},
12+
"data": {
13+
"type": [
14+
"object",
15+
"string"
16+
]
17+
},
18+
"event": {
19+
"properties": {
20+
"specversion": {
21+
"$ref": "#/definitions/specversion"
22+
},
23+
"contenttype": {
24+
"$ref": "#/definitions/contenttype"
25+
},
26+
"data": {
27+
"$ref": "#/definitions/data"
28+
},
29+
"id": {
30+
"$ref": "#/definitions/id"
31+
},
32+
"time": {
33+
"$ref": "#/definitions/time"
34+
},
35+
"type": {
36+
"$ref": "#/definitions/type"
37+
},
38+
"extensions": {
39+
"$ref": "#/definitions/extensions"
40+
},
41+
"source": {
42+
"$ref": "#/definitions/source"
43+
}
44+
},
45+
"required": [
46+
"specversion",
47+
"id",
48+
"type",
49+
"source"
50+
],
51+
"type": "object"
52+
},
53+
"id": {
54+
"type": "string",
55+
"minLength": 1
56+
},
57+
"time": {
58+
"format": "date-time",
59+
"type": "string"
60+
},
61+
"type": {
62+
"type": "string",
63+
"minLength": 1
64+
},
65+
"extensions": {
66+
"type": "object"
67+
},
68+
"source": {
69+
"format": "uri-reference",
70+
"type": "string"
71+
}
72+
},
73+
"type": "object" 9E12 ;
74+
}

lib/bindings/http/binary_0_2.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ HTTPBinary.prototype.emit = function(cloudevent){
1717
// Always set stuff in _config
1818
var _headers = _config["headers"];
1919

20-
if(cloudevent.getContenttype()) {
21-
_headers["Content-Type"] = cloudevent.getContenttype();
22-
}
20+
_headers["Content-Type"] = cloudevent.getContenttype();
2321

2422
_headers["ce-type"] = cloudevent.getType();
2523
_headers["ce-specversion"] = cloudevent.getSpecversion();
@@ -36,7 +34,9 @@ HTTPBinary.prototype.emit = function(cloudevent){
3634
// Have extensions?
3735
var exts = cloudevent.getExtensions();
3836
for(var ext in exts){
39-
_headers["ce-" + ext] = exts[ext];
37+
if({}.hasOwnProperty.call(exts, ext)){
38+
_headers["ce-" + ext] = exts[ext];
39+
}
4040
}
4141

4242
// Return the Promise

lib/specs/spec_0_2.js

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
var uuid = require("uuid/v4");
22
var empty = require("is-empty");
3+
var Ajv = require("ajv");
4+
5+
// Reserved attributes names
6+
const reserved = {
7+
type: "type",
8+
specversion: "specversion",
9+
source: "source",
10+
id: "id",
11+
time: "time",
12+
schemaurl: "schemaurl",
13+
contenttype: "contenttype",
14+
data: "data"
15+
};
16+
17+
const schema = require("../../ext/spec_0_2.json");
18+
19+
// Default options
20+
const ajv = new Ajv();
21+
22+
const validate = ajv.compile(schema);
323

424
function Spec02(){
525
this.payload = {
@@ -13,22 +33,12 @@ function Spec02(){
1333
*/
1434
Spec02.prototype.check = function(){
1535

16-
if(empty(this.payload["type"])) {
17-
throw {message: "'type' is invalid"};
18-
}
19-
20-
if(empty(this.payload["specversion"])) {
21-
throw {message: "'specversion' is invalid"};
22-
}
36+
var valid = validate(this.payload);
2337

24-
if(this.payload["specversion"] !== "0.2") {
25-
throw {message: "'specversion' value is invalid: '"
26-
+ this.payload["specversion"] + "'"};
38+
if(!valid) {
39+
throw {message: "invalid payload"};
2740
}
2841

29-
if(empty(this.payload["id"])) {
30-
throw {message: "'id' is invalid"};
31-
}
3242
};
3343

3444
Spec02.prototype.type = function(_type){
@@ -95,7 +105,11 @@ Spec02.prototype.getData = function() {
95105
};
96106

97107
Spec02.prototype.addExtension = function(key, value){
98-
this.payload[key] = value;
108+
if(!reserved.hasOwnProperty(key)){
109+
this.payload[key] = value;
110+
} else {
111+
throw {message: "Reserved attribute name: '" + key + "'"};
112+
}
99113
return this;
100114
};
101115

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
},
3131
"homepage": "https://github.com/cloudevents/sdk-javascript#readme",
3232
"dependencies": {
33+
"ajv": "^6.7.0",
3334
"axios": "0.18.0",
3435
"is-empty": "1.2.0",
3536
"uri-js": "4.2.2",

test/cloudevent_spec_0_2.js

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ const contenttype = "application/json";
99
const data = {};
1010
const extensions = {};
1111

12-
var cloudevent = new Cloudevent(Cloudevent.specs["0.2"])
13-
.type(type)
14-
.source(source);
12+
var cloudevent =
13+
new Cloudevent(Cloudevent.specs["0.2"])
14+
.type(type)
15+
.source(source);
1516

1617
describe("CloudEvents Spec 0.2 - JavaScript SDK", () => {
1718

@@ -65,6 +66,17 @@ describe("CloudEvents Spec 0.2 - JavaScript SDK", () => {
6566
cloudevent.addExtension("extension2", "value2");
6667
expect(cloudevent.format()["extension2"]).to.equal("value2");
6768
});
69+
70+
it("should throw an error when employ reserved name as extension", () => {
71+
72+
var cevt =
73+
new Cloudevent(Cloudevent.specs["0.2"])
74+
.type(type)
75+
.source(source);
76+
expect(cevt.addExtension.bind(cevt, "id"))
77+
.to
78+
.throw("Reserved attribute name: 'id'");
79+
});
6880
});
6981

7082
describe("The Constraints check", () => {
@@ -73,7 +85,7 @@ describe("CloudEvents Spec 0.2 - JavaScript SDK", () => {
7385
cloudevent.type("");
7486
expect(cloudevent.format.bind(cloudevent))
7587
.to
76-
.throw("'type' is invalid");
88+
.throw("invalid payload");
7789
});
7890

7991
it("must be a non-empty string", () => {
@@ -95,7 +107,15 @@ describe("CloudEvents Spec 0.2 - JavaScript SDK", () => {
95107
cloudevent.spec.payload.specversion = "";
96108
expect(cloudevent.format.bind(cloudevent))
97109
.to
98-
.throw("'specversion' is invalid");
110+
.throw("invalid payload");
111+
cloudevent.spec.payload.specversion = "0.2";
112+
});
113+
114+
it("should throw an error when the value is not '0.2'", () => {
115+
cloudevent.spec.payload.specversion = "0.4";
116+
expect(cloudevent.format.bind(cloudevent))
117+
.to
118+
.throw("invalid payload");
99119
cloudevent.spec.payload.specversion = "0.2";
100120
});
101121
});
@@ -105,7 +125,7 @@ describe("CloudEvents Spec 0.2 - JavaScript SDK", () => {
105125
cloudevent.id("");
106126
expect(cloudevent.format.bind(cloudevent))
107127
.to
108-
.throw("'id' is invalid");
128+
.throw("invalid payload");
109129
});
110130
it("must be a non-empty string", () => {
111131
cloudevent.id("my.id-0x0090");

0 commit comments

Comments
 (0)
0