From 335531745f93d6bcc3513117d18bc7cf4cbeb42d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabio=20Jos=C3=A9?= Date: Tue, 25 Jun 2019 12:01:33 -0300 Subject: [PATCH] Apply promise in the unmarshaller impl MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fabio José --- lib/bindings/http/unmarshaller_0_2.js | 28 ++++---- test/bindings/http/unmarshaller_0_2_tests.js | 67 ++++++++++++-------- 2 files changed, 57 insertions(+), 38 deletions(-) diff --git a/lib/bindings/http/unmarshaller_0_2.js b/lib/bindings/http/unmarshaller_0_2.js index 1108086..27b0590 100644 --- a/lib/bindings/http/unmarshaller_0_2.js +++ b/lib/bindings/http/unmarshaller_0_2.js @@ -54,22 +54,28 @@ var Unmarshaller = function() { } Unmarshaller.prototype.unmarshall = function(payload, headers) { - validate_args(payload, headers); + return new Promise((resolve, reject) => { + try { + validate_args(payload, headers); - var sanity_headers = Commons.sanity_and_clone(headers); + var sanity_headers = Commons.sanity_and_clone(headers); - // Validation level 1 - if(!sanity_headers[Constants.HEADER_CONTENT_TYPE]){ - throw {message: "content-type header not found"}; - } + // Validation level 1 + if(!sanity_headers[Constants.HEADER_CONTENT_TYPE]){ + throw {message: "content-type header not found"}; + } - // Resolve the binding - var binding_name = resolve_binding_name(payload, sanity_headers); + // Resolve the binding + var binding_name = resolve_binding_name(payload, sanity_headers); - var cloudevent = - receiver_by_binding[binding_name].parse(payload, sanity_headers); + var cloudevent = + receiver_by_binding[binding_name].parse(payload, sanity_headers); - return cloudevent; + resolve(cloudevent); + }catch(e){ + reject(e); + } + }); } module.exports = Unmarshaller; diff --git a/test/bindings/http/unmarshaller_0_2_tests.js b/test/bindings/http/unmarshaller_0_2_tests.js index 0846916..17df61f 100644 --- a/test/bindings/http/unmarshaller_0_2_tests.js +++ b/test/bindings/http/unmarshaller_0_2_tests.js @@ -23,8 +23,10 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.2", () => { var un = new Unmarshaller(); // act and assert - expect(un.unmarshall.bind(un, payload)) - .to.throw("payload is null or undefined"); + return un.unmarshall(payload) + .then(actual => {throw {message: "failed"}}) + .catch(err => + expect(err.message).to.equal("payload is null or undefined")); }); it("Throw error when headers is null", () => { @@ -34,8 +36,10 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.2", () => { var un = new Unmarshaller(); // act and assert - expect(un.unmarshall.bind(un, payload, headers)) - .to.throw("headers is null or undefined"); + return un.unmarshall(payload, headers) + .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", () => { @@ -45,8 +49,10 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.2", () => { var un = new Unmarshaller(); // act and assert - expect(un.unmarshall.bind(un, payload, headers)) - .to.throw("content-type header not found"); + un.unmarshall(payload, headers) + .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", () => { @@ -58,8 +64,10 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.2", () => { var un = new Unmarshaller(); // act and assert - expect(un.unmarshall.bind(un, payload, headers)) - .to.throw("content type not allowed"); + un.unmarshall(payload, headers) + .then(actual => {throw {message: "failed"}}) + .catch(err => + expect(err.message).to.equal("content type not allowed")); }); describe("Structured", () => { @@ -72,8 +80,10 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.2", () => { var un = new Unmarshaller(); // act and assert - expect(un.unmarshall.bind(un, payload, headers)) - .to.throw("structured+type not allowed"); + un.unmarshall(payload, headers) + .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", () => { @@ -95,8 +105,10 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.2", () => { var un = new Unmarshaller(); // act and assert - expect(un.unmarshall.bind(un, payload, headers)) - .to.throw("invalid payload"); + un.unmarshall(payload, headers) + .then(actual => {throw {message: "failed"}}) + .catch(err => + expect(err.message).to.equal("invalid payload")); }); 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(); - // act - var actual = un.unmarshall(payload, headers); + // act and assert + 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(); // act and assert - expect(un.unmarshall.bind(un, payload, attributes)) - .to.throw("content type not allowed"); + un.unmarshall(payload, attributes) + .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", () => { @@ -167,8 +181,10 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.2", () => { var un = new Unmarshaller(); // act and assert - expect(un.unmarshall.bind(un, payload, attributes)) - .to.throw(); + un.unmarshall(payload, attributes) + .then(actual => {throw {message: "failed"}}) + .catch(err => + expect(err.message).to.not.empty()); }); 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(); - // act - var actual = un.unmarshall(payload, attributes); - - // assert - expect(actual) - .to.be.an("object"); + // act and assert + un.unmarshall(payload, attributes) + .then(actual => expect(actual).to.be.an("object")); }); });