Checking for datacontentencoding
Signed-off-by: Fabio José <fabiojose@gmail.com>
This commit is contained in:
parent
29bc8b9e17
commit
4fdba57069
|
@ -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){
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Reference in New Issue