Add the special handling in the getData() method

Signed-off-by: Fabio José <fabiojose@gmail.com>
This commit is contained in:
Fabio José 2019-09-01 10:50:55 -03:00
parent fcdf580b64
commit 15616995c3
4 changed files with 80 additions and 1 deletions

View File

@ -5,7 +5,8 @@ const Ajv = require("ajv");
const { const {
equalsOrThrow, equalsOrThrow,
isBase64, isBase64,
clone clone,
asData
} = require("../utils/fun.js"); } = require("../utils/fun.js");
const RESERVED_ATTRIBUTES = { const RESERVED_ATTRIBUTES = {
@ -209,6 +210,13 @@ Spec03.prototype.data = function(_data){
return this; return this;
}; };
Spec03.prototype.getData = function() { Spec03.prototype.getData = function() {
let dct = this.payload["datacontenttype"];
let dce = this.payload["datacontentencoding"];
if(dct && !dce){
this.payload["data"] = asData(this.payload["data"], dct);
}
return this.payload["data"]; return this.payload["data"];
}; };

View File

@ -206,5 +206,27 @@ describe("HTTP Transport Binding Structured Receiver for CloudEvents v0.3", () =
expect(actualExtensions["extension1"]) expect(actualExtensions["extension1"])
.to.equal(extension1); .to.equal(extension1);
}); });
it("Should parse 'data' stringfied json to json object", () => {
// setup
var payload = v03.event()
.type(type)
.source(source)
.contenttype(ceContentType)
.time(now)
.schemaurl(schemaurl)
.data(JSON.stringify(data))
.toString();
var headers = {
"content-type":"application/cloudevents+json"
};
// act
var actual = receiver.parse(payload, headers);
// assert
expect(actual.getData()).to.deep.equal(data);
});
}); });
}); });

View File

@ -141,6 +141,37 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
}); });
}); });
it("Should parse 'data' stringfied json to json object", () => {
// setup
var payload =
new Cloudevent(v03.Spec)
.type(type)
.source(source)
.dataContentType(ceContentType)
.time(now)
.schemaurl(schemaurl)
.subject(subject)
.data(JSON.stringify(data))
.toString();
var headers = {
"content-type":"application/cloudevents+json"
};
var un = new Unmarshaller();
// act and assert
return un.unmarshall(payload, headers)
.then(actual => {
expect(actual.getData()).to.deep.equal(data)
})
.catch((err) => {
console.log(err);
throw err;
});
});
}); });
describe("Binary", () => { describe("Binary", () => {

View File

@ -201,6 +201,24 @@ describe("CloudEvents Spec v0.3", () => {
}); });
}); });
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'", () => { describe("'subject'", () => {
it("should throw an error when is an empty string", () => { it("should throw an error when is an empty string", () => {
cloudevent.subject(""); cloudevent.subject("");