Merge pull request #6 from cloudevents/develop

Develop
This commit is contained in:
Fabio José 2019-01-20 22:03:15 -02:00 committed by GitHub
commit 2cc5999704
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 137 additions and 25 deletions

3
.gitignore vendored
View File

@ -75,3 +75,6 @@ typings/
# FuseBox cache
.fusebox/
# Vim
*.swp

74
ext/spec_0_2.json Normal file
View File

@ -0,0 +1,74 @@
{
"$ref": "#/definitions/event",
"definitions": {
"specversion": {
"type": "string",
"minLength": 1,
"const": "0.2"
},
"contenttype": {
"type": "string"
},
"data": {
"type": [
"object",
"string"
]
},
"event": {
"properties": {
"specversion": {
"$ref": "#/definitions/specversion"
},
"contenttype": {
"$ref": "#/definitions/contenttype"
},
"data": {
"$ref": "#/definitions/data"
},
"id": {
"$ref": "#/definitions/id"
},
"time": {
"$ref": "#/definitions/time"
},
"type": {
"$ref": "#/definitions/type"
},
"extensions": {
"$ref": "#/definitions/extensions"
},
"source": {
"$ref": "#/definitions/source"
}
},
"required": [
"specversion",
"id",
"type",
"source"
],
"type": "object"
},
"id": {
"type": "string",
"minLength": 1
},
"time": {
"format": "date-time",
"type": "string"
},
"type": {
"type": "string",
"minLength": 1
},
"extensions": {
"type": "object"
},
"source": {
"format": "uri-reference",
"type": "string"
}
},
"type": "object"
}

View File

@ -17,9 +17,7 @@ HTTPBinary.prototype.emit = function(cloudevent){
// Always set stuff in _config
var _headers = _config["headers"];
if(cloudevent.getContenttype()) {
_headers["Content-Type"] = cloudevent.getContenttype();
}
_headers["Content-Type"] = cloudevent.getContenttype();
_headers["ce-type"] = cloudevent.getType();
_headers["ce-specversion"] = cloudevent.getSpecversion();
@ -36,7 +34,9 @@ HTTPBinary.prototype.emit = function(cloudevent){
// Have extensions?
var exts = cloudevent.getExtensions();
for(var ext in exts){
_headers["ce-" + ext] = exts[ext];
if({}.hasOwnProperty.call(exts, ext)){
_headers["ce-" + ext] = exts[ext];
}
}
// Return the Promise

View File

@ -1,5 +1,25 @@
var uuid = require("uuid/v4");
var empty = require("is-empty");
var Ajv = require("ajv");
// Reserved attributes names
const reserved = {
type: "type",
specversion: "specversion",
source: "source",
id: "id",
time: "time",
schemaurl: "schemaurl",
contenttype: "contenttype",
data: "data"
};
const schema = require("../../ext/spec_0_2.json");
// Default options
const ajv = new Ajv();
const validate = ajv.compile(schema);
function Spec02(){
this.payload = {
@ -13,22 +33,12 @@ function Spec02(){
*/
Spec02.prototype.check = function(){
if(empty(this.payload["type"])) {
throw {message: "'type' is invalid"};
var valid = validate(this.payload);
if(!valid) {
throw {message: "invalid payload"};
}
if(empty(this.payload["specversion"])) {
throw {message: "'specversion' is invalid"};
}
if(this.payload["specversion"] !== "0.2") {
throw {message: "'specversion' value is invalid: '"
+ this.payload["specversion"] + "'"};
}
if(empty(this.payload["id"])) {
throw {message: "'id' is invalid"};
}
};
Spec02.prototype.type = function(_type){
@ -95,7 +105,11 @@ Spec02.prototype.getData = function() {
};
Spec02.prototype.addExtension = function(key, value){
this.payload[key] = value;
if(!reserved.hasOwnProperty(key)){
this.payload[key] = value;
} else {
throw {message: "Reserved attribute name: '" + key + "'"};
}
return this;
};

View File

@ -30,6 +30,7 @@
},
"homepage": "https://github.com/cloudevents/sdk-javascript#readme",
"dependencies": {
"ajv": "^6.7.0",
"axios": "0.18.0",
"is-empty": "1.2.0",
"uri-js": "4.2.2",

View File

@ -9,9 +9,10 @@ const contenttype = "application/json";
const data = {};
const extensions = {};
var cloudevent = new Cloudevent(Cloudevent.specs["0.2"])
.type(type)
.source(source);
var cloudevent =
new Cloudevent(Cloudevent.specs["0.2"])
.type(type)
.source(source);
describe("CloudEvents Spec 0.2 - JavaScript SDK", () => {
@ -65,6 +66,17 @@ describe("CloudEvents Spec 0.2 - JavaScript SDK", () => {
cloudevent.addExtension("extension2", "value2");
expect(cloudevent.format()["extension2"]).to.equal("value2");
});
it("should throw an error when employ reserved name as extension", () => {
var cevt =
new Cloudevent(Cloudevent.specs["0.2"])
.type(type)
.source(source);
expect(cevt.addExtension.bind(cevt, "id"))
.to
.throw("Reserved attribute name: 'id'");
});
});
describe("The Constraints check", () => {
@ -73,7 +85,7 @@ describe("CloudEvents Spec 0.2 - JavaScript SDK", () => {
cloudevent.type("");
expect(cloudevent.format.bind(cloudevent))
.to
.throw("'type' is invalid");
.throw("invalid payload");
});
it("must be a non-empty string", () => {
@ -95,7 +107,15 @@ describe("CloudEvents Spec 0.2 - JavaScript SDK", () => {
cloudevent.spec.payload.specversion = "";
expect(cloudevent.format.bind(cloudevent))
.to
.throw("'specversion' is invalid");
.throw("invalid payload");
cloudevent.spec.payload.specversion = "0.2";
});
it("should throw an error when the value is not '0.2'", () => {
cloudevent.spec.payload.specversion = "0.4";
expect(cloudevent.format.bind(cloudevent))
.to
.throw("invalid payload");
cloudevent.spec.payload.specversion = "0.2";
});
});
@ -105,7 +125,7 @@ describe("CloudEvents Spec 0.2 - JavaScript SDK", () => {
cloudevent.id("");
expect(cloudevent.format.bind(cloudevent))
.to
.throw("'id' is invalid");
.throw("invalid payload");
});
it("must be a non-empty string", () => {
cloudevent.id("my.id-0x0090");