Skip to content

Commit 63ae1ad

Browse files
authored
feat(unmarshaller)!: remove asynchronous 0.3 unmarshaller API (cloudevents#126)
This commit removes the unnecessary use of Promises in the 0.3 unmarshaller. There was actually no asynchronous activity happening in that function, so there was no need to deal with Promises, and as a result testing was made much more difficult. Fixes: cloudevents#95 Signed-off-by: Lance Ball <lball@redhat.com>
1 parent 106b943 commit 63ae1ad

File tree

2 files changed

+47
-137
lines changed

2 files changed

+47
-137
lines changed

lib/bindings/http/unmarshaller.js

+16-18
Original file line numberDiff line numberDiff line change
@@ -45,27 +45,25 @@ class Unmarshaller {
4545
}
4646

4747
unmarshall(payload, headers) {
48-
return new Promise((resolve, reject) => {
49-
if (!payload) {
50-
return reject(new TypeError("payload is null or undefined"));
51-
}
52-
if (!headers) {
53-
return reject(new TypeError("headers is null or undefined"));
54-
}
48+
if (!payload) {
49+
throw new TypeError("payload is null or undefined");
50+
}
51+
if (!headers) {
52+
throw new TypeError("headers is null or undefined");
53+
}
5554

56-
// Validation level 1
57-
const sanityHeaders = Commons.sanityAndClone(headers);
58-
if (!sanityHeaders[Constants.HEADER_CONTENT_TYPE]) {
59-
throw new TypeError("content-type header not found");
60-
}
55+
// Validation level 1
56+
const sanityHeaders = Commons.sanityAndClone(headers);
57+
if (!sanityHeaders[Constants.HEADER_CONTENT_TYPE]) {
58+
throw new TypeError("content-type header not found");
59+
}
6160

62-
// Resolve the binding
63-
const bindingName = resolveBindingName(payload, sanityHeaders);
64-
const cloudevent = this.receiverByBinding[bindingName]
65-
.parse(payload, sanityHeaders);
61+
// Resolve the binding
62+
const bindingName = resolveBindingName(payload, sanityHeaders);
63+
const cloudevent = this.receiverByBinding[bindingName]
64+
.parse(payload, sanityHeaders);
6665

67-
resolve(cloudevent);
68-
});
66+
return cloudevent;
6967
}
7068
}
7169

test/bindings/http/unmarshaller_0_3_tests.js

+31-119
Original file line numberDiff line numberDiff line change
@@ -19,128 +19,75 @@ const data = {
1919
foo: "bar"
2020
};
2121

22+
const un = new Unmarshaller();
23+
2224
describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
2325
it("Throw error when payload is null", () => {
24-
// setup
25-
const payload = null;
26-
const un = new Unmarshaller();
27-
28-
// act and assert
29-
return un.unmarshall(payload)
30-
.then(() => { throw new Error("failed"); })
31-
.catch((err) =>
32-
expect(err.message).to.equal("payload is null or undefined"));
26+
expect(() => un.unmarshall(null)).to.throw("payload is null or undefined");
3327
});
3428

3529
it("Throw error when headers is null", () => {
36-
// setup
37-
const payload = {};
38-
const headers = null;
39-
const un = new Unmarshaller();
40-
41-
// act and assert
42-
return un.unmarshall(payload, headers)
43-
.then(() => { throw new Error("failed"); })
44-
.catch((err) =>
45-
expect(err.message).to.equal("headers is null or undefined"));
30+
expect(() => un.unmarshall({})).to.throw("headers is null or undefined");
31+
expect(() => un.unmarshall({}, null)).to
32+
.throw("headers is null or undefined");
4633
});
4734

4835
it("Throw error when there is no content-type header", () => {
49-
// setup
50-
const payload = {};
51-
const headers = {};
52-
const un = new Unmarshaller();
53-
54-
// act and assert
55-
un.unmarshall(payload, headers)
56-
.then(() => { throw new Error("failed"); })
57-
.catch((err) =>
58-
expect(err.message).to.equal("content-type header not found"));
36+
expect(() => un.unmarshall({}, {})).to
37+
.throw("content-type header not found");
5938
});
6039

6140
it("Throw error when content-type is not allowed", () => {
62-
// setup
63-
const payload = {};
6441
const headers = {
6542
"content-type": "text/xml"
6643
};
67-
const un = new Unmarshaller();
68-
69-
// act and assert
70-
un.unmarshall(payload, headers)
71-
.then(() => { throw new Error("failed"); })
72-
.catch((err) =>
73-
expect(err.message).to.equal("content type not allowed"));
44+
expect(() => un.unmarshall({}, headers)).to
45+
.throw("content type not allowed");
7446
});
7547

7648
describe("Structured", () => {
7749
it("Throw error when has not allowed mime", () => {
7850
// setup
79-
const payload = {};
8051
const headers = {
8152
"content-type": "application/cloudevents+zip"
8253
};
83-
const un = new Unmarshaller();
8454

8555
// act and assert
86-
un.unmarshall(payload, headers)
87-
.then(() => { throw new Error("failed"); })
88-
.catch((err) =>
89-
expect(err.message).to.equal("structured+type not allowed"));
56+
expect(() => un.unmarshall({}, headers)).to
57+
.throw("structured+type not allowed");
9058
});
9159

9260
it("Throw error when the event does not follow the spec 0.3", () => {
93-
// setup
9461
const payload =
95-
new v03.CloudEvent(v03.Spec)
96-
.type(type)
97-
.source(source)
98-
.dataContentType(ceContentType)
62+
new CloudEvent(v03.Spec)
9963
.time(now)
100-
.schemaurl(schemaurl)
101-
.data(data)
10264
.toString();
10365

10466
const headers = {
10567
"content-type": "application/cloudevents+json"
10668
};
10769

108-
const un = new Unmarshaller();
109-
110-
// act and assert
111-
un.unmarshall(payload, headers)
112-
.then(() => { throw new Error("failed"); })
113-
.catch((err) =>
114-
expect(err.message).to.equal("invalid payload"));
70+
expect(() => un.unmarshall(payload, headers)).to
71+
.throw(TypeError);
11572
});
11673

11774
it("Should accept event that follow the spec 0.3", () => {
118-
// setup
11975
const payload =
12076
new CloudEvent(v03.Spec)
12177
.type(type)
78+
.data(data)
12279
.source(source)
12380
.dataContentType(ceContentType)
12481
.time(now)
12582
.schemaurl(schemaurl)
12683
.subject(subject)
127-
.data(data)
128-
.toString();
84+
.format();
12985

13086
const headers = {
13187
"content-type": "application/cloudevents+json"
13288
};
133-
134-
const un = new Unmarshaller();
135-
136-
// act and assert
137-
return un.unmarshall(payload, headers)
138-
.then((actual) =>
139-
expect(actual).to.be.an("object"))
140-
.catch((err) => {
141-
console.error(err);
142-
throw err;
143-
});
89+
const event = un.unmarshall(payload, headers);
90+
expect(event instanceof CloudEvent).to.equal(true);
14491
});
14592

14693
it("Should parse 'data' stringfied json to json object", () => {
@@ -160,17 +107,8 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
160107
"content-type": "application/cloudevents+json"
161108
};
162109

163-
const un = new Unmarshaller();
164-
165-
// act and assert
166-
return un.unmarshall(payload, headers)
167-
.then((actual) => {
168-
expect(actual.getData()).to.deep.equal(data);
169-
})
170-
.catch((err) => {
171-
console.error(err);
172-
throw err;
173-
});
110+
const event = un.unmarshall(payload, headers);
111+
expect(event.getData()).to.deep.equal(data);
174112
});
175113
});
176114

@@ -190,13 +128,8 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
190128
[HEADER_CONTENT_TYPE]: "text/html"
191129
};
192130

193-
const un = new Unmarshaller();
194-
195-
// act and assert
196-
un.unmarshall(payload, attributes)
197-
.then(() => { throw new Error("failed"); })
198-
.catch((err) =>
199-
expect(err.message).to.equal("content type not allowed"));
131+
expect(() => un.unmarshall(payload, attributes)).to
132+
.throw("content type not allowed");
200133
});
201134

202135
it("Throw error when the event does not follow the spec 0.3", () => {
@@ -214,13 +147,8 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
214147
[HEADER_CONTENT_TYPE]: "application/json"
215148
};
216149

217-
const un = new Unmarshaller();
218-
219-
// act and assert
220-
un.unmarshall(payload, attributes)
221-
.then(() => { throw new Error("failed"); })
222-
.catch((err) =>
223-
expect(err.message).to.not.empty);
150+
expect(() => un.unmarshall(payload, attributes)).to
151+
.throw("header 'ce-specversion' not found");
224152
});
225153

226154
it("No error when all attributes are in place", () => {
@@ -238,11 +166,8 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
238166
[HEADER_CONTENT_TYPE]: "application/json"
239167
};
240168

241-
const un = new Unmarshaller();
242-
243-
// act and assert
244-
un.unmarshall(payload, attributes)
245-
.then((actual) => expect(actual).to.be.an("object"));
169+
const event = un.unmarshall(payload, attributes);
170+
expect(event instanceof CloudEvent).to.equal(true);
246171
});
247172

248173
it("Throw error when 'ce-datacontentencoding' is not allowed", () => {
@@ -260,14 +185,8 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
260185
[BINARY_HEADERS_03.CONTENT_ENCONDING]: "binary"
261186
};
262187

263-
const un = new Unmarshaller();
264-
265-
// act and assert
266-
return un.unmarshall(payload, attributes)
267-
.then(() => { throw new Error("failed"); })
268-
.catch((err) => {
269-
expect(err.message).to.equal("unsupported datacontentencoding");
270-
});
188+
expect(() => un.unmarshall(payload, attributes)).to
189+
.throw("unsupported datacontentencoding");
271190
});
272191

273192
it("No error when 'ce-datacontentencoding' is base64", () => {
@@ -288,15 +207,8 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
288207
[BINARY_HEADERS_03.CONTENT_ENCONDING]: "base64"
289208
};
290209

291-
const un = new Unmarshaller();
292-
293-
// act and assert
294-
return un.unmarshall(payload, attributes)
295-
.then((actual) => expect(actual.getData()).to.deep.equal(expected))
296-
.catch((err) => {
297-
console.error(err);
298-
throw err;
299-
});
210+
const event = un.unmarshall(payload, attributes);
211+
expect(event.getData()).to.deep.equal(expected);
300212
});
301213
});
302214
});

0 commit comments

Comments
 (0)