From 15616995c36a61bbe37800a791650eeeb96ada59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabio=20Jos=C3=A9?= Date: Sun, 1 Sep 2019 10:50:55 -0300 Subject: [PATCH] Fixes: #33 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add the special handling in the getData() method Signed-off-by: Fabio José --- lib/specs/spec_0_3.js | 10 +++++- .../http/receiver_strutured_0_3_test.js | 22 +++++++++++++ test/bindings/http/unmarshaller_0_3_tests.js | 31 +++++++++++++++++++ test/spec_0_3_tests.js | 18 +++++++++++ 4 files changed, 80 insertions(+), 1 deletion(-) diff --git a/lib/specs/spec_0_3.js b/lib/specs/spec_0_3.js index 5dade5d..34a27ea 100644 --- a/lib/specs/spec_0_3.js +++ b/lib/specs/spec_0_3.js @@ -5,7 +5,8 @@ const Ajv = require("ajv"); const { equalsOrThrow, isBase64, - clone + clone, + asData } = require("../utils/fun.js"); const RESERVED_ATTRIBUTES = { @@ -209,6 +210,13 @@ Spec03.prototype.data = function(_data){ return this; }; Spec03.prototype.getData = function() { + let dct = this.payload["datacontenttype"]; + let dce = this.payload["datacontentencoding"]; + + if(dct && !dce){ + this.payload["data"] = asData(this.payload["data"], dct); + } + return this.payload["data"]; }; diff --git a/test/bindings/http/receiver_strutured_0_3_test.js b/test/bindings/http/receiver_strutured_0_3_test.js index a6caf7c..0c23f1e 100644 --- a/test/bindings/http/receiver_strutured_0_3_test.js +++ b/test/bindings/http/receiver_strutured_0_3_test.js @@ -206,5 +206,27 @@ describe("HTTP Transport Binding Structured Receiver for CloudEvents v0.3", () = expect(actualExtensions["extension1"]) .to.equal(extension1); }); + + it("Should parse 'data' stringfied json to json object", () => { + // setup + var payload = v03.event() + .type(type) + .source(source) + .contenttype(ceContentType) + .time(now) + .schemaurl(schemaurl) + .data(JSON.stringify(data)) + .toString(); + + var headers = { + "content-type":"application/cloudevents+json" + }; + + // act + var actual = receiver.parse(payload, headers); + + // assert + expect(actual.getData()).to.deep.equal(data); + }); }); }); diff --git a/test/bindings/http/unmarshaller_0_3_tests.js b/test/bindings/http/unmarshaller_0_3_tests.js index 9304451..08950d0 100644 --- a/test/bindings/http/unmarshaller_0_3_tests.js +++ b/test/bindings/http/unmarshaller_0_3_tests.js @@ -141,6 +141,37 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => { }); }); + + it("Should parse 'data' stringfied json to json object", () => { + // setup + var payload = + new Cloudevent(v03.Spec) + .type(type) + .source(source) + .dataContentType(ceContentType) + .time(now) + .schemaurl(schemaurl) + .subject(subject) + .data(JSON.stringify(data)) + .toString(); + + var headers = { + "content-type":"application/cloudevents+json" + }; + + var un = new Unmarshaller(); + + // act and assert + return un.unmarshall(payload, headers) + .then(actual => { + expect(actual.getData()).to.deep.equal(data) + }) + .catch((err) => { + console.log(err); + throw err; + }); + + }); }); describe("Binary", () => { diff --git a/test/spec_0_3_tests.js b/test/spec_0_3_tests.js index de7dd83..a477095 100644 --- a/test/spec_0_3_tests.js +++ b/test/spec_0_3_tests.js @@ -201,6 +201,24 @@ describe("CloudEvents Spec v0.3", () => { }); }); + describe("'data'", () => { + it("should maintain the type of data when no data content type", () =>{ + delete cloudevent.spec.payload.datacontenttype; + cloudevent + .data(JSON.stringify(data)); + + expect(typeof cloudevent.getData()).to.equal("string"); + cloudevent.dataContentType(dataContentType); + }); + + it("should convert data with stringified json to a json object", () => { + cloudevent + .dataContentType(dataContentType) + .data(JSON.stringify(data)); + expect(cloudevent.getData()).to.deep.equal(data); + }); + }); + describe("'subject'", () => { it("should throw an error when is an empty string", () => { cloudevent.subject("");