Skip to content

Commit e1f26f8

Browse files
committed
feat(unmarshaller)!: remove asynchronous 0.3 unmarshaller API
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 <[email protected]>
1 parent 79ec3ef commit e1f26f8

File tree

2 files changed

+69
-78
lines changed

2 files changed

+69
-78
lines changed

lib/bindings/http/unmarshaller.js

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

Lines changed: 53 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
2626
const un = new Unmarshaller();
2727

2828
// 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"));
29+
try {
30+
un.unmarshall(payload);
31+
} catch (err) {
32+
expect(err.message).to.equal("payload is null or undefined");
33+
}
3334
});
3435

3536
it("Throw error when headers is null", () => {
@@ -39,10 +40,11 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
3940
const un = new Unmarshaller();
4041

4142
// 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"));
43+
try {
44+
un.unmarshall(payload, headers);
45+
} catch (err) {
46+
expect(err.message).to.equal("headers is null or undefined");
47+
}
4648
});
4749

4850
it("Throw error when there is no content-type header", () => {
@@ -52,10 +54,11 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
5254
const un = new Unmarshaller();
5355

5456
// 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"));
57+
try {
58+
un.unmarshall(payload, headers);
59+
} catch (err) {
60+
expect(err.message).to.equal("content-type header not found");
61+
}
5962
});
6063

6164
it("Throw error when content-type is not allowed", () => {
@@ -67,10 +70,11 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
6770
const un = new Unmarshaller();
6871

6972
// 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"));
73+
try {
74+
un.unmarshall(payload, headers);
75+
} catch (err) {
76+
expect(err.message).to.equal("content type not allowed");
77+
}
7478
});
7579

7680
describe("Structured", () => {
@@ -83,10 +87,11 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
8387
const un = new Unmarshaller();
8488

8589
// 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"));
90+
try {
91+
un.unmarshall(payload, headers);
92+
} catch (err) {
93+
expect(err.message).to.equal("structured+type not allowed");
94+
}
9095
});
9196

9297
it("Throw error when the event does not follow the spec 0.3", () => {
@@ -108,10 +113,11 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
108113
const un = new Unmarshaller();
109114

110115
// 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"));
116+
try {
117+
un.unmarshall(payload, headers);
118+
} catch (err) {
119+
expect(err.message).to.equal("invalid payload");
120+
}
115121
});
116122

117123
it("Should accept event that follow the spec 0.3", () => {
@@ -134,13 +140,8 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
134140
const un = new Unmarshaller();
135141

136142
// 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-
});
143+
const event = un.unmarshall(payload, headers);
144+
expect(event instanceof CloudEvent).to.equal(true);
144145
});
145146

146147
it("Should parse 'data' stringfied json to json object", () => {
@@ -163,14 +164,8 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
163164
const un = new Unmarshaller();
164165

165166
// 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-
});
167+
const event = un.unmarshall(payload, headers);
168+
expect(event.getData()).to.deep.equal(data);
174169
});
175170
});
176171

@@ -193,10 +188,11 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
193188
const un = new Unmarshaller();
194189

195190
// 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"));
191+
try {
192+
un.unmarshall(payload, attributes);
193+
} catch (err) {
194+
expect(err.message).to.equal("content type not allowed");
195+
}
200196
});
201197

202198
it("Throw error when the event does not follow the spec 0.3", () => {
@@ -217,10 +213,11 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
217213
const un = new Unmarshaller();
218214

219215
// act and assert
220-
un.unmarshall(payload, attributes)
221-
.then(() => { throw new Error("failed"); })
222-
.catch((err) =>
223-
expect(err.message).to.not.empty);
216+
try {
217+
un.unmarshall(payload, attributes);
218+
} catch (err) {
219+
expect(err.message).to.equal("header 'ce-specversion' not found");
220+
}
224221
});
225222

226223
it("No error when all attributes are in place", () => {
@@ -241,8 +238,8 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
241238
const un = new Unmarshaller();
242239

243240
// act and assert
244-
un.unmarshall(payload, attributes)
245-
.then((actual) => expect(actual).to.be.an("object"));
241+
const event = un.unmarshall(payload, attributes);
242+
expect(event instanceof CloudEvent).to.equal(true);
246243
});
247244

248245
it("Throw error when 'ce-datacontentencoding' is not allowed", () => {
@@ -263,11 +260,11 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
263260
const un = new Unmarshaller();
264261

265262
// 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-
});
263+
try {
264+
un.unmarshall(payload, attributes);
265+
} catch (err) {
266+
expect(err.message).to.equal("unsupported datacontentencoding");
267+
}
271268
});
272269

273270
it("No error when 'ce-datacontentencoding' is base64", () => {
@@ -291,12 +288,8 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
291288
const un = new Unmarshaller();
292289

293290
// 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-
});
291+
const event = un.unmarshall(payload, attributes);
292+
expect(event.getData()).to.deep.equal(expected);
300293
});
301294
});
302295
});

0 commit comments

Comments
 (0)