diff --git a/lib/specs/spec_0_3.js b/lib/specs/spec_0_3.js index 0a778b6..a0a80a8 100644 --- a/lib/specs/spec_0_3.js +++ b/lib/specs/spec_0_3.js @@ -3,7 +3,9 @@ const empty = require("is-empty"); const Ajv = require("ajv"); const { - equalsOrThrow + equalsOrThrow, + isBase64, + clone } = require("../utils/fun.js"); const RESERVED_ATTRIBUTES = { @@ -19,8 +21,10 @@ const RESERVED_ATTRIBUTES = { data: "data" }; -const SUPPORTED_CONTENT_ENCODING = []; -SUPPORTED_CONTENT_ENCODING.push("base64"); +const SUPPORTED_CONTENT_ENCODING = {}; +SUPPORTED_CONTENT_ENCODING["base64"] = { + check : (data) => isBase64(data) +}; const schema = require("../../ext/spec_0_3.json"); @@ -79,7 +83,9 @@ function Spec03(_caller){ Spec03.prototype.check = function(ce){ var toCheck = (!ce ? this.payload : ce); - var valid = isValidAgainstSchema(toCheck); + if(!isValidAgainstSchema(toCheck)) { + throw {message: "invalid payload", errors: isValidAgainstSchema.errors}; + } Array.of(toCheck) .filter((tc) => (typeof tc.data) !== "string") @@ -93,16 +99,31 @@ Spec03.prototype.check = function(ce){ Array.of(toCheck) .filter((tc) => tc["datacontentencoding"]) .map((tc) => tc["datacontentencoding"].toLocaleLowerCase("en-US")) - .filter((dce) => !SUPPORTED_CONTENT_ENCODING.includes(dce)) + .filter((dce) => !Object.keys(SUPPORTED_CONTENT_ENCODING).includes(dce)) .forEach((dce) => { throw {message: "invalid payload", errors: [ "Unsupported content encoding: " + dce ]}; }); - if(!valid) { - throw {message: "invalid payload", errors: isValidAgainstSchema.errors}; - } + Array.of(toCheck) + .filter((tc) => tc["datacontentencoding"]) + .map((tc) => { + let newtc = clone(tc); + newtc.datacontentencoding = + newtc.datacontentencoding.toLocaleLowerCase("en-US"); + + return newtc; + }) + .filter((tc) => Object.keys(SUPPORTED_CONTENT_ENCODING) + .includes(tc.datacontentencoding)) + .filter((tc) => !SUPPORTED_CONTENT_ENCODING[tc.datacontentencoding] + .check(tc.data)) + .forEach((tc) => { + throw {message: "invalid payload", errors: [ + "Invalid content encoding of data: " + tc.data + ]}; + }); }; Spec03.prototype.id = function(_id){ diff --git a/test/spec_0_3_tests.js b/test/spec_0_3_tests.js index 2256f7a..70046f8 100644 --- a/test/spec_0_3_tests.js +++ b/test/spec_0_3_tests.js @@ -131,7 +131,7 @@ describe("CloudEvents Spec v0.3", () => { cloudevent.spec.payload.specversion = "0.3"; }); - it("should throw an erro when is empty", () => { + it("should throw an error when is empty", () => { cloudevent.spec.payload.specversion = ""; expect(cloudevent.format.bind(cloudevent)) .to @@ -185,6 +185,22 @@ describe("CloudEvents Spec v0.3", () => { cloudevent.data(data); }); + it("should throw an error when 'data' does not carry base64", + () => { + + cloudevent + .data("no base 64 value") + .dataContentEncoding("base64") + .dataContentType("text/plain"); + + expect(cloudevent.format.bind(cloudevent)) + .to + .throw("invalid payload"); + + delete cloudevent.spec.payload.datacontentencoding; + cloudevent.data(data); + }); + it("should accept when 'data' is a string", () => { cloudevent .data("Y2xvdWRldmVudHMK")