From 1f8940c8a7fe49f4c77955551b2c157c6fd410ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabio=20Jos=C3=A9?= Date: Mon, 4 Nov 2019 13:41:13 -0300 Subject: [PATCH] Support for data_base64 when event data is Binary MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fabio José --- lib/specs/spec_1.js | 29 ++++++++++++++++++- test/http_binding_1.js | 63 ++++++++++++++++++++++++++++++++++++++++++ test/spec_1_tests.js | 6 ++-- 3 files changed, 95 insertions(+), 3 deletions(-) diff --git a/lib/specs/spec_1.js b/lib/specs/spec_1.js index 3b4207e..d0f2571 100644 --- a/lib/specs/spec_1.js +++ b/lib/specs/spec_1.js @@ -9,7 +9,8 @@ const { isInteger, isString, isDate, - isBinary + isBinary, + clone } = require("../utils/fun.js"); const isValidType = (v) => @@ -77,6 +78,32 @@ function Spec1(_caller) { this.caller.prototype.getSubject = function(){ return this.spec.getSubject(); }; + + // format() method override + this.caller.prototype.format = function(){ + // Check the constraints + this.spec.check(); + + // Check before getData() call + let isbin = isBinary(this.spec.payload[RESERVED_ATTRIBUTES.data]); + + // May be used, if isbin==true + let payload = clone(this.spec.payload); + + // To run asData() + this.getData(); + + // Handle when is binary, creating the data_base64 + if(isbin) { + payload[RESERVED_ATTRIBUTES.data_base64] = this.spec.payload[RESERVED_ATTRIBUTES.data]; + delete payload[RESERVED_ATTRIBUTES.data]; + + return this.formatter.format(payload); + } + + // Then, format + return this.formatter.format(this.spec.payload); + }; } /* diff --git a/test/http_binding_1.js b/test/http_binding_1.js index b007331..566368a 100644 --- a/test/http_binding_1.js +++ b/test/http_binding_1.js @@ -2,6 +2,7 @@ const expect = require("chai").expect; const nock = require("nock"); const http = require("http"); const request = require("request"); +const {asBase64} = require("../lib/utils/fun.js"); const BinaryHTTPEmitter = require("../lib/bindings/http/emitter_binary_1.js"); @@ -38,6 +39,8 @@ const cloudevent = .addExtension(ext1Name, ext1Value) .addExtension(ext2Name, ext2Value); +const dataString = ")(*~^my data for ce#@#$%" + const webhook = "https://cloudevents.io/webhook/v1"; const httpcfg = { method : "POST", @@ -45,6 +48,7 @@ const httpcfg = { }; const binary = new BinaryHTTPEmitter(httpcfg); +const structured = new v1.StructuredHTTPEmitter(httpcfg); describe("HTTP Transport Binding - Version 1.0", () => { beforeEach(() => { @@ -54,6 +58,65 @@ describe("HTTP Transport Binding - Version 1.0", () => { .reply(201, {status: "accepted"}); }); + describe("Structured", () => { + describe("JSON Format", () => { + it("requires '" + contentType + "' Content-Type in the header", () => { + return structured.emit(cloudevent) + .then((response) => { + expect(response.config.headers["Content-Type"]) + .to.equal(contentType); + }); + }); + + it("the request payload should be correct", () => { + return structured.emit(cloudevent) + .then((response) => { + expect(JSON.parse(response.config.data)) + .to.deep.equal(cloudevent.format()); + }); + }); + + describe("Binary event data", () => { + it("the request payload should be correct when data is binary", () => { + let bindata = Uint32Array.from(dataString, (c) => c.codePointAt(0)); + let expected = asBase64(bindata); + let binevent = + new Cloudevent(v1.Spec) + .type(type) + .source(source) + .dataContentType("text/plain") + .data(bindata) + .addExtension(ext1Name, ext1Value) + .addExtension(ext2Name, ext2Value); + + return structured.emit(binevent) + .then((response) => { + expect(JSON.parse(response.config.data).data_base64) + .to.equal(expected); + }); + }); + + it("the payload must have 'data_base64' when data is binary", () => { + let binevent = + new Cloudevent(v1.Spec) + .type(type) + .source(source) + .dataContentType("text/plain") + .data(Uint32Array.from(dataString, (c) => c.codePointAt(0))) + .addExtension(ext1Name, ext1Value) + .addExtension(ext2Name, ext2Value); + + return structured.emit(binevent) + .then((response) => { + console.log(response.config.data); + expect(JSON.parse(response.config.data)) + .to.have.property("data_base64"); + }); + }); + }); + }); + }); + describe("Binary", () => { describe("JSON Format", () => { it("requires '" + cloudevent.getDataContentType() + "' Content-Type in the header", () => { diff --git a/test/spec_1_tests.js b/test/spec_1_tests.js index 566c218..7e88635 100644 --- a/test/spec_1_tests.js +++ b/test/spec_1_tests.js @@ -2,6 +2,7 @@ const expect = require("chai").expect; const Spec1 = require("../lib/specs/spec_1.js"); const Cloudevent = require("../index.js"); const uuid = require("uuid/v4"); +const {asBase64} = require("../lib/utils/fun.js"); const id = uuid(); const type = "com.github.pull.create"; @@ -227,12 +228,13 @@ describe("CloudEvents Spec v1.0", () => { it("should be ok when type is 'Uint32Array' for 'Binary'", () => { let dataString = ")(*~^my data for ce#@#$%" - let expected = Uint32Array.from(dataString, (c) => c.codePointAt(0));; + let dataBinary = Uint32Array.from(dataString, (c) => c.codePointAt(0)); + let expected = asBase64(dataBinary); let olddct = cloudevent.getDataContentType(); cloudevent .dataContentType("text/plain") - .data(expected); + .data(dataBinary); expect(cloudevent.getData()).to.deep.equal(expected); cloudevent.dataContentType(olddct);