Remaining attributes tests
Extensions Signed-off-by: Fabio José <fabiojose@gmail.com>
This commit is contained in:
parent
df736d53ec
commit
0d34de5315
|
@ -2,6 +2,10 @@ const uuid = require("uuid/v4");
|
||||||
const empty = require("is-empty");
|
const empty = require("is-empty");
|
||||||
const Ajv = require("ajv");
|
const Ajv = require("ajv");
|
||||||
|
|
||||||
|
const {
|
||||||
|
asData
|
||||||
|
} = require("../utils/fun.js");
|
||||||
|
|
||||||
const RESERVED_ATTRIBUTES = {
|
const RESERVED_ATTRIBUTES = {
|
||||||
type: "type",
|
type: "type",
|
||||||
specversion: "specversion",
|
specversion: "specversion",
|
||||||
|
@ -150,7 +154,22 @@ Spec1.prototype.data = function(_data){
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
Spec1.prototype.getData = function() {
|
Spec1.prototype.getData = function() {
|
||||||
|
let dct = this.payload["datacontenttype"];
|
||||||
|
|
||||||
|
if(dct){
|
||||||
|
this.payload["data"] = asData(this.payload["data"], dct);
|
||||||
|
}
|
||||||
|
|
||||||
return this.payload["data"];
|
return this.payload["data"];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Spec1.prototype.addExtension = function(key, value){
|
||||||
|
if(!RESERVED_ATTRIBUTES.hasOwnProperty(key)){
|
||||||
|
this.payload[key] = value;
|
||||||
|
} else {
|
||||||
|
throw {message: "Reserved attribute name: '" + key + "'"};
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = Spec1;
|
module.exports = Spec1;
|
||||||
|
|
|
@ -65,5 +65,122 @@ describe("CloudEvents Spec v1.0", () => {
|
||||||
it("Should have 'data'", () => {
|
it("Should have 'data'", () => {
|
||||||
expect(cloudevent.getData()).to.deep.equal(data);
|
expect(cloudevent.getData()).to.deep.equal(data);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("Should have the 'extension1'", () => {
|
||||||
|
cloudevent.addExtension("extension1", "value1");
|
||||||
|
expect(cloudevent.spec.payload["extension1"])
|
||||||
|
.to.equal("value1");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should throw an error when use a reserved name as extension", () => {
|
||||||
|
expect(cloudevent.addExtension.bind(cloudevent, "id"))
|
||||||
|
.to.throw("Reserved attribute name: 'id'");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("The Constraints check", () => {
|
||||||
|
describe("'id'", () => {
|
||||||
|
it("should throw an error when is absent", () => {
|
||||||
|
delete cloudevent.spec.payload.id;
|
||||||
|
expect(cloudevent.format.bind(cloudevent))
|
||||||
|
.to
|
||||||
|
.throw("invalid payload");
|
||||||
|
cloudevent.spec.payload.id = id;
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should throw an erro when is empty", () => {
|
||||||
|
cloudevent.spec.payload.id = "";
|
||||||
|
expect(cloudevent.format.bind(cloudevent))
|
||||||
|
.to
|
||||||
|
.throw("invalid payload");
|
||||||
|
cloudevent.spec.payload.id = id;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("'source'", () => {
|
||||||
|
it("should throw an error when is absent", () => {
|
||||||
|
delete cloudevent.spec.payload.source;
|
||||||
|
expect(cloudevent.format.bind(cloudevent))
|
||||||
|
.to
|
||||||
|
.throw("invalid payload");
|
||||||
|
cloudevent.spec.payload.source = source;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("'specversion'", () => {
|
||||||
|
it("should throw an error when is absent", () => {
|
||||||
|
delete cloudevent.spec.payload.specversion;
|
||||||
|
expect(cloudevent.format.bind(cloudevent))
|
||||||
|
.to
|
||||||
|
.throw("invalid payload");
|
||||||
|
cloudevent.spec.payload.specversion = "1.0";
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should throw an error when is empty", () => {
|
||||||
|
cloudevent.spec.payload.specversion = "";
|
||||||
|
expect(cloudevent.format.bind(cloudevent))
|
||||||
|
.to
|
||||||
|
.throw("invalid payload");
|
||||||
|
cloudevent.spec.payload.specversion = "1.0";
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("'type'", () => {
|
||||||
|
it("should throw an error when is absent", () => {
|
||||||
|
delete cloudevent.spec.payload.type;
|
||||||
|
expect(cloudevent.format.bind(cloudevent))
|
||||||
|
.to
|
||||||
|
.throw("invalid payload");
|
||||||
|
cloudevent.spec.payload.type = type;
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should throw an error when is an empty string", () => {
|
||||||
|
cloudevent.type("");
|
||||||
|
expect(cloudevent.format.bind(cloudevent))
|
||||||
|
.to
|
||||||
|
.throw("invalid payload");
|
||||||
|
cloudevent.type(type);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("must be a non-empty string", () => {
|
||||||
|
cloudevent.type(type);
|
||||||
|
expect(cloudevent.spec.payload.type).to.equal(type);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("'data'", () => {
|
||||||
|
it("should maintain the type of data when no data content type", () =>{
|
||||||
|
delete cloudevent.spec.payload.datacontenttype;
|
||||||
|
cloudevent
|
||||||
|
.data(JSON.stringify(data));
|
||||||
|
|
||||||
|
expect(typeof cloudevent.getData()).to.equal("string");
|
||||||
|
cloudevent.dataContentType(dataContentType);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should convert data with stringified json to a json object", () => {
|
||||||
|
cloudevent
|
||||||
|
.dataContentType(dataContentType)
|
||||||
|
.data(JSON.stringify(data));
|
||||||
|
expect(cloudevent.getData()).to.deep.equal(data);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("'subject'", () => {
|
||||||
|
it("should throw an error when is an empty string", () => {
|
||||||
|
cloudevent.subject("");
|
||||||
|
expect(cloudevent.format.bind(cloudevent))
|
||||||
|
.to
|
||||||
|
.throw("invalid payload");
|
||||||
|
cloudevent.subject(type);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("'time'", () => {
|
||||||
|
it("must adhere to the format specified in RFC 3339", () => {
|
||||||
|
cloudevent.time(time);
|
||||||
|
expect(cloudevent.format()["time"]).to.equal(time.toISOString());
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue