From 50fdc762f4ed6ac5ceddd243bd7e1ce108cbe964 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabio=20Jos=C3=A9?= Date: Wed, 14 Nov 2018 21:18:40 -0200 Subject: [PATCH] Unit testing for http bindings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fabio José --- lib/bindings/http/structured_0_1.js | 24 ++++++++++++++++ test/http_binding_0_1.js | 43 +++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 lib/bindings/http/structured_0_1.js create mode 100644 test/http_binding_0_1.js diff --git a/lib/bindings/http/structured_0_1.js b/lib/bindings/http/structured_0_1.js new file mode 100644 index 0000000..0502173 --- /dev/null +++ b/lib/bindings/http/structured_0_1.js @@ -0,0 +1,24 @@ +var axios = require("axios"); + +function HTTPStructured(configuration){ + this.config = configuration; + + this.config['headers'] = { + 'Content-Type':'application/cloudevents+json; charset=utf-8' + }; +} + +HTTPStructured.prototype.emit = function(cloudevent){ + + // Create new request object + var _config = JSON.parse(JSON.stringify(this.config)); + + // Set the cloudevent payload + _config['data'] = cloudevent.format(); + + // Return the Promise + return axios.request(_config); +} + +module.exports = HTTPStructured; + diff --git a/test/http_binding_0_1.js b/test/http_binding_0_1.js new file mode 100644 index 0000000..aaaceff --- /dev/null +++ b/test/http_binding_0_1.js @@ -0,0 +1,43 @@ +var expect = require("chai").expect; +var Cloudevent = require("../index.js"); +var nock = require("nock"); + +const type = "com.github.pull.create"; +const source = "urn:event:from:myapi/resourse/123"; +const webhook = "https://cloudevents.io/webhook"; +const contentType = "application/cloudevents+json; charset=utf-8"; + +var cloudevent = new Cloudevent() + .type(type) + .source(source); + +var httpcfg = { + method : 'POST', + url : webhook + '/json' +}; + +var httpstructured_0_1 = + new Cloudevent.bindings['http-structured0.1'](httpcfg); + +describe("HTTP Transport Binding", () => { + beforeEach(() => { + // Mocking the webhook + nock(webhook) + .post("/json") + .reply(201, {status: 'accepted'}); + }); + + describe("Structured", () => { + describe("JSON Format", () => { + it("requires '" + contentType + "' Content-Type in header", () => { + return httpstructured_0_1.emit(cloudevent) + .then(response => { + //console.log(response.config); + expect(response.config.headers['Content-Type']) + .to.equal(contentType); + }); + }); + }); + }); +}); +