Skip to content

Commit 1561699

Browse files
committed
Fixes: #33
Add the special handling in the getData() method Signed-off-by: Fabio José <fabiojose@gmail.com>
1 parent fcdf580 commit 1561699

File tree

4 files changed

+80
-1
lines changed

4 files changed

+80
-1
lines changed

lib/specs/spec_0_3.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ const Ajv = require("ajv");
55
const {
66
equalsOrThrow,
77
isBase64,
8-
clone
8+
clone,
9+
asData
910
} = require("../utils/fun.js");
1011

1112
const RESERVED_ATTRIBUTES = {
@@ -209,6 +210,13 @@ Spec03.prototype.data = function(_data){
209210
return this;
210211
};
211212
Spec03.prototype.getData = function() {
213+
let dct = this.payload["datacontenttype"];
214+
let dce = this.payload["datacontentencoding"];
215+
216+
if(dct && !dce){
217+
this.payload["data"] = asData(this.payload["data"], dct);
218+
}
219+
212220
return this.payload["data"];
213221
};
214222

test/bindings/http/receiver_strutured_0_3_test.js

+22
Original file line numberDiff line numberDiff line change
@@ -206,5 +206,27 @@ describe("HTTP Transport Binding Structured Receiver for CloudEvents v0.3", () =
206206
expect(actualExtensions["extension1"])
207207
.to.equal(extension1);
208208
});
209+
210+
it("Should parse 'data' stringfied json to json object", () => {
211+
// setup
212+
var payload = v03.event()
213+
.type(type)
214+
.source(source)
215+
.contenttype(ceContentType)
216+
.time(now)
217+
.schemaurl(schemaurl)
218+
.data(JSON.stringify(data))
219+
.toString();
220+
221+
var headers = {
222+
"content-type":"application/cloudevents+json"
223+
};
224+
225+
// act
226+
var actual = receiver.parse(payload, headers);
227+
228+
// assert
229+
expect(actual.getData()).to.deep.equal(data);
230+
});
209231
});
210232
});

test/bindings/http/unmarshaller_0_3_tests.js

+31
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,37 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
141141
});
142142

143143
});
144+
145+
it("Should parse 'data' stringfied json to json object", () => {
146+
// setup
147+
var payload =
148+
new Cloudevent(v03.Spec)
149+
.type(type)
150+
.source(source)
151+
.dataContentType(ceContentType)
152+
.time(now)
153+
.schemaurl(schemaurl)
154+
.subject(subject)
155+
.data(JSON.stringify(data))
156+
.toString();
157+
158+
var headers = {
159+
"content-type":"application/cloudevents+json"
160+
};
161+
162+
var un = new Unmarshaller();
163+
164+
// act and assert
165+
return un.unmarshall(payload, headers)
166+
.then(actual => {
167+
expect(actual.getData()).to.deep.equal(data)
168+
})
169+
.catch((err) => {
170+
console.log(err);
171+
throw err;
172+
});
173+
174+
});
144175
});
145176

146177
describe("Binary", () => {

test/spec_0_3_tests.js

+18
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,24 @@ describe("CloudEvents Spec v0.3", () => {
201201
});
202202
});
203203

204+
describe("'data'", () => {
205+
it("should maintain the type of data when no data content type", () =>{
206+
delete cloudevent.spec.payload.datacontenttype;
207+
cloudevent
208+
.data(JSON.stringify(data));
209+
210+
expect(typeof cloudevent.getData()).to.equal("string");
211+
cloudevent.dataContentType(dataContentType);
212+
});
213+
214+
it("should convert data with stringified json to a json object", () => {
215+
cloudevent
216+
.dataContentType(dataContentType)
217+
.data(JSON.stringify(data));
218+
expect(cloudevent.getData()).to.deep.equal(data);
219+
});
220+
});
221+
204222
describe("'subject'", () => {
205223
it("should throw an error when is an empty string", () => {
206224
cloudevent.subject("");

0 commit comments

Comments
 (0)