Apply promise in the unmarshaller impl

Signed-off-by: Fabio José <fabiojose@gmail.com>
This commit is contained in:
Fabio José 2019-06-25 12:01:33 -03:00
parent 542efe2a6c
commit 335531745f
2 changed files with 57 additions and 38 deletions

View File

@ -54,6 +54,8 @@ var Unmarshaller = function() {
} }
Unmarshaller.prototype.unmarshall = function(payload, headers) { Unmarshaller.prototype.unmarshall = function(payload, headers) {
return new Promise((resolve, reject) => {
try {
validate_args(payload, headers); validate_args(payload, headers);
var sanity_headers = Commons.sanity_and_clone(headers); var sanity_headers = Commons.sanity_and_clone(headers);
@ -69,7 +71,11 @@ Unmarshaller.prototype.unmarshall = function(payload, headers) {
var cloudevent = var cloudevent =
receiver_by_binding[binding_name].parse(payload, sanity_headers); receiver_by_binding[binding_name].parse(payload, sanity_headers);
return cloudevent; resolve(cloudevent);
}catch(e){
reject(e);
}
});
} }
module.exports = Unmarshaller; module.exports = Unmarshaller;

View File

@ -23,8 +23,10 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.2", () => {
var un = new Unmarshaller(); var un = new Unmarshaller();
// act and assert // act and assert
expect(un.unmarshall.bind(un, payload)) return un.unmarshall(payload)
.to.throw("payload is null or undefined"); .then(actual => {throw {message: "failed"}})
.catch(err =>
expect(err.message).to.equal("payload is null or undefined"));
}); });
it("Throw error when headers is null", () => { it("Throw error when headers is null", () => {
@ -34,8 +36,10 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.2", () => {
var un = new Unmarshaller(); var un = new Unmarshaller();
// act and assert // act and assert
expect(un.unmarshall.bind(un, payload, headers)) return un.unmarshall(payload, headers)
.to.throw("headers is null or undefined"); .then(actual => {throw {message: "failed"}})
.catch(err =>
expect(err.message).to.equal("headers is null or undefined"));
}); });
it("Throw error when there is no content-type header", () => { it("Throw error when there is no content-type header", () => {
@ -45,8 +49,10 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.2", () => {
var un = new Unmarshaller(); var un = new Unmarshaller();
// act and assert // act and assert
expect(un.unmarshall.bind(un, payload, headers)) un.unmarshall(payload, headers)
.to.throw("content-type header not found"); .then(actual => {throw {message: "failed"}})
.catch(err =>
expect(err.message).to.equal("content-type header not found"));
}); });
it("Throw error when content-type is not allowed", () => { it("Throw error when content-type is not allowed", () => {
@ -58,8 +64,10 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.2", () => {
var un = new Unmarshaller(); var un = new Unmarshaller();
// act and assert // act and assert
expect(un.unmarshall.bind(un, payload, headers)) un.unmarshall(payload, headers)
.to.throw("content type not allowed"); .then(actual => {throw {message: "failed"}})
.catch(err =>
expect(err.message).to.equal("content type not allowed"));
}); });
describe("Structured", () => { describe("Structured", () => {
@ -72,8 +80,10 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.2", () => {
var un = new Unmarshaller(); var un = new Unmarshaller();
// act and assert // act and assert
expect(un.unmarshall.bind(un, payload, headers)) un.unmarshall(payload, headers)
.to.throw("structured+type not allowed"); .then(actual => {throw {message: "failed"}})
.catch(err =>
expect(err.message).to.equal("structured+type not allowed"));
}); });
it("Throw error when the event does not follow the spec 0.2", () => { 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", () => {
var un = new Unmarshaller(); var un = new Unmarshaller();
// act and assert // act and assert
expect(un.unmarshall.bind(un, payload, headers)) un.unmarshall(payload, headers)
.to.throw("invalid payload"); .then(actual => {throw {message: "failed"}})
.catch(err =>
expect(err.message).to.equal("invalid payload"));
}); });
it("Should accept event that follow the spec 0.2", () => { it("Should accept event that follow the spec 0.2", () => {
@ -117,12 +129,11 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.2", () => {
var un = new Unmarshaller(); var un = new Unmarshaller();
// act // act and assert
var actual = un.unmarshall(payload, headers); un.unmarshall(payload, headers)
.then(actual =>
expect(actual).to.be.an("object"));
// assert
expect(actual)
.to.be.an("object");
}); });
}); });
@ -145,8 +156,11 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.2", () => {
var un = new Unmarshaller(); var un = new Unmarshaller();
// act and assert // act and assert
expect(un.unmarshall.bind(un, payload, attributes)) un.unmarshall(payload, attributes)
.to.throw("content type not allowed"); .then(actual => {throw {message: "failed"}})
.catch(err =>
expect(err.message).to.equal("content type not allowed"));
}); });
it("Throw error when the event does not follow the spec 0.2", () => { 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", () => {
var un = new Unmarshaller(); var un = new Unmarshaller();
// act and assert // act and assert
expect(un.unmarshall.bind(un, payload, attributes)) un.unmarshall(payload, attributes)
.to.throw(); .then(actual => {throw {message: "failed"}})
.catch(err =>
expect(err.message).to.not.empty());
}); });
it("No error when all attributes are in place", () => { it("No error when all attributes are in place", () => {
@ -188,12 +204,9 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.2", () => {
var un = new Unmarshaller(); var un = new Unmarshaller();
// act // act and assert
var actual = un.unmarshall(payload, attributes); un.unmarshall(payload, attributes)
.then(actual => expect(actual).to.be.an("object"));
// assert
expect(actual)
.to.be.an("object");
}); });
}); });