From 141cb045166fc9b01a7751a8748c08d2d3613372 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabio=20Jos=C3=A9?= Date: Tue, 11 Jun 2019 14:41:28 -0300 Subject: [PATCH] Partial check() method impl for binary 0.2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fabio José --- lib/bindings/http/emitter_binary_0_2.js | 34 +++++++++ test/http_binding_0_2.js | 91 +++++++++++++++++++++++++ 2 files changed, 125 insertions(+) diff --git a/lib/bindings/http/emitter_binary_0_2.js b/lib/bindings/http/emitter_binary_0_2.js index f507628..08f0405 100644 --- a/lib/bindings/http/emitter_binary_0_2.js +++ b/lib/bindings/http/emitter_binary_0_2.js @@ -13,6 +13,26 @@ const Headers02 = { EXTENSIONS_PREFIX : "ce-" }; +const required_attributes = []; +required_attributes.push(Headers02.TYPE); +required_attributes.push(Headers02.SPEC_VERSION); +required_attributes.push(Headers02.SOURCE); +required_attributes.push(Headers02.ID); + +function validate_args(payload, attributes) { + if(!payload){ + throw {message: "payload is null or undefined"}; + } + + if(!attributes) { + throw {message: "attributes is null or undefined"}; + } + + if((typeof payload) !== "object"){ + throw {message: "payload must be an object", erros: [typeof payload]}; + } +} + function HTTPBinary02(configuration){ this.config = configuration; @@ -25,6 +45,20 @@ function HTTPBinary02(configuration){ Constants.MIME_CE_JSON + "; charset=" + Constants.CHARSET_DEFAULT; } +HTTPBinary02.prototype.check = function(payload, attributes) { + // Validation Level 0 + validate_args(payload, attributes); + + // Validation Level 1 + for(i in required_attributes){ + if(!attributes[required_attributes[i]]){ + throw {message: "header '" + required_attributes[i] + "' not found"}; + } + } + + +} + HTTPBinary02.prototype.emit = function(cloudevent){ // Create new request object diff --git a/test/http_binding_0_2.js b/test/http_binding_0_2.js index b5ea70f..7471bc3 100644 --- a/test/http_binding_0_2.js +++ b/test/http_binding_0_2.js @@ -7,6 +7,8 @@ var http = require("http"); var request = require("request"); var Spec02 = require("../lib/specs/spec_0_2.js"); +var {HTTPBinary02} = require("../lib/bindings/http/emitter_binary_0_2.js"); + const type = "com.github.pull.create"; const source = "urn:event:from:myapi/resourse/123"; const webhook = "https://cloudevents.io/webhook"; @@ -175,6 +177,95 @@ describe("HTTP Transport Binding - Version 0.2", () => { }); describe("Binary", () => { + describe("Check", () => { + it("Throw error when payload arg is null or undefined", () => { + // setup + var payload = null; + var attributes = {}; + + // act and assert + expect(httpbinary02.check.bind(httpbinary02, payload, attributes)) + .to.throw("payload is null or undefined"); + }); + + it("Throw error when attributes arg is null or undefined", () => { + // setup + var payload = {}; + var attributes = null; + + // act and assert + expect(httpbinary02.check.bind(httpbinary02, payload, attributes)) + .to.throw("attributes is null or undefined"); + }); + + it("Throw error when payload is not an object", () => { + // setup + var payload = "wow"; + var attributes = {}; + + // act and assert + expect(httpbinary02.check.bind(httpbinary02, payload, attributes)) + .to.throw("payload must be an object"); + }); + + it("Throw error when headers has no 'ce-type'", () => { + // setup + var payload = {}; + var attributes = { + //"ce-type" : "type", + "ce-specversion" : "specversion", + "ce-source" : "source", + "ce-id" : "id" + }; + + // act and assert + expect(httpbinary02.check.bind(httpbinary02, payload, attributes)) + .to.throw("header 'ce-type' not found"); + }); + + it("Throw error when headers has no 'ce-specversion'", () => { + // setup + var payload = {}; + var attributes = { + "ce-type" : "type", + "ce-source" : "source", + "ce-id" : "id" + }; + + // act and assert + expect(httpbinary02.check.bind(httpbinary02, payload, attributes)) + .to.throw("header 'ce-specversion' not found"); + }); + + it("Throw error when headers has no 'ce-source'", () => { + // setup + var payload = {}; + var attributes = { + "ce-type" : "type", + "ce-specversion" : "specversion", + "ce-id" : "id" + }; + + // act and assert + expect(httpbinary02.check.bind(httpbinary02, payload, attributes)) + .to.throw("header 'ce-source' not found"); + }); + + it("Throw error when headers has no 'ce-id'", () => { + // setup + var payload = {}; + var attributes = { + "ce-type" : "type", + "ce-specversion" : "specversion", + "ce-source" : "source" + }; + + // act and assert + expect(httpbinary02.check.bind(httpbinary02, payload, attributes)) + .to.throw("header 'ce-id' not found"); + }); + }); + describe("JSON Format", () => { it("requires '" + cloudevent.getContenttype() + "' Content-Type in the header", () => { return httpbinary02.emit(cloudevent)