Skip to content

Commit 3355317

Browse files
committed
Apply promise in the unmarshaller impl
Signed-off-by: Fabio José <[email protected]>
1 parent 542efe2 commit 3355317

File tree

2 files changed

+57
-38
lines changed

2 files changed

+57
-38
lines changed

lib/bindings/http/unmarshaller_0_2.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,28 @@ var Unmarshaller = function() {
5454
}
5555

5656
Unmarshaller.prototype.unmarshall = function(payload, headers) {
57-
validate_args(payload, headers);
57+
return new Promise((resolve, reject) => {
58+
try {
59+
validate_args(payload, headers);
5860

59-
var sanity_headers = Commons.sanity_and_clone(headers);
61+
var sanity_headers = Commons.sanity_and_clone(headers);
6062

61-
// Validation level 1
62-
if(!sanity_headers[Constants.HEADER_CONTENT_TYPE]){
63-
throw {message: "content-type header not found"};
64-
}
63+
// Validation level 1
64+
if(!sanity_headers[Constants.HEADER_CONTENT_TYPE]){
65+
throw {message: "content-type header not found"};
66+
}
6567

66-
// Resolve the binding
67-
var binding_name = resolve_binding_name(payload, sanity_headers);
68+
// Resolve the binding
69+
var binding_name = resolve_binding_name(payload, sanity_headers);
6870

69-
var cloudevent =
70-
receiver_by_binding[binding_name].parse(payload, sanity_headers);
71+
var cloudevent =
72+
receiver_by_binding[binding_name].parse(payload, sanity_headers);
7173

72-
return cloudevent;
74+
resolve(cloudevent);
75+
}catch(e){
76+
reject(e);
77+
}
78+
});
7379
}
7480

7581
module.exports = Unmarshaller;

test/bindings/http/unmarshaller_0_2_tests.js

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.2", () => {
2323
var un = new Unmarshaller();
2424

2525
// act and assert
26-
expect(un.unmarshall.bind(un, payload))
27-
.to.throw("payload is null or undefined");
26+
return un.unmarshall(payload)
27+
.then(actual => {throw {message: "failed"}})
28+
.catch(err =>
29+
expect(err.message).to.equal("payload is null or undefined"));
2830
});
2931

3032
it("Throw error when headers is null", () => {
@@ -34,8 +36,10 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.2", () => {
3436
var un = new Unmarshaller();
3537

3638
// act and assert
37-
expect(un.unmarshall.bind(un, payload, headers))
38-
.to.throw("headers is null or undefined");
39+
return un.unmarshall(payload, headers)
40+
.then(actual => {throw {message: "failed"}})
41+
.catch(err =>
42+
expect(err.message).to.equal("headers is null or undefined"));
3943
});
4044

4145
it("Throw error when there is no content-type header", () => {
@@ -45,8 +49,10 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.2", () => {
4549
var un = new Unmarshaller();
4650

4751
// act and assert
48-
expect(un.unmarshall.bind(un, payload, headers))
49-
.to.throw("content-type header not found");
52+
un.unmarshall(payload, headers)
53+
.then(actual => {throw {message: "failed"}})
54+
.catch(err =>
55+
expect(err.message).to.equal("content-type header not found"));
5056
});
5157

5258
it("Throw error when content-type is not allowed", () => {
@@ -58,8 +64,10 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.2", () => {
5864
var un = new Unmarshaller();
5965

6066
// act and assert
61-
expect(un.unmarshall.bind(un, payload, headers))
62-
.to.throw("content type not allowed");
67+
un.unmarshall(payload, headers)
68+
.then(actual => {throw {message: "failed"}})
69+
.catch(err =>
70+
expect(err.message).to.equal("content type not allowed"));
6371
});
6472

6573
describe("Structured", () => {
@@ -72,8 +80,10 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.2", () => {
7280
var un = new Unmarshaller();
7381

7482
// act and assert
75-
expect(un.unmarshall.bind(un, payload, headers))
76-
.to.throw("structured+type not allowed");
83+
un.unmarshall(payload, headers)
84+
.then(actual => {throw {message: "failed"}})
85+
.catch(err =>
86+
expect(err.message).to.equal("structured+type not allowed"));
7787
});
7888

7989
it("Throw error when the event does not follow the spec 0.2", () => {
@@ -95,8 +105,10 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.2", () => {
95105
var un = new Unmarshaller();
96106

97107
// act and assert
98-
expect(un.unmarshall.bind(un, payload, headers))
99-
.to.throw("invalid payload");
108+
un.unmarshall(payload, headers)
109+
.then(actual => {throw {message: "failed"}})
110+
.catch(err =>
111+
expect(err.message).to.equal("invalid payload"));
100112
});
101113

102114
it("Should accept event that follow the spec 0.2", () => {
@@ -117,12 +129,11 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.2", () => {
117129

118130
var un = new Unmarshaller();
119131

120-
// act
121-
var actual = un.unmarshall(payload, headers);
132+
// act and assert
133+
un.unmarshall(payload, headers)
134+
.then(actual =>
135+
expect(actual).to.be.an("object"));
122136

123-
// assert
124-
expect(actual)
125-
.to.be.an("object");
126137
});
127138
});
128139

@@ -145,8 +156,11 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.2", () => {
145156
var un = new Unmarshaller();
146157

147158
// act and assert
148-
expect(un.unmarshall.bind(un, payload, attributes))
149-
.to.throw("content type not allowed");
159+
un.unmarshall(payload, attributes)
160+
.then(actual => {throw {message: "failed"}})
161+
.catch(err =>
162+
expect(err.message).to.equal("content type not allowed"));
163+
150164
});
151165

152166
it("Throw error when the event does not follow the spec 0.2", () => {
@@ -167,8 +181,10 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.2", () => {
167181
var un = new Unmarshaller();
168182

169183
// act and assert
170-
expect(un.unmarshall.bind(un, payload, attributes))
171-
.to.throw();
184+
un.unmarshall(payload, attributes)
185+
.then(actual => {throw {message: "failed"}})
186+
.catch(err =>
187+
expect(err.message).to.not.empty());
172188
});
173189

174190
it("No error when all attributes are in place", () => {
@@ -188,12 +204,9 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.2", () => {
188204

189205
var un = new Unmarshaller();
190206

191-
// act
192-
var actual = un.unmarshall(payload, attributes);
193-
194-
// assert
195-
expect(actual)
196-
.to.be.an("object");
207+
// act and assert
208+
un.unmarshall(payload, attributes)
209+
.then(actual => expect(actual).to.be.an("object"));
197210

198211
});
199212
});

0 commit comments

Comments
 (0)