Skip to content

Commit 0ac40bc

Browse files
authored
Merge pull request #35 from cloudevents/develop
To fix the 'data' special handling
2 parents 5ba6cf7 + fceac22 commit 0ac40bc

File tree

9 files changed

+176
-5
lines changed

9 files changed

+176
-5
lines changed

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,23 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
66

7+
## [Unreleased]
8+
9+
## [0.3.2]
10+
11+
### Fixed
12+
13+
- Fix the special `data` handling: issue
14+
[#33](https://github.com/cloudevents/sdk-javascript/issues/33)
15+
716
## [0.3.1]
817

918
### Fixed
1019

1120
- Axios version to `0.18.1` due the CVE-2019-10742
1221
- Fix the `subject` attribute unmarshal error: issue
1322
[#32](https://github.com/cloudevents/sdk-javascript/issues/32)
23+
24+
[Unreleased]: https://github.com/cloudevents/sdk-javascript/compare/v0.3.2...HEAD
25+
[0.3.2]: https://github.com/cloudevents/sdk-javascript/compare/v0.3.1...v0.3.2
26+
[0.3.1]: https://github.com/cloudevents/sdk-javascript/compare/v0.3.0...v0.3.1

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ Official CloudEvents' SDK for JavaScript.
1313

1414
<img src="https://github.com/cncf/artwork/blob/master/projects/cloudevents/horizontal/color/cloudevents-horizontal-color.png" width="300" height="58" alt="CloudEvents logo">
1515

16-
__Checkout the [changelog](./CHANGELOG.md) to see what's going on!__
16+
**NOTE: This SDK is still considered work in progress, things might (and will)
17+
break with every update.**
18+
19+
**Checkout the [changelog](./CHANGELOG.md) to see what's going on!**
1720

1821
## Contributing
1922

@@ -65,7 +68,8 @@ let ce =
6568

6669
### Before Spec reaches 1.0
6770

68-
- `0.x.p`: where `x` relates to spec version and `p` relates to fixes and releases.
71+
- `0.x.p`: where `x` relates to spec version and `p` relates to fixes, releases
72+
and breaking changes
6973

7074
### After Spec reaches 1.0
7175

lib/specs/spec_0_3.js

Lines changed: 9 additions & 1 deletion
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

lib/utils/fun.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ const isBase64 = (value) =>
3131
const clone = (o) =>
3232
JSON.parse(JSON.stringify(o));
3333

34+
const isJsonContentType = (contentType) =>
35+
contentType && contentType.match(/(json)/i);
36+
37+
const asData = (data, contentType) =>
38+
((typeof data) !== "string"
39+
? data
40+
: isJsonContentType(contentType)
41+
? JSON.parse(data)
42+
: data);
43+
3444
module.exports = {
3545
isString,
3646
isStringOrThrow,
@@ -42,5 +52,7 @@ module.exports = {
4252

4353
equalsOrThrow,
4454
isBase64,
45-
clone
55+
clone,
56+
57+
asData
4658
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cloudevents-sdk",
3-
"version": "0.3.1",
3+
"version": "0.3.2",
44
"description": "CloudEvents SDK for JavaScript",
55
"main": "index.js",
66
"scripts": {

test/bindings/http/receiver_strutured_0_3_test.js

Lines changed: 22 additions & 0 deletions
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

Lines changed: 31 additions & 0 deletions
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/fun_tests.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,67 @@ describe("Functional approach", () => {
4343
expect(actual).to.equal(true);
4444
});
4545
});
46+
47+
describe("asData" , () => {
48+
it("should throw error when data is not a valid json", () => {
49+
let data = "not a json";
50+
51+
expect(fun.asData.bind(fun, data, "application/json"))
52+
.to
53+
.throws();
54+
});
55+
56+
it("should parse string content type as string", () => {
57+
let expected = "a string";
58+
59+
let actual = fun.asData(expected, "text/plain");
60+
61+
expect((typeof actual)).to.equal("string");
62+
expect(actual).to.equal(expected);
63+
});
64+
65+
it("should parse 'application/json' as json object", () => {
66+
let expected = {
67+
much: "wow",
68+
myext : {
69+
ext : "x04"
70+
}
71+
};
72+
73+
let actual = fun.asData(JSON.stringify(expected), "application/json");
74+
75+
expect((typeof actual)).to.equal("object");
76+
expect(actual).to.deep.equal(expected);
77+
});
78+
79+
it("should parse 'application/cloudevents+json' as json object", () => {
80+
let expected = {
81+
much: "wow",
82+
myext : {
83+
ext : "x04"
84+
}
85+
};
86+
87+
let actual = fun.asData(JSON.stringify(expected),
88+
"application/cloudevents+json");
89+
90+
expect((typeof actual)).to.equal("object");
91+
expect(actual).to.deep.equal(expected);
92+
});
93+
94+
it("should parse 'text/json' as json object", () => {
95+
let expected = {
96+
much: "wow",
97+
myext : {
98+
ext : "x04"
99+
}
100+
};
101+
102+
let actual = fun.asData(JSON.stringify(expected),
103+
"text/json");
104+
105+
expect((typeof actual)).to.equal("object");
106+
expect(actual).to.deep.equal(expected);
107+
});
108+
});
46109
});

test/spec_0_3_tests.js

Lines changed: 18 additions & 0 deletions
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)