From 1a81e6e7e1ef2b414a21788e92c26d226ab2da99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabio=20Jos=C3=A9?= Date: Fri, 18 Jan 2019 23:42:43 -0200 Subject: [PATCH] Test the extensions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fabio José --- lib/bindings/http/binary_0_2.js | 7 +++++++ lib/cloudevent.js | 11 +++++++++++ test/http_binding_0_2.js | 23 ++++++++++++++++++++++- 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/lib/bindings/http/binary_0_2.js b/lib/bindings/http/binary_0_2.js index a657a35..b252a6f 100644 --- a/lib/bindings/http/binary_0_2.js +++ b/lib/bindings/http/binary_0_2.js @@ -1,4 +1,5 @@ var axios = require("axios"); +var empty = require("is-empty"); function HTTPBinary(configuration){ this.config = configuration; @@ -32,6 +33,12 @@ HTTPBinary.prototype.emit = function(cloudevent){ // Set the cloudevent payload _config["data"] = cloudevent.format().data; + // Have extensions? + var exts = cloudevent.getExtensions(); + for(ext in exts){ + _headers["ce-" + ext] = exts[ext]; + } + // Return the Promise return axios.request(_config); }; diff --git a/lib/cloudevent.js b/lib/cloudevent.js index 0cf5e8c..ea9ed36 100644 --- a/lib/cloudevent.js +++ b/lib/cloudevent.js @@ -14,6 +14,9 @@ var HTTPBinary02 = require("./bindings/http/binary_0_2.js"); function Cloudevent(_spec, _formatter){ this.spec = (_spec) ? new _spec(Cloudevent) : new Spec01(Cloudevent); this.formatter = (_formatter) ? _formatter : new JSONFormatter01(); + + // The map of extensions + this.extensions = {}; } /* @@ -100,9 +103,17 @@ Cloudevent.prototype.getData = function() { Cloudevent.prototype.addExtension = function(key, value){ this.spec.addExtension(key, value); + + // Stores localy + this.extensions[key] = value; + return this; }; +Cloudevent.prototype.getExtensions = function() { + return this.extensions; +} + /* * Export the specs diff --git a/test/http_binding_0_2.js b/test/http_binding_0_2.js index 16086a9..5684952 100644 --- a/test/http_binding_0_2.js +++ b/test/http_binding_0_2.js @@ -15,6 +15,11 @@ const data = { foo: "bar" }; +const ext1Name = "extension1"; +const ext1Value = "foobar"; +const ext2Name = "extension2"; +const ext2Value = "acme"; + const Structured02 = Cloudevent.bindings["http-structured0.2"]; const Binary02 = Cloudevent.bindings["http-binary0.2"]; @@ -25,7 +30,9 @@ var cloudevent = .contenttype(ceContentType) .time(now) .schemaurl(schemaurl) - .data(data); + .data(data) + .addExtension(ext1Name, ext1Value) + .addExtension(ext2Name, ext2Value); var httpcfg = { method : "POST", @@ -123,6 +130,20 @@ describe("HTTP Transport Binding - Version 0.2", () => { .to.have.property("ce-schemaurl"); }); }); + it("HTTP Header contains 'ce-" + ext1Name + "'", () => { + return httpbinary02.emit(cloudevent) + .then((response) => { + expect(response.config.headers) + .to.have.property("ce-" + ext1Name); + }); + }); + it("HTTP Header contains 'ce-" + ext2Name + "'", () => { + return httpbinary02.emit(cloudevent) + .then((response) => { + expect(response.config.headers) + .to.have.property("ce-" + ext2Name); + }); + }); }); }); });