Fixes: #33
Add the special handling in the getData() method Signed-off-by: Fabio José <fabiojose@gmail.com>
This commit is contained in:
parent
fcdf580b64
commit
15616995c3
|
@ -5,7 +5,8 @@ const Ajv = require("ajv");
|
|||
const {
|
||||
equalsOrThrow,
|
||||
isBase64,
|
||||
clone
|
||||
clone,
|
||||
asData
|
||||
} = require("../utils/fun.js");
|
||||
|
||||
const RESERVED_ATTRIBUTES = {
|
||||
|
@ -209,6 +210,13 @@ Spec03.prototype.data = function(_data){
|
|||
return this;
|
||||
};
|
||||
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"];
|
||||
};
|
||||
|
||||
|
|
|
@ -206,5 +206,27 @@ describe("HTTP Transport Binding Structured Receiver for CloudEvents v0.3", () =
|
|||
expect(actualExtensions["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);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -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", () => {
|
||||
|
|
|
@ -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'", () => {
|
||||
it("should throw an error when is an empty string", () => {
|
||||
cloudevent.subject("");
|
||||
|
|
Loading…
Reference in New Issue