Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,6 @@ typings/
# FuseBox cache
.fusebox/

# Vim
*.swp

74 changes: 74 additions & 0 deletions ext/spec_0_2.json
Original file line number Diff line number Diff line change
@@ -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"
}
8 changes: 4 additions & 4 deletions lib/bindings/http/binary_0_2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
Expand Down
42 changes: 28 additions & 14 deletions lib/specs/spec_0_2.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -13,22 +33,12 @@ function Spec02(){
*/
Spec02.prototype.check = function(){

if(empty(this.payload["type"])) {
throw {message: "'type' is invalid"};
}

if(empty(this.payload["specversion"])) {
throw {message: "'specversion' is invalid"};
}
var valid = validate(this.payload);

if(this.payload["specversion"] !== "0.2") {
throw {message: "'specversion' value is invalid: '"
+ this.payload["specversion"] + "'"};
if(!valid) {
throw {message: "invalid payload"};
}

if(empty(this.payload["id"])) {
throw {message: "'id' is invalid"};
}
};

Spec02.prototype.type = function(_type){
Expand Down Expand Up @@ -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;
};

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
32 changes: 26 additions & 6 deletions test/cloudevent_spec_0_2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {

Expand Down Expand Up @@ -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", () => {
Expand All @@ -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", () => {
Expand All @@ -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";
});
});
Expand All @@ -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");
Expand Down