src: add eslint configuration and npm script

Automatically fixed > 2000 issues. The remaining 200+ issues need
to be fixed by hand. Additionally, all strings are double quotes
which is not typically standard and I wonder about fixing that too.

Signed-off-by: Lance Ball <lball@redhat.com>
This commit is contained in:
Lance Ball 2020-04-14 17:11:42 -04:00
parent b03a243b20
commit 3f238a0124
No known key found for this signature in database
GPG Key ID: DB1D2F8DCDB4EE5C
54 changed files with 3595 additions and 2261 deletions

26
.eslintrc Normal file
View File

@ -0,0 +1,26 @@
{
"extends": "eslint:recommended",
"env": {
"es6": true,
"node": true,
"mocha": true
},
"rules": {
"space-before-function-paren": ["error", "never"],
"standard/no-callback-literal": "off",
"arrow-spacing": "error",
"arrow-parens": ["error", "always"],
"arrow-body-style": ["error", "as-needed"],
"prefer-template": "error",
"max-len": ["warn", { "code": 80 }],
"no-unused-vars": ["warn", {
"argsIgnorePattern": "^_$|^e$|^reject$|^resolve$"
}],
"no-console": ["error", {
"allow": ["warn", "error"]
}],
"valid-jsdoc": "error",
"semi": ["error", "always"],
"quotes": ["error", "double", { "allowTemplateLiterals": true }]
}
}

View File

@ -1,3 +1,5 @@
/* eslint-disable no-console */
const express = require("express");
const app = express();
@ -30,14 +32,13 @@ app.post("/v1", function (req, res) {
console.log(req.body);
try {
let myevent = structured1.parse(req.body, req.headers);
const myevent = structured1.parse(req.body, req.headers);
// pretty print
console.log("Accepted event:");
console.log(JSON.stringify(myevent.format(), null, 2));
res.status(201)
.json(myevent.format());
} catch (err) {
console.error(err);
res.status(415)
@ -51,14 +52,13 @@ app.post("/v1/binary", function (req, res) {
console.log(req.body);
try {
let myevent = binary1.parse(req.body, req.headers);
const myevent = binary1.parse(req.body, req.headers);
// pretty print
console.log("Accepted event:");
console.log(JSON.stringify(myevent.format(), null, 2));
res.status(201)
.json(myevent.format());
} catch (err) {
console.error(err);
res.status(415)
@ -72,7 +72,7 @@ app.post("/v03", function (req, res) {
console.log(req.body);
unmarshaller03.unmarshall(req.body, req.headers)
.then(cloudevent => {
.then((cloudevent) => {
// pretty print
console.log("Accepted event:");
console.log(JSON.stringify(cloudevent.format(), null, 2));
@ -80,7 +80,7 @@ app.post("/v03", function (req, res) {
res.status(201)
.json(cloudevent.format());
})
.catch(err => {
.catch((err) => {
console.error(err);
res.status(415)
.header("Content-Type", "application/json")
@ -93,7 +93,7 @@ app.post("/v02", function (req, res) {
console.log(req.body);
unmarshaller02.unmarshall(req.body, req.headers)
.then(cloudevent => {
.then((cloudevent) => {
// pretty print
console.log("Accepted event:");
console.log(JSON.stringify(cloudevent.format(), null, 2));
@ -101,7 +101,7 @@ app.post("/v02", function (req, res) {
res.status(201)
.json(cloudevent.format());
})
.catch(err => {
.catch((err) => {
console.error(err);
res.status(415)
.header("Content-Type", "application/json")

View File

@ -1,4 +1,4 @@
module.exports = {
singleQuote: true,
trailingComma: 'es5',
trailingComma: "es5"
};

View File

@ -13,7 +13,6 @@ function sanityContentType(contentType) {
}
function sanityAndClone(headers) {
const sanityHeaders = {};
Array.from(Object.keys(headers))

View File

@ -1,9 +1,10 @@
var axios = require("axios");
const axios = require("axios");
const Constants = require("./constants.js");
const defaults = {};
defaults[Constants.HEADERS] = {};
defaults[Constants.HEADERS][Constants.HEADER_CONTENT_TYPE] = Constants.DEFAULT_CONTENT_TYPE;
defaults[Constants.HEADERS][Constants.HEADER_CONTENT_TYPE] =
Constants.DEFAULT_CONTENT_TYPE;
function BinaryHTTPEmitter(config, headerByGetter, extensionPrefix) {
this.config = Object.assign({}, defaults, config);

View File

@ -4,42 +4,42 @@ const Constants = require("./constants.js");
const headerByGetter = {};
headerByGetter["getContenttype"] = {
headerByGetter.getContenttype = {
name: Constants.HEADER_CONTENT_TYPE,
parser: (v) => v
};
headerByGetter["getType"] = {
headerByGetter.getType = {
name: "CE-EventType",
parser: (v) => v
};
headerByGetter["getSpecversion"] = {
headerByGetter.getSpecversion = {
name: "CE-CloudEventsVersion",
parser: (v) => v
};
headerByGetter["getSource"] = {
headerByGetter.getSource = {
name: "CE-Source",
parser: (v) => v
};
headerByGetter["getId"] = {
headerByGetter.getId = {
name: "CE-EventID",
parser: (v) => v
};
headerByGetter["getEventTypeVersion"] = {
headerByGetter.getEventTypeVersion = {
name: "CE-EventTypeVersion",
parser: (v) => v
};
headerByGetter["getTime"] = {
headerByGetter.getTime = {
name: "CE-EventTime",
parser: (v) => v
};
headerByGetter["getSchemaurl"] = {
headerByGetter.getSchemaurl = {
name: "CE-SchemaURL",
parser: (v) => v
};
@ -47,27 +47,25 @@ headerByGetter["getSchemaurl"] = {
function HTTPBinary(configuration) {
this.config = JSON.parse(JSON.stringify(configuration));
if(!this.config["headers"]){
this.config["headers"] = {};
if (!this.config.headers) {
this.config.headers = {};
}
this.config["headers"]
[Constants.HEADER_CONTENT_TYPE] =
Constants.MIME_JSON + "; charset=" + Constants.CHARSET_DEFAULT;
this.config.headers[Constants.HEADER_CONTENT_TYPE] =
`${Constants.MIME_JSON}; charset=${Constants.CHARSET_DEFAULT}`;
}
HTTPBinary.prototype.emit = function(cloudevent) {
// Create new request object
const _config = JSON.parse(JSON.stringify(this.config));
// Always set stuff in _config
const _headers = _config["headers"];
const _headers = _config.headers;
Object.keys(headerByGetter)
.filter((getter) => cloudevent[getter]())
.forEach((getter) => {
let header = headerByGetter[getter];
const header = headerByGetter[getter];
_headers[header.name] =
header.parser(
cloudevent[getter]()
@ -75,15 +73,15 @@ HTTPBinary.prototype.emit = function(cloudevent){
});
// Set the cloudevent payload
_config["data"] = cloudevent.format().data;
_config.data = cloudevent.format().data;
// EXTENSION CONTEXT ATTRIBUTES
const exts = cloudevent.getExtensions();
Object.keys(exts)
.filter((ext) => Object.hasOwnProperty.call(exts, ext))
.forEach((ext) => {
let capsExt = ext.charAt(0).toUpperCase() + ext.slice(1);
_headers["CE-X-" + capsExt] = exts[ext];
const capsExt = ext.charAt(0).toUpperCase() + ext.slice(1);
_headers[`CE-X-${capsExt}`] = exts[ext];
});
// Return the Promise

View File

@ -4,37 +4,37 @@ const Constants = require("./constants.js");
const headerByGetter = {};
headerByGetter["getContenttype"] = {
headerByGetter.getContenttype = {
name: Constants.HEADER_CONTENT_TYPE,
parser: (v) => v
};
headerByGetter["getType"] = {
headerByGetter.getType = {
name: Constants.BINARY_HEADERS_02.TYPE,
parser: (v) => v
};
headerByGetter["getSpecversion"] = {
headerByGetter.getSpecversion = {
name: Constants.BINARY_HEADERS_02.SPEC_VERSION,
parser: (v) => v
};
headerByGetter["getSource"] = {
headerByGetter.getSource = {
name: Constants.BINARY_HEADERS_02.SOURCE,
parser: (v) => v
};
headerByGetter["getId"] = {
headerByGetter.getId = {
name: Constants.BINARY_HEADERS_02.ID,
parser: (v) => v
};
headerByGetter["getTime"] = {
headerByGetter.getTime = {
name: Constants.BINARY_HEADERS_02.TIME,
parser: (v) => v
};
headerByGetter["getSchemaurl"] = {
headerByGetter.getSchemaurl = {
name: Constants.BINARY_HEADERS_02.SCHEMA_URL,
parser: (v) => v
};

View File

@ -4,47 +4,47 @@ const Constants = require("./constants.js");
const headerByGetter = {};
headerByGetter["getDataContentType"] = {
headerByGetter.getDataContentType = {
name: Constants.HEADER_CONTENT_TYPE,
parser: (v) => v
};
headerByGetter["getDataContentEncoding"] = {
headerByGetter.getDataContentEncoding = {
name: Constants.BINARY_HEADERS_03.CONTENT_ENCONDING,
parser: (v) => v
};
headerByGetter["getSubject"] = {
headerByGetter.getSubject = {
name: Constants.BINARY_HEADERS_03.SUBJECT,
parser: (v) => v
};
headerByGetter["getType"] = {
headerByGetter.getType = {
name: Constants.BINARY_HEADERS_03.TYPE,
parser: (v) => v
};
headerByGetter["getSpecversion"] = {
headerByGetter.getSpecversion = {
name: Constants.BINARY_HEADERS_03.SPEC_VERSION,
parser: (v) => v
};
headerByGetter["getSource"] = {
headerByGetter.getSource = {
name: Constants.BINARY_HEADERS_03.SOURCE,
parser: (v) => v
};
headerByGetter["getId"] = {
headerByGetter.getId = {
name: Constants.BINARY_HEADERS_03.ID,
parser: (v) => v
};
headerByGetter["getTime"] = {
headerByGetter.getTime = {
name: Constants.BINARY_HEADERS_03.TIME,
parser: (v) => v
};
headerByGetter["getSchemaurl"] = {
headerByGetter.getSchemaurl = {
name: Constants.BINARY_HEADERS_03.SCHEMA_URL,
parser: (v) => v
};

View File

@ -4,42 +4,42 @@ const Constants = require("./constants.js");
const headerByGetter = {};
headerByGetter["getDataContentType"] = {
headerByGetter.getDataContentType = {
name: Constants.HEADER_CONTENT_TYPE,
parser: (v) => v
};
headerByGetter["getSubject"] = {
headerByGetter.getSubject = {
name: Constants.BINARY_HEADERS_1.SUBJECT,
parser: (v) => v
};
headerByGetter["getType"] = {
headerByGetter.getType = {
name: Constants.BINARY_HEADERS_1.TYPE,
parser: (v) => v
};
headerByGetter["getSpecversion"] = {
headerByGetter.getSpecversion = {
name: Constants.BINARY_HEADERS_1.SPEC_VERSION,
parser: (v) => v
};
headerByGetter["getSource"] = {
headerByGetter.getSource = {
name: Constants.BINARY_HEADERS_1.SOURCE,
parser: (v) => v
};
headerByGetter["getId"] = {
headerByGetter.getId = {
name: Constants.BINARY_HEADERS_1.ID,
parser: (v) => v
};
headerByGetter["getTime"] = {
headerByGetter.getTime = {
name: Constants.BINARY_HEADERS_1.TIME,
parser: (v) => v
};
headerByGetter["getDataschema"] = {
headerByGetter.getDataschema = {
name: Constants.BINARY_HEADERS_1.DATA_SCHEMA,
parser: (v) => v
};

View File

@ -3,7 +3,8 @@ const axios = require("axios");
const Constants = require("./constants.js");
const defaults = {};
defaults[Constants.HEADERS] = {};
defaults[Constants.HEADERS][Constants.HEADER_CONTENT_TYPE] = Constants.DEFAULT_CE_CONTENT_TYPE;
defaults[Constants.HEADERS][Constants.HEADER_CONTENT_TYPE] =
Constants.DEFAULT_CE_CONTENT_TYPE;
function StructuredHTTPEmitter(configuration) {
this.config = Object.assign({}, defaults, configuration);

View File

@ -5,23 +5,20 @@ const Constants = require("./constants.js");
function HTTPStructured(configuration) {
this.config = JSON.parse(JSON.stringify(configuration));
if(!this.config["headers"]){
this.config["headers"] = {};
if (!this.config.headers) {
this.config.headers = {};
}
this.config["headers"]
[Constants.HEADER_CONTENT_TYPE] =
Constants.MIME_CE_JSON + "; charset=" + Constants.CHARSET_DEFAULT;
this.config.headers[Constants.HEADER_CONTENT_TYPE] =
`${Constants.MIME_CE_JSON}; charset=${Constants.CHARSET_DEFAULT}`;
}
HTTPStructured.prototype.emit = function(cloudevent) {
// Create new request object
const _config = JSON.parse(JSON.stringify(this.config));
// Set the cloudevent payload
_config["data"] = cloudevent.format();
_config.data = cloudevent.format();
// Return the Promise
return axios.request(_config);

View File

@ -8,7 +8,6 @@ const {
} = require("../../utils/fun.js");
function validateArgs(payload, attributes) {
Array.of(payload)
.filter((p) => isDefinedOrThrow(p,
{ message: "payload is null or undefined" }))
@ -31,7 +30,6 @@ function BinaryHTTPReceiver(
specversion,
extensionsPrefix,
checkDecorator) {
this.parsersByEncoding = parsersByEncoding;
this.setterByHeader = setterByHeader;
this.allowedContentTypes = allowedContentTypes;
@ -57,30 +55,29 @@ BinaryHTTPReceiver.prototype.check = function(payload, headers) {
// Validation Level 1
if (!this.allowedContentTypes
.includes(sanityHeaders[Constants.HEADER_CONTENT_TYPE])) {
throw {
message: "invalid content type",
errors: [sanityHeaders[Constants.HEADER_CONTENT_TYPE]]
};
const err = new TypeError("invalid content type");
err.errors = [sanityHeaders[Constants.HEADER_CONTENT_TYPE]];
throw err;
}
this.requiredHeaders
.filter((required) => !sanityHeaders[required])
.forEach((required) => {
throw {message: "header '" + required + "' not found"};
throw new TypeError(`header '${required}' not found`);
});
if(sanityHeaders[Constants.DEFAULT_SPEC_VERSION_HEADER] !== this.specversion){
throw {
message: "invalid spec version",
errors: [sanityHeaders[Constants.DEFAULT_SPEC_VERSION_HEADER]]
};
if (sanityHeaders[Constants.DEFAULT_SPEC_VERSION_HEADER] !==
this.specversion) {
const err = new TypeError("invalid spec version");
err.errors = [sanityHeaders[Constants.DEFAULT_SPEC_VERSION_HEADER]];
throw err;
}
// No erros! Its contains the minimum required attributes
};
function parserFor(parsersByEncoding, cloudevent, headers) {
let encoding = cloudevent.spec.payload["datacontentencoding"];
const encoding = cloudevent.spec.payload.datacontentencoding;
return parsersByEncoding[encoding][headers[Constants.HEADER_CONTENT_TYPE]];
}
@ -108,7 +105,7 @@ BinaryHTTPReceiver.prototype.parse = function(payload, headers) {
});
// Parses the payload
let parsedPayload =
const parsedPayload =
parserFor(this.parsersByEncoding, cloudevent, sanityHeaders)
.parse(payload);

View File

@ -2,14 +2,8 @@ const Constants = require("./constants.js");
const Spec02 = require("../../specs/spec_0_2.js");
const JSONParser = require("../../formats/json/parser.js");
const BinaryHTTPReceiver = require("./receiver_binary.js");
const {
isDefinedOrThrow,
isStringOrObjectOrThrow
} = require("../../utils/fun.js");
const parserByType = {};
parserByType[Constants.MIME_JSON] = new JSONParser();
parserByType[Constants.MIME_OCTET_STREAM] = {
@ -17,7 +11,7 @@ parserByType[Constants.MIME_OCTET_STREAM] = {
};
const parsersByEncoding = {};
parsersByEncoding[null] = parserByType;
parsersByEncoding.null = parserByType;
parsersByEncoding[undefined] = parserByType;
const allowedContentTypes = [];
@ -37,7 +31,7 @@ setterByHeader[Constants.BINARY_HEADERS_02.TYPE] = {
};
setterByHeader[Constants.BINARY_HEADERS_02.SPEC_VERSION] = {
name: "specversion",
parser : (v) => "0.2"
parser: () => "0.2"
};
setterByHeader[Constants.BINARY_HEADERS_02.SOURCE] = {
name: "source",
@ -60,6 +54,8 @@ setterByHeader[Constants.HEADER_CONTENT_TYPE] = {
parser: (v) => v
};
// Leaving this in place for now. TODO: fixme
// eslint-disable-next-line
function Receiver(configuration) {
this.receiver = new BinaryHTTPReceiver(
parsersByEncoding,

View File

@ -6,11 +6,6 @@ const Base64Parser = require("../../formats/base64.js");
const BinaryHTTPReceiver = require("./receiver_binary.js");
const {
isDefinedOrThrow,
isStringOrObjectOrThrow
} = require("../../utils/fun.js");
const parserByType = {};
parserByType[Constants.MIME_JSON] = new JSONParser();
parserByType[Constants.MIME_OCTET_STREAM] = {
@ -18,7 +13,7 @@ parserByType[Constants.MIME_OCTET_STREAM] = {
};
const parsersByEncoding = {};
parsersByEncoding[null] = parserByType;
parsersByEncoding.null = parserByType;
parsersByEncoding[undefined] = parserByType;
// base64
@ -49,7 +44,7 @@ setterByHeader[Constants.BINARY_HEADERS_03.TYPE] = {
};
setterByHeader[Constants.BINARY_HEADERS_03.SPEC_VERSION] = {
name: "specversion",
parser : (v) => "0.3"
parser: () => "0.3"
};
setterByHeader[Constants.BINARY_HEADERS_03.SOURCE] = {
name: "source",
@ -80,6 +75,8 @@ setterByHeader[Constants.BINARY_HEADERS_03.SUBJECT] = {
parser: (v) => v
};
// Leaving this in place for now. TODO: fixme
// eslint-disable-next-line
function checkDecorator(payload, headers) {
Object.keys(headers)
.map((header) => header.toLocaleLowerCase("en-US"))
@ -87,12 +84,15 @@ function checkDecorator(payload, headers) {
header === Constants.BINARY_HEADERS_03.CONTENT_ENCONDING)
.filter((header) => !allowedEncodings.includes(headers[header]))
.forEach((header) => {
throw {message : "unsupported datacontentencoding", errors: [
headers[header]
]};
// TODO: using forEach here seems off
const err = new TypeError("unsupported datacontentencoding");
err.errors = [headers[header]];
throw err;
});
}
// Leaving this in place for now. TODO: fixme
// eslint-disable-next-line
function Receiver(configuration) {
this.receiver = new BinaryHTTPReceiver(
parsersByEncoding,

View File

@ -7,8 +7,6 @@ const Base64Parser = require("../../formats/base64.js");
const BinaryHTTPReceiver = require("./receiver_binary.js");
const {
isDefinedOrThrow,
isStringOrObjectOrThrow,
isString,
isBase64
} = require("../../utils/fun.js");
@ -20,7 +18,7 @@ parserByType[Constants.MIME_OCTET_STREAM] = {
};
const parsersByEncoding = {};
parsersByEncoding[null] = parserByType;
parsersByEncoding.null = parserByType;
parsersByEncoding[undefined] = parserByType;
// base64
@ -51,7 +49,7 @@ setterByHeader[Constants.BINARY_HEADERS_1.TYPE] = {
};
setterByHeader[Constants.BINARY_HEADERS_1.SPEC_VERSION] = {
name: "specversion",
parser : (v) => "1.0"
parser: () => "1.0"
};
setterByHeader[Constants.BINARY_HEADERS_1.SOURCE] = {
name: "source",
@ -78,9 +76,13 @@ setterByHeader[Constants.BINARY_HEADERS_1.SUBJECT] = {
parser: (v) => v
};
// Leaving these in place for now. TODO: fixme
// eslint-disable-next-line
function checkDecorator(payload, headers) {
}
// Leaving this in place for now. TODO: fixme
// eslint-disable-next-line
function Receiver(configuration) {
this.receiver = new BinaryHTTPReceiver(
parsersByEncoding,

View File

@ -26,7 +26,6 @@ function StructuredHTTPReceiver(
setterByAttribute,
allowedContentTypes,
Spec) {
this.parserByMime = parserByMime;
this.setterByAttribute = setterByAttribute;
this.allowedContentTypes = allowedContentTypes;
@ -42,10 +41,9 @@ StructuredHTTPReceiver.prototype.check = function(payload, headers) {
// Validation Level 1
if (!this.allowedContentTypes
.includes(sanityHeaders[Constants.HEADER_CONTENT_TYPE])) {
throw {
message: "invalid content type",
errors: [sanityHeaders[Constants.HEADER_CONTENT_TYPE]]
};
const err = new TypeError("invalid content type");
err.errors = [sanityHeaders[Constants.HEADER_CONTENT_TYPE]];
throw err;
}
// No erros! Its contains the minimum required attributes
@ -68,8 +66,8 @@ StructuredHTTPReceiver.prototype.parse = function(payload, headers) {
Array.from(Object.keys(this.setterByAttribute))
.filter((attribute) => event[attribute])
.forEach((attribute) => {
let setterName = this.setterByAttribute[attribute].name;
let parserFun = this.setterByAttribute[attribute].parser;
const setterName = this.setterByAttribute[attribute].name;
const parserFun = this.setterByAttribute[attribute].parser;
// invoke the setter function
cloudevent[setterName](parserFun(event[attribute]));

View File

@ -4,11 +4,6 @@ const JSONParser = require("../../formats/json/parser.js");
const StructuredHTTPReceiver = require("./receiver_structured.js");
const {
isDefinedOrThrow,
isStringOrObjectOrThrow
} = require("../../utils/fun.js");
const jsonParserSpec02 = new JSONParser();
const parserByMime = {};
@ -52,6 +47,8 @@ setterByAttribute[Constants.STRUCTURED_ATTRS_02.DATA] = {
parser: (v) => v
};
// Leaving this in place for now. TODO: fixme
// eslint-disable-next-line
function Receiver(configuration) {
this.receiver = new StructuredHTTPReceiver(
parserByMime,

View File

@ -4,11 +4,6 @@ const JSONParser = require("../../formats/json/parser.js");
const StructuredHTTPReceiver = require("./receiver_structured.js");
const {
isDefinedOrThrow,
isStringOrObjectOrThrow
} = require("../../utils/fun.js");
const jsonParserSpec = new JSONParser();
const parserByMime = {};
@ -60,6 +55,8 @@ setterByAttribute[Constants.STRUCTURED_ATTRS_03.DATA] = {
parser: (v) => v
};
// Leaving this in place for now. TODO: fixme
// eslint-disable-next-line
function Receiver(configuration) {
this.receiver = new StructuredHTTPReceiver(
parserByMime,

View File

@ -4,11 +4,6 @@ const JSONParser = require("../../formats/json/parser.js");
const StructuredHTTPReceiver = require("./receiver_structured.js");
const {
isDefinedOrThrow,
isStringOrObjectOrThrow
} = require("../../utils/fun.js");
const jsonParserSpec = new JSONParser();
const parserByMime = {};
@ -60,6 +55,8 @@ setterByAttribute[Constants.STRUCTURED_ATTRS_1.DATA_BASE64] = {
parser: (v) => v
};
// Leaving this in place for now. TODO: fixme
// eslint-disable-next-line
function Receiver(configuration) {
this.receiver = new StructuredHTTPReceiver(
parserByMime,

View File

@ -1,17 +1,9 @@
const StructuredReceiver = require("./receiver_structured_0_2.js");
const BinaryReceiver = require("./receiver_binary_0_2.js");
const Constants = require("./constants.js");
const Commons = require("./commons.js");
const STRUCTURED = "structured";
const BINARY = "binary";
const receiverByBinding = {
structured : new StructuredReceiver(),
binary : new BinaryReceiver(),
};
const allowedBinaryContentTypes = [];
allowedBinaryContentTypes.push(Constants.MIME_JSON);
allowedBinaryContentTypes.push(Constants.MIME_OCTET_STREAM);
@ -21,17 +13,16 @@ allowedStructuredContentTypes.push(Constants.MIME_CE_JSON);
function validateArgs(payload, headers) {
if (!payload) {
throw {message: "payload is null or undefined"};
throw new TypeError("payload is null or undefined");
}
if (!headers) {
throw {message: "headers is null or undefined"};
throw new TypeError("headers is null or undefined");
}
}
// Is it binary or structured?
function resolveBindingName(payload, headers) {
const contentType =
Commons.sanityContentType(headers[Constants.HEADER_CONTENT_TYPE]);
@ -40,14 +31,18 @@ function resolveBindingName(payload, headers) {
if (allowedStructuredContentTypes.includes(contentType)) {
return STRUCTURED;
} else {
throw {message: "structured+type not allowed", errors: [contentType]};
const err = new TypeError("structured+type not allowed");
err.errors = [contentType];
throw err;
}
} else {
// Binary
if (allowedBinaryContentTypes.includes(contentType)) {
return BINARY;
} else {
throw {message: "content type not allowed", errors : [contentType]};
const err = new TypeError("content type not allowed");
err.errors = [contentType];
throw err;
}
}
}
@ -65,14 +60,13 @@ Unmarshaller.prototype.unmarshall = function(payload, headers) {
// Validation level 1
if (!sanityHeaders[Constants.HEADER_CONTENT_TYPE]) {
throw {message: "content-type header not found"};
throw new TypeError("content-type header not found");
}
// Resolve the binding
const bindingName = resolveBindingName(payload, sanityHeaders);
const cloudevent =
this.receiverByBinding[bindingName].parse(payload, sanityHeaders);
const cloudevent = this.receiverByBinding[bindingName]
.parse(payload, sanityHeaders);
resolve(cloudevent);
} catch (e) {

View File

@ -5,7 +5,7 @@ const BinaryReceiver = require("./receiver_binary_0_2.js");
const RECEIVER_BY_BINDING = {
structured: new StructuredReceiver(),
binary : new BinaryReceiver(),
binary: new BinaryReceiver()
};
const Unmarshaller = function() {

View File

@ -5,7 +5,7 @@ const BinaryReceiver = require("./receiver_binary_0_3.js");
const RECEIVER_BY_BINDING = {
structured: new StructuredReceiver(),
binary : new BinaryReceiver(),
binary: new BinaryReceiver()
};
const Unmarshaller = function() {

View File

@ -13,7 +13,7 @@ const {HTTPBinary02} = require("./bindings/http/emitter_binary_0_2.js");
*/
function Cloudevent(_spec, _formatter) {
this.spec = (_spec) ? new _spec(Cloudevent) : new Spec01(Cloudevent);
this.formatter = (_formatter) ? _formatter : new JSONFormatter01();
this.formatter = (_formatter) || new JSONFormatter01();
// The map of extensions
this.extensions = {};
@ -121,20 +121,19 @@ Cloudevent.prototype.getExtensions = function() {
return this.extensions;
};
/*
* Export the specs
*/
Cloudevent.specs = {
"0.1": Spec01,
"0.2": Spec02
0.1: Spec01,
0.2: Spec02
};
/*
* Export the formats
*/
Cloudevent.formats = {
"json" : JSONFormatter01,
json: JSONFormatter01,
"json0.1": JSONFormatter01
};

View File

@ -17,12 +17,9 @@ const nullOrIndefinedPayload =
// Function
const asJSON = (v) => (isString(v) ? JSON.parse(v) : v);
/**
* Level 0 of validation: is that string? is that JSON?
*/
// Level 0 of validation: is that string? is that JSON?
function validateAndParse(payload) {
const json =
var json =
Array.of(payload)
.filter((p) => isDefinedOrThrow(p, nullOrIndefinedPayload))
.filter((p) => isStringOrObjectOrThrow(p, invalidPayloadTypeError))
@ -32,17 +29,6 @@ function validateAndParse(payload) {
return json;
}
/*
* Level 1 of validation: is that follow a spec?
*/
function validateSpec(payload, spec) {
// is that follow the spec?
spec.check(payload);
return payload;
}
JSONParser.prototype.parse = function(payload) {
let toparse = payload;

View File

@ -29,94 +29,92 @@ function Spec01(_caller){
* throw an error if do not pass.
*/
Spec01.prototype.check = function() {
if(!this.payload["eventType"]){
throw {message: "'eventType' is invalid"};
if (!this.payload.eventType) {
throw new TypeError("'eventType' is invalid");
}
};
Spec01.prototype.type = function(_type) {
this.payload["eventType"] = _type;
this.payload.eventType = _type;
return this;
};
Spec01.prototype.getType = function() {
return this.payload["eventType"];
return this.payload.eventType;
};
Spec01.prototype.getSpecversion = function() {
return this.payload["cloudEventsVersion"];
return this.payload.cloudEventsVersion;
};
Spec01.prototype.eventTypeVersion = function(version) {
this.payload["eventTypeVersion"] = version;
this.payload.eventTypeVersion = version;
return this;
};
Spec01.prototype.getEventTypeVersion = function() {
return this.payload["eventTypeVersion"];
return this.payload.eventTypeVersion;
};
Spec01.prototype.source = function(_source) {
this.payload["source"] = _source;
this.payload.source = _source;
return this;
};
Spec01.prototype.getSource = function() {
return this.payload["source"];
return this.payload.source;
};
Spec01.prototype.id = function(_id) {
this.payload["eventID"] = _id;
this.payload.eventID = _id;
return this;
};
Spec01.prototype.getId = function() {
return this.payload["eventID"];
return this.payload.eventID;
};
Spec01.prototype.time = function(_time) {
this.payload["eventTime"] = _time.toISOString();
this.payload.eventTime = _time.toISOString();
return this;
};
Spec01.prototype.getTime = function() {
return this.payload["eventTime"];
return this.payload.eventTime;
};
Spec01.prototype.schemaurl = function(_schemaurl) {
this.payload["schemaURL"] = _schemaurl;
this.payload.schemaURL = _schemaurl;
return this;
};
Spec01.prototype.getSchemaurl = function() {
return this.payload["schemaURL"];
return this.payload.schemaURL;
};
Spec01.prototype.contenttype = function(_contenttype) {
this.payload["contentType"] = _contenttype;
this.payload.contentType = _contenttype;
return this;
};
Spec01.prototype.getContenttype = function() {
return this.payload["contentType"];
return this.payload.contentType;
};
Spec01.prototype.data = function(_data) {
this.payload["data"] = _data;
this.payload.data = _data;
return this;
};
Spec01.prototype.getData = function() {
return this.payload["data"];
return this.payload.data;
};
Spec01.prototype.addExtension = function(key, value) {
if(!this.payload["extensions"]){
this.payload["extensions"] = {};
if (!this.payload.extensions) {
this.payload.extensions = {};
}
this.payload["extensions"][key] = value;
this.payload.extensions[key] = value;
return this;
};

View File

@ -1,5 +1,5 @@
var uuid = require("uuid/v4");
var Ajv = require("ajv");
const uuid = require("uuid/v4");
const Ajv = require("ajv");
// Reserved attributes names
const reserved = {
@ -16,7 +16,8 @@ const reserved = {
const schema = require("../../ext/spec_0_2.json");
const ajv = new Ajv({
extendRefs: true // validate all keywords in the schemas with $ref (the default behaviour in versions before 5.0.0)
// validate all keywords in the schemas with $ref
extendRefs: true
});
const validate = ajv.compile(schema);
@ -39,87 +40,89 @@ Spec02.prototype.check = function(ce){
const valid = validate(toCheck);
if (!valid) {
throw {message: "invalid payload", errors: validate.errors};
const err = new TypeError("invalid payload");
err.errors = validate.errors;
throw err;
}
};
Spec02.prototype.type = function(_type) {
this.payload["type"] = _type;
this.payload.type = _type;
return this;
};
Spec02.prototype.getType = function() {
return this.payload["type"];
return this.payload.type;
};
Spec02.prototype.specversion = function(_specversion){
Spec02.prototype.specversion = function() {
// does not set! This is right
return this;
};
Spec02.prototype.getSpecversion = function() {
return this.payload["specversion"];
return this.payload.specversion;
};
Spec02.prototype.source = function(_source) {
this.payload["source"] = _source;
this.payload.source = _source;
return this;
};
Spec02.prototype.getSource = function() {
return this.payload["source"];
return this.payload.source;
};
Spec02.prototype.id = function(_id) {
this.payload["id"] = _id;
this.payload.id = _id;
return this;
};
Spec02.prototype.getId = function() {
return this.payload["id"];
return this.payload.id;
};
Spec02.prototype.time = function(_time) {
this.payload["time"] = _time.toISOString();
this.payload.time = _time.toISOString();
return this;
};
Spec02.prototype.getTime = function() {
return this.payload["time"];
return this.payload.time;
};
Spec02.prototype.schemaurl = function(_schemaurl) {
this.payload["schemaurl"] = _schemaurl;
this.payload.schemaurl = _schemaurl;
return this;
};
Spec02.prototype.getSchemaurl = function() {
return this.payload["schemaurl"];
return this.payload.schemaurl;
};
Spec02.prototype.contenttype = function(_contenttype) {
this.payload["contenttype"] = _contenttype;
this.payload.contenttype = _contenttype;
return this;
};
Spec02.prototype.getContenttype = function() {
return this.payload["contenttype"];
return this.payload.contenttype;
};
Spec02.prototype.data = function(_data) {
this.payload["data"] = _data;
this.payload.data = _data;
return this;
};
Spec02.prototype.getData = function() {
return this.payload["data"];
return this.payload.data;
};
Spec02.prototype.addExtension = function(key, value) {
if(!reserved.hasOwnProperty(key)){
if (!Object.prototype.hasOwnProperty.call(reserved, key)) {
this.payload[key] = value;
} else {
throw {message: "Reserved attribute name: '" + key + "'"};
throw new TypeError(`Reserved attribute name: '${key}'`);
}
return this;
};

View File

@ -2,7 +2,6 @@ const uuid = require("uuid/v4");
const Ajv = require("ajv");
const {
equalsOrThrow,
isBase64,
clone,
asData
@ -22,7 +21,7 @@ const RESERVED_ATTRIBUTES = {
};
const SUPPORTED_CONTENT_ENCODING = {};
SUPPORTED_CONTENT_ENCODING["base64"] = {
SUPPORTED_CONTENT_ENCODING.base64 = {
check: (data) => isBase64(data)
};
@ -84,24 +83,28 @@ Spec03.prototype.check = function(ce){
const toCheck = (!ce ? this.payload : ce);
if (!isValidAgainstSchema(toCheck)) {
throw {message: "invalid payload", errors: isValidAgainstSchema.errors};
const err = new TypeError("invalid payload");
err.errors = isValidAgainstSchema.errors;
throw err;
}
Array.of(toCheck)
.filter((tc) => tc["datacontentencoding"])
.map((tc) => tc["datacontentencoding"].toLocaleLowerCase("en-US"))
.filter((tc) => tc.datacontentencoding)
.map((tc) => tc.datacontentencoding.toLocaleLowerCase("en-US"))
.filter((dce) => !Object.keys(SUPPORTED_CONTENT_ENCODING).includes(dce))
.forEach((dce) => {
throw {message: "invalid payload", errors: [
"Unsupported content encoding: " + dce
]};
const err = new TypeError("invalid payload");
err.errors = [
`Unsupported content encoding: ${dce}`
];
throw err;
});
Array.of(toCheck)
.filter((tc) => tc["datacontentencoding"])
.filter((tc) => tc.datacontentencoding)
.filter((tc) => (typeof tc.data) === "string")
.map((tc) => {
let newtc = clone(tc);
const newtc = clone(tc);
newtc.datacontentencoding =
newtc.datacontentencoding.toLocaleLowerCase("en-US");
@ -112,118 +115,120 @@ Spec03.prototype.check = function(ce){
.filter((tc) => !SUPPORTED_CONTENT_ENCODING[tc.datacontentencoding]
.check(tc.data))
.forEach((tc) => {
throw {message: "invalid payload", errors: [
"Invalid content encoding of data: " + tc.data
]};
const err = new TypeError("invalid payload");
err.errors = [
`Invalid content encoding of data: ${tc.data}`
];
throw err;
});
};
Spec03.prototype.id = function(_id) {
this.payload["id"] = _id;
this.payload.id = _id;
return this;
};
Spec03.prototype.getId = function() {
return this.payload["id"];
return this.payload.id;
};
Spec03.prototype.source = function(_source) {
this.payload["source"] = _source;
this.payload.source = _source;
return this;
};
Spec03.prototype.getSource = function() {
return this.payload["source"];
return this.payload.source;
};
Spec03.prototype.specversion = function(_specversion){
Spec03.prototype.specversion = function() {
// does not set! This is right
return this;
};
Spec03.prototype.getSpecversion = function() {
return this.payload["specversion"];
return this.payload.specversion;
};
Spec03.prototype.type = function(_type) {
this.payload["type"] = _type;
this.payload.type = _type;
return this;
};
Spec03.prototype.getType = function() {
return this.payload["type"];
return this.payload.type;
};
Spec03.prototype.dataContentEncoding = function(encoding) {
this.payload["datacontentencoding"] = encoding;
this.payload.datacontentencoding = encoding;
return this;
};
Spec03.prototype.getDataContentEncoding = function() {
return this.payload["datacontentencoding"];
return this.payload.datacontentencoding;
};
// maps to datacontenttype
Spec03.prototype.contenttype = function(_contenttype) {
this.payload["datacontenttype"] = _contenttype;
this.payload.datacontenttype = _contenttype;
return this;
};
Spec03.prototype.getContenttype = function() {
return this.payload["datacontenttype"];
return this.payload.datacontenttype;
};
Spec03.prototype.dataContentType = function(_contenttype) {
this.payload["datacontenttype"] = _contenttype;
this.payload.datacontenttype = _contenttype;
return this;
};
Spec03.prototype.getDataContentType = function() {
return this.payload["datacontenttype"];
return this.payload.datacontenttype;
};
Spec03.prototype.schemaurl = function(_schemaurl) {
this.payload["schemaurl"] = _schemaurl;
this.payload.schemaurl = _schemaurl;
return this;
};
Spec03.prototype.getSchemaurl = function() {
return this.payload["schemaurl"];
return this.payload.schemaurl;
};
Spec03.prototype.subject = function(_subject) {
this.payload["subject"] = _subject;
this.payload.subject = _subject;
return this;
};
Spec03.prototype.getSubject = function() {
return this.payload["subject"];
return this.payload.subject;
};
Spec03.prototype.time = function(_time) {
this.payload["time"] = _time.toISOString();
this.payload.time = _time.toISOString();
return this;
};
Spec03.prototype.getTime = function() {
return this.payload["time"];
return this.payload.time;
};
Spec03.prototype.data = function(_data) {
this.payload["data"] = _data;
this.payload.data = _data;
return this;
};
Spec03.prototype.getData = function() {
let dct = this.payload["datacontenttype"];
let dce = this.payload["datacontentencoding"];
const dct = this.payload.datacontenttype;
const dce = this.payload.datacontentencoding;
if (dct && !dce) {
this.payload["data"] = asData(this.payload["data"], dct);
this.payload.data = asData(this.payload.data, dct);
}
return this.payload["data"];
return this.payload.data;
};
Spec03.prototype.addExtension = function(key, value) {
if(!RESERVED_ATTRIBUTES.hasOwnProperty(key)){
if (!Object.prototype.hasOwnProperty.call(RESERVED_ATTRIBUTES, key)) {
this.payload[key] = value;
} else {
throw {message: "Reserved attribute name: '" + key + "'"};
throw new TypeError(`Reserved attribute name: '${key}'`);
}
return this;
};

View File

@ -83,17 +83,18 @@ function Spec1(_caller) {
this.spec.check();
// Check before getData() call
let isbin = isBinary(this.spec.payload[RESERVED_ATTRIBUTES.data]);
const isbin = isBinary(this.spec.payload[RESERVED_ATTRIBUTES.data]);
// May be used, if isbin==true
let payload = clone(this.spec.payload);
const payload = clone(this.spec.payload);
// To run asData()
this.getData();
// Handle when is binary, creating the data_base64
if (isbin) {
payload[RESERVED_ATTRIBUTES.data_base64] = this.spec.payload[RESERVED_ATTRIBUTES.data];
payload[RESERVED_ATTRIBUTES.data_base64] =
this.spec.payload[RESERVED_ATTRIBUTES.data];
delete payload[RESERVED_ATTRIBUTES.data];
return this.formatter.format(payload);
@ -108,104 +109,106 @@ function Spec1(_caller) {
* Check the spec constraints
*/
Spec1.prototype.check = function(ce) {
const toCheck = (!ce ? this.payload : ce);
var toCheck = (!ce ? this.payload : ce);
if (!isValidAgainstSchema(toCheck)) {
throw {message: "invalid payload", errors: isValidAgainstSchema.errors};
const err = new TypeError("invalid payload");
err.errors = isValidAgainstSchema.errors;
throw err;
}
};
Spec1.prototype.id = function(_id) {
this.payload["id"] = _id;
this.payload.id = _id;
return this;
};
Spec1.prototype.getId = function() {
return this.payload["id"];
return this.payload.id;
};
Spec1.prototype.source = function(_source) {
this.payload["source"] = _source;
this.payload.source = _source;
return this;
};
Spec1.prototype.getSource = function() {
return this.payload["source"];
return this.payload.source;
};
Spec1.prototype.specversion = function(_specversion){
Spec1.prototype.specversion = function() {
// does not set! This is right
return this;
};
Spec1.prototype.getSpecversion = function() {
return this.payload["specversion"];
return this.payload.specversion;
};
Spec1.prototype.type = function(_type) {
this.payload["type"] = _type;
this.payload.type = _type;
return this;
};
Spec1.prototype.getType = function() {
return this.payload["type"];
return this.payload.type;
};
Spec1.prototype.dataContentType = function(_contenttype) {
this.payload["datacontenttype"] = _contenttype;
this.payload.datacontenttype = _contenttype;
return this;
};
Spec1.prototype.getDataContentType = function() {
return this.payload["datacontenttype"];
return this.payload.datacontenttype;
};
Spec1.prototype.dataschema = function(_schema) {
this.payload["dataschema"] = _schema;
this.payload.dataschema = _schema;
return this;
};
Spec1.prototype.getDataschema = function() {
return this.payload["dataschema"];
return this.payload.dataschema;
};
Spec1.prototype.subject = function(_subject) {
this.payload["subject"] = _subject;
this.payload.subject = _subject;
return this;
};
Spec1.prototype.getSubject = function() {
return this.payload["subject"];
return this.payload.subject;
};
Spec1.prototype.time = function(_time) {
this.payload["time"] = _time.toISOString();
this.payload.time = _time.toISOString();
return this;
};
Spec1.prototype.getTime = function() {
return this.payload["time"];
return this.payload.time;
};
Spec1.prototype.data = function(_data) {
this.payload["data"] = _data;
this.payload.data = _data;
return this;
};
Spec1.prototype.getData = function() {
let dct = this.payload["datacontenttype"];
const dct = this.payload.datacontenttype;
if (dct) {
this.payload["data"] = asData(this.payload["data"], dct);
this.payload.data = asData(this.payload.data, dct);
}
return this.payload["data"];
return this.payload.data;
};
Spec1.prototype.addExtension = function(key, value) {
if(!RESERVED_ATTRIBUTES.hasOwnProperty(key)){
if (!Object.prototype.hasOwnProperty.call(RESERVED_ATTRIBUTES, key)) {
if (isValidType(value)) {
this.payload[key] = value;
} else {
throw {message: "Invalid type of extension value"};
throw new TypeError("Invalid type of extension value");
}
} else {
throw {message: "Reserved attribute name: '" + key + "'"};
throw new TypeError(`Reserved attribute name: '${key}'`);
}
return this;
};

View File

@ -1,7 +1,7 @@
// Functional approach
const isString = (v) => (typeof v) === "string";
const isObject = (v) => (typeof v) === "object";
const isDefined = (v) => v && (typeof v) != "undefined";
const isDefined = (v) => v && (typeof v) !== "undefined";
const isBoolean = (v) => (typeof v) === "boolean";
const isInteger = (v) => Number.isInteger(v);
@ -41,7 +41,7 @@ const asBuffer = (value) =>
? Buffer.from(value)
: isBuffer(value)
? value
: (() => {throw {message: "is not buffer or a valid binary"};})();
: (() => { throw new TypeError("is not buffer or a valid binary"); })();
const asBase64 = (value) =>
asBuffer(value).toString("base64");
@ -56,7 +56,9 @@ const asData = (data, contentType) => {
let result = data;
// pattern matching alike
result = isString(result) && !isBase64(result) && isJsonContentType(contentType)
result = isString(result) &&
!isBase64(result) &&
isJsonContentType(contentType)
? JSON.parse(result)
: result;

1524
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -4,6 +4,8 @@
"description": "CloudEvents SDK for JavaScript",
"main": "index.js",
"scripts": {
"lint": "standardx",
"pretest": "npm run lint",
"test": "mocha test/**/*.js",
"coverage": "nyc --reporter=lcov --reporter=text npm run test",
"precoverage-publish": "npm run coverage",
@ -40,7 +42,11 @@
"chai": "~4.2.0",
"mocha": "~7.1.1",
"nock": "~12.0.3",
"nyc": "~15.0.0"
"nyc": "~15.0.0",
"eslint-config-standard": "^14.1.1",
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-node": "^11.1.0",
"standardx": "^5.0.0"
},
"publishConfig": {
"access": "public"

View File

@ -150,7 +150,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.2", () => {
it("Cloudevent contains 'type'", () => {
// setup
var payload = {
"data" : "dataString"
data: "dataString"
};
var attributes = {
"ce-type": "type",
@ -173,7 +173,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.2", () => {
it("Cloudevent contains 'specversion'", () => {
// setup
var payload = {
"data" : "dataString"
data: "dataString"
};
var attributes = {
"ce-type": "type",
@ -196,7 +196,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.2", () => {
it("Cloudevent contains 'source'", () => {
// setup
var payload = {
"data" : "dataString"
data: "dataString"
};
var attributes = {
"ce-type": "type",
@ -219,7 +219,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.2", () => {
it("Cloudevent contains 'id'", () => {
// setup
var payload = {
"data" : "dataString"
data: "dataString"
};
var attributes = {
"ce-type": "type",
@ -242,7 +242,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.2", () => {
it("Cloudevent contains 'time'", () => {
// setup
var payload = {
"data" : "dataString"
data: "dataString"
};
var attributes = {
"ce-type": "type",
@ -265,7 +265,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.2", () => {
it("Cloudevent contains 'schemaurl'", () => {
// setup
var payload = {
"data" : "dataString"
data: "dataString"
};
var attributes = {
"ce-type": "type",
@ -288,7 +288,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.2", () => {
it("Cloudevent contains 'contenttype' (application/json)", () => {
// setup
var payload = {
"data" : "dataString"
data: "dataString"
};
var attributes = {
"ce-type": "type",
@ -332,7 +332,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.2", () => {
it("Cloudevent contains 'data' (application/json)", () => {
// setup
var payload = {
"data" : "dataString"
data: "dataString"
};
var attributes = {
"ce-type": "type",
@ -376,7 +376,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.2", () => {
it("No error when all attributes are in place", () => {
// setup
var payload = {
"data" : "dataString"
data: "dataString"
};
var attributes = {
"ce-type": "type",
@ -403,7 +403,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.2", () => {
// setup
var extension1 = "mycuston-ext1";
var payload = {
"data" : "dataString"
data: "dataString"
};
var attributes = {
"ce-type": "type",
@ -421,7 +421,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.2", () => {
var actualExtensions = actual.getExtensions();
// assert
expect(actualExtensions["extension1"])
expect(actualExtensions.extension1)
.to.equal(extension1);
});
});

View File

@ -150,7 +150,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.3", () => {
it("Cloudevent contains 'type'", () => {
// setup
var payload = {
"data" : "dataString"
data: "dataString"
};
var attributes = {
"ce-type": "type",
@ -173,7 +173,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.3", () => {
it("Cloudevent contains 'specversion'", () => {
// setup
var payload = {
"data" : "dataString"
data: "dataString"
};
var attributes = {
"ce-type": "type",
@ -196,7 +196,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.3", () => {
it("Cloudevent contains 'source'", () => {
// setup
var payload = {
"data" : "dataString"
data: "dataString"
};
var attributes = {
"ce-type": "type",
@ -219,7 +219,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.3", () => {
it("Cloudevent contains 'id'", () => {
// setup
var payload = {
"data" : "dataString"
data: "dataString"
};
var attributes = {
"ce-type": "type",
@ -242,7 +242,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.3", () => {
it("Cloudevent contains 'time'", () => {
// setup
var payload = {
"data" : "dataString"
data: "dataString"
};
var attributes = {
"ce-type": "type",
@ -265,7 +265,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.3", () => {
it("Cloudevent contains 'schemaurl'", () => {
// setup
var payload = {
"data" : "dataString"
data: "dataString"
};
var attributes = {
"ce-type": "type",
@ -288,7 +288,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.3", () => {
it("Cloudevent contains 'contenttype' (application/json)", () => {
// setup
var payload = {
"data" : "dataString"
data: "dataString"
};
var attributes = {
"ce-type": "type",
@ -332,7 +332,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.3", () => {
it("Cloudevent contains 'data' (application/json)", () => {
// setup
var payload = {
"data" : "dataString"
data: "dataString"
};
var attributes = {
"ce-type": "type",
@ -376,7 +376,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.3", () => {
it("No error when all attributes are in place", () => {
// setup
var payload = {
"data" : "dataString"
data: "dataString"
};
var attributes = {
"ce-type": "type",
@ -403,7 +403,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.3", () => {
// setup
var extension1 = "mycuston-ext1";
var payload = {
"data" : "dataString"
data: "dataString"
};
var attributes = {
"ce-type": "type",
@ -421,7 +421,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.3", () => {
var actualExtensions = actual.getExtensions();
// assert
expect(actualExtensions["extension1"])
expect(actualExtensions.extension1)
.to.equal(extension1);
});
});

View File

@ -151,7 +151,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v1.0", () => {
it("Cloudevent contains 'type'", () => {
// setup
var payload = {
"data" : "dataString"
data: "dataString"
};
var attributes = {
"ce-type": "type",
@ -174,7 +174,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v1.0", () => {
it("Cloudevent contains 'specversion'", () => {
// setup
var payload = {
"data" : "dataString"
data: "dataString"
};
var attributes = {
"ce-type": "type",
@ -197,7 +197,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v1.0", () => {
it("Cloudevent contains 'source'", () => {
// setup
var payload = {
"data" : "dataString"
data: "dataString"
};
var attributes = {
"ce-type": "type",
@ -220,7 +220,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v1.0", () => {
it("Cloudevent contains 'id'", () => {
// setup
var payload = {
"data" : "dataString"
data: "dataString"
};
var attributes = {
"ce-type": "type",
@ -243,7 +243,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v1.0", () => {
it("Cloudevent contains 'time'", () => {
// setup
var payload = {
"data" : "dataString"
data: "dataString"
};
var attributes = {
"ce-type": "type",
@ -266,7 +266,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v1.0", () => {
it("Cloudevent contains 'dataschema'", () => {
// setup
var payload = {
"data" : "dataString"
data: "dataString"
};
var attributes = {
"ce-type": "type",
@ -289,7 +289,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v1.0", () => {
it("Cloudevent contains 'contenttype' (application/json)", () => {
// setup
var payload = {
"data" : "dataString"
data: "dataString"
};
var attributes = {
"ce-type": "type",
@ -333,7 +333,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v1.0", () => {
it("Cloudevent contains 'data' (application/json)", () => {
// setup
var payload = {
"data" : "dataString"
data: "dataString"
};
var attributes = {
"ce-type": "type",
@ -377,11 +377,12 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v1.0", () => {
it("The content of 'data' is base64 for binary", () => {
// setup
var expected = {
"data" : "dataString"
data: "dataString"
};
let bindata = Uint32Array.from(JSON.stringify(expected), (c) => c.codePointAt(0));
let payload = asBase64(bindata);
const bindata = Uint32Array
.from(JSON.stringify(expected), (c) => c.codePointAt(0));
const payload = asBase64(bindata);
var attributes = {
"ce-type": "type",
@ -404,7 +405,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v1.0", () => {
it("No error when all attributes are in place", () => {
// setup
var payload = {
"data" : "dataString"
data: "dataString"
};
var attributes = {
"ce-type": "type",
@ -431,7 +432,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v1.0", () => {
// setup
var extension1 = "mycuston-ext1";
var payload = {
"data" : "dataString"
data: "dataString"
};
var attributes = {
"ce-type": "type",
@ -449,7 +450,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v1.0", () => {
var actualExtensions = actual.getExtensions();
// assert
expect(actualExtensions["extension1"])
expect(actualExtensions.extension1)
.to.equal(extension1);
});
});

View File

@ -1,6 +1,5 @@
var expect = require("chai").expect;
var Cloudevent = require("../../../index.js");
var Spec02 = require("../../../lib/specs/spec_0_2.js");
var HTTPStructuredReceiver02 =
require("../../../lib/bindings/http/receiver_structured_0_2.js");
@ -9,8 +8,6 @@ var receiver = new HTTPStructuredReceiver02();
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";
const now = new Date();
const schemaurl = "http://cloudevents.io/schema.json";
@ -20,23 +17,8 @@ const data = {
foo: "bar"
};
const ext1Name = "extension1";
const ext1Value = "foobar";
const ext2Name = "extension2";
const ext2Value = "acme";
var cloudevent =
new Cloudevent(Spec02)
.type(type)
.source(source)
.contenttype(ceContentType)
.time(now)
.schemaurl(schemaurl)
.data(data)
.addExtension(ext1Name, ext1Value)
.addExtension(ext2Name, ext2Value);
describe("HTTP Transport Binding Structured Receiver for CloudEvents v0.2", () => {
describe("HTTP Transport Binding Structured Receiver for CloudEvents v0.2",
() => {
describe("Check", () => {
it("Throw error when payload arg is null or undefined", () => {
// setup
@ -148,7 +130,7 @@ describe("HTTP Transport Binding Structured Receiver for CloudEvents v0.2", () =
it("Should accept 'extension1'", () => {
// setup
var extension1 = "mycuston-ext1"
var extension1 = "mycuston-ext1";
var payload =
new Cloudevent(Cloudevent.specs["0.2"])
.type(type)
@ -169,7 +151,7 @@ describe("HTTP Transport Binding Structured Receiver for CloudEvents v0.2", () =
var actualExtensions = actual.getExtensions();
// assert
expect(actualExtensions["extension1"])
expect(actualExtensions.extension1)
.to.equal(extension1);
});
});

View File

@ -1,7 +1,6 @@
var expect = require("chai").expect;
var v03 = require("../../../v03/index.js");
var Cloudevent = require("../../../index.js");
var Spec = require("../../../lib/specs/spec_0_3.js");
var HTTPStructuredReceiver =
require("../../../lib/bindings/http/receiver_structured_0_3.js");
@ -10,9 +9,6 @@ var receiver = new HTTPStructuredReceiver();
const type = "com.github.pull.create";
const source = "urn:event:from:myapi/resourse/123";
const webhook = "https://cloudevents.io/webhook";
const contentEncoding = "base64";
const contentType = "application/cloudevents+json; charset=utf-8";
const now = new Date();
const schemaurl = "http://cloudevents.io/schema.json";
@ -21,35 +17,14 @@ const ceContentType = "application/json";
const data = {
foo: "bar"
};
const dataBase64 = "Y2xvdWRldmVudHMK";
const ext1Name = "extension1";
const ext1Value = "foobar";
const ext2Name = "extension2";
const ext2Value = "acme";
var cloudevent = v03.event()
.type(type)
.source(source)
.contenttype(ceContentType)
.time(now)
.schemaurl(schemaurl)
.data(data)
.addExtension(ext1Name, ext1Value)
.addExtension(ext2Name, ext2Value);
const cebase64 = v03.event()
.type(type)
.source(source)
.dataContentType(ceContentType)
.dataContentEncoding(contentEncoding)
.time(now)
.schemaurl(schemaurl)
.data(dataBase64)
.addExtension(ext1Name, ext1Value)
.addExtension(ext2Name, ext2Value);
describe("HTTP Transport Binding Structured Receiver for CloudEvents v0.3", () => {
describe("HTTP Transport Binding Structured Receiver for CloudEvents v0.3",
() => {
describe("Check", () => {
it("Throw error when payload arg is null or undefined", () => {
// setup
@ -93,9 +68,10 @@ describe("HTTP Transport Binding Structured Receiver for CloudEvents v0.3", () =
.to.throw("invalid content type");
});
it("Throw error data content encoding is base64, but 'data' is not", () => {
it("Throw error data content encoding is base64, but 'data' is not",
() => {
// setup
let payload = v03.event()
const payload = v03.event()
.type(type)
.source(source)
.dataContentType("text/plain")
@ -107,7 +83,7 @@ describe("HTTP Transport Binding Structured Receiver for CloudEvents v0.3", () =
.addExtension(ext2Name, ext2Value)
.toString();
let attributes = {
const attributes = {
"Content-Type": "application/cloudevents+json"
};
@ -183,7 +159,7 @@ describe("HTTP Transport Binding Structured Receiver for CloudEvents v0.3", () =
it("Should accept 'extension1'", () => {
// setup
var extension1 = "mycuston-ext1"
var extension1 = "mycuston-ext1";
var payload = v03.event()
.type(type)
.source(source)
@ -203,7 +179,7 @@ describe("HTTP Transport Binding Structured Receiver for CloudEvents v0.3", () =
var actualExtensions = actual.getExtensions();
// assert
expect(actualExtensions["extension1"])
expect(actualExtensions.extension1)
.to.equal(extension1);
});

View File

@ -1,7 +1,6 @@
var expect = require("chai").expect;
var v1 = require("../../../v1/index.js");
var Cloudevent = require("../../../index.js");
var Spec = require("../../../lib/specs/spec_1.js");
const { asBase64 } = require("../../../lib/utils/fun.js");
@ -12,9 +11,6 @@ var receiver = new HTTPStructuredReceiver();
const type = "com.github.pull.create";
const source = "urn:event:from:myapi/resourse/123";
const webhook = "https://cloudevents.io/webhook";
const contentEncoding = "base64";
const contentType = "application/cloudevents+json; charset=utf-8";
const now = new Date();
const dataschema = "http://cloudevents.io/schema.json";
@ -24,12 +20,8 @@ const data = {
foo: "bar"
};
const ext1Name = "extension1";
const ext1Value = "foobar";
const ext2Name = "extension2";
const ext2Value = "acme";
describe("HTTP Transport Binding Structured Receiver for CloudEvents v1.0", () => {
describe("HTTP Transport Binding Structured Receiver for CloudEvents v1.0",
() => {
describe("Check", () => {
it("Throw error when payload arg is null or undefined", () => {
// setup
@ -138,7 +130,7 @@ describe("HTTP Transport Binding Structured Receiver for CloudEvents v1.0", () =
it("Should accept 'extension1'", () => {
// setup
var extension1 = "mycuston-ext1"
var extension1 = "mycuston-ext1";
var payload = v1.event()
.type(type)
.source(source)
@ -158,7 +150,7 @@ describe("HTTP Transport Binding Structured Receiver for CloudEvents v1.0", () =
var actualExtensions = actual.getExtensions();
// assert
expect(actualExtensions["extension1"])
expect(actualExtensions.extension1)
.to.equal(extension1);
});
@ -186,9 +178,10 @@ describe("HTTP Transport Binding Structured Receiver for CloudEvents v1.0", () =
it("Should maps 'data_base64' to 'data' attribute", () => {
// setup
let bindata = Uint32Array.from(JSON.stringify(data), (c) => c.codePointAt(0));
let expected = asBase64(bindata);
let payload = v1.event()
const bindata = Uint32Array
.from(JSON.stringify(data), (c) => c.codePointAt(0));
const expected = asBase64(bindata);
const payload = v1.event()
.type(type)
.source(source)
.dataContentType(ceContentType)

View File

@ -4,8 +4,6 @@ var Cloudevent = require("../../../index.js");
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";
const now = new Date();
const schemaurl = "http://cloudevents.io/schema.json";
@ -16,7 +14,6 @@ const data = {
};
describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.2", () => {
it("Throw error when payload is null", () => {
// setup
var payload = null;
@ -24,8 +21,8 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.2", () => {
// act and assert
return un.unmarshall(payload)
.then(actual => {throw {message: "failed"}})
.catch(err =>
.then(() => { throw new Error("failed"); })
.catch((err) =>
expect(err.message).to.equal("payload is null or undefined"));
});
@ -37,8 +34,8 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.2", () => {
// act and assert
return un.unmarshall(payload, headers)
.then(actual => {throw {message: "failed"}})
.catch(err =>
.then(() => { throw new Error("failed"); })
.catch((err) =>
expect(err.message).to.equal("headers is null or undefined"));
});
@ -50,8 +47,8 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.2", () => {
// act and assert
un.unmarshall(payload, headers)
.then(actual => {throw {message: "failed"}})
.catch(err =>
.then(() => { throw new Error("failed"); })
.catch((err) =>
expect(err.message).to.equal("content-type header not found"));
});
@ -65,8 +62,8 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.2", () => {
// act and assert
un.unmarshall(payload, headers)
.then(actual => {throw {message: "failed"}})
.catch(err =>
.then(() => { throw new Error("failed"); })
.catch((err) =>
expect(err.message).to.equal("content type not allowed"));
});
@ -81,8 +78,8 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.2", () => {
// act and assert
un.unmarshall(payload, headers)
.then(actual => {throw {message: "failed"}})
.catch(err =>
.then(() => { throw new Error("failed"); })
.catch((err) =>
expect(err.message).to.equal("structured+type not allowed"));
});
@ -106,8 +103,8 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.2", () => {
// act and assert
un.unmarshall(payload, headers)
.then(actual => {throw {message: "failed"}})
.catch(err =>
.then(() => { throw new Error("failed"); })
.catch((err) =>
expect(err.message).to.equal("invalid payload"));
});
@ -131,9 +128,8 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.2", () => {
// act and assert
un.unmarshall(payload, headers)
.then(actual =>
.then((actual) =>
expect(actual).to.be.an("object"));
});
});
@ -141,7 +137,7 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.2", () => {
it("Throw error when has not allowed mime", () => {
// setup
var payload = {
"data" : "dataString"
data: "dataString"
};
var attributes = {
"ce-type": "type",
@ -157,16 +153,15 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.2", () => {
// act and assert
un.unmarshall(payload, attributes)
.then(actual => {throw {message: "failed"}})
.catch(err =>
.then(() => { throw new Error("failed"); })
.catch((err) =>
expect(err.message).to.equal("content type not allowed"));
});
it("Throw error when the event does not follow the spec 0.2", () => {
// setup
var payload = {
"data" : "dataString"
data: "dataString"
};
var attributes = {
"ce-type": "type",
@ -182,15 +177,15 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.2", () => {
// act and assert
un.unmarshall(payload, attributes)
.then(actual => {throw {message: "failed"}})
.catch(err =>
.then(() => { throw new Error("failed"); })
.catch((err) =>
expect(err.message).to.not.empty);
});
it("No error when all attributes are in place", () => {
// setup
var payload = {
"data" : "dataString"
data: "dataString"
};
var attributes = {
"ce-type": "type",
@ -206,8 +201,7 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.2", () => {
// act and assert
un.unmarshall(payload, attributes)
.then(actual => expect(actual).to.be.an("object"));
.then((actual) => expect(actual).to.be.an("object"));
});
});
});

View File

@ -5,8 +5,6 @@ var v03 = require("../../../v03/index.js");
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";
const now = new Date();
const schemaurl = "http://cloudevents.io/schema.json";
const subject = "subject.ext";
@ -17,7 +15,6 @@ const data = {
};
describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
it("Throw error when payload is null", () => {
// setup
var payload = null;
@ -25,8 +22,8 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
// act and assert
return un.unmarshall(payload)
.then(actual => {throw {message: "failed"}})
.catch(err =>
.then(() => { throw new Error("failed"); })
.catch((err) =>
expect(err.message).to.equal("payload is null or undefined"));
});
@ -38,8 +35,8 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
// act and assert
return un.unmarshall(payload, headers)
.then(actual => {throw {message: "failed"}})
.catch(err =>
.then(() => { throw new Error("failed"); })
.catch((err) =>
expect(err.message).to.equal("headers is null or undefined"));
});
@ -51,8 +48,8 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
// act and assert
un.unmarshall(payload, headers)
.then(actual => {throw {message: "failed"}})
.catch(err =>
.then(() => { throw new Error("failed"); })
.catch((err) =>
expect(err.message).to.equal("content-type header not found"));
});
@ -66,8 +63,8 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
// act and assert
un.unmarshall(payload, headers)
.then(actual => {throw {message: "failed"}})
.catch(err =>
.then(() => { throw new Error("failed"); })
.catch((err) =>
expect(err.message).to.equal("content type not allowed"));
});
@ -82,8 +79,8 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
// act and assert
un.unmarshall(payload, headers)
.then(actual => {throw {message: "failed"}})
.catch(err =>
.then(() => { throw new Error("failed"); })
.catch((err) =>
expect(err.message).to.equal("structured+type not allowed"));
});
@ -107,8 +104,8 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
// act and assert
un.unmarshall(payload, headers)
.then(actual => {throw {message: "failed"}})
.catch(err =>
.then(() => { throw new Error("failed"); })
.catch((err) =>
expect(err.message).to.equal("invalid payload"));
});
@ -133,13 +130,12 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
// act and assert
return un.unmarshall(payload, headers)
.then(actual =>
.then((actual) =>
expect(actual).to.be.an("object"))
.catch((err) => {
console.log(err);
console.error(err);
throw err;
});
});
it("Should parse 'data' stringfied json to json object", () => {
@ -163,14 +159,13 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
// act and assert
return un.unmarshall(payload, headers)
.then(actual => {
expect(actual.getData()).to.deep.equal(data)
.then((actual) => {
expect(actual.getData()).to.deep.equal(data);
})
.catch((err) => {
console.log(err);
console.error(err);
throw err;
});
});
});
@ -178,7 +173,7 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
it("Throw error when has not allowed mime", () => {
// setup
var payload = {
"data" : "dataString"
data: "dataString"
};
var attributes = {
"ce-type": "type",
@ -194,16 +189,15 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
// act and assert
un.unmarshall(payload, attributes)
.then(actual => {throw {message: "failed"}})
.catch(err =>
.then(() => { throw new Error("failed"); })
.catch((err) =>
expect(err.message).to.equal("content type not allowed"));
});
it("Throw error when the event does not follow the spec 0.3", () => {
// setup
var payload = {
"data" : "dataString"
data: "dataString"
};
var attributes = {
"ce-type": "type",
@ -219,15 +213,15 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
// act and assert
un.unmarshall(payload, attributes)
.then(actual => {throw {message: "failed"}})
.catch(err =>
.then(() => { throw new Error("failed"); })
.catch((err) =>
expect(err.message).to.not.empty);
});
it("No error when all attributes are in place", () => {
// setup
var payload = {
"data" : "dataString"
data: "dataString"
};
var attributes = {
"ce-type": "type",
@ -243,7 +237,7 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
// act and assert
un.unmarshall(payload, attributes)
.then(actual => expect(actual).to.be.an("object"));
.then((actual) => expect(actual).to.be.an("object"));
});
it("Throw error when 'ce-datacontentencoding' is not allowed", () => {
@ -265,8 +259,8 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
// act and assert
return un.unmarshall(payload, attributes)
.then(actual => {throw {message: "failed"}})
.catch(err => {
.then(() => { throw new Error("failed"); })
.catch((err) => {
expect(err.message).to.equal("unsupported datacontentencoding");
});
});
@ -274,7 +268,7 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
it("No error when 'ce-datacontentencoding' is base64", () => {
// setup
var payload = "eyJtdWNoIjoid293In0=";
let expected = {
const expected = {
much: "wow"
};
@ -293,9 +287,9 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
// act and assert
return un.unmarshall(payload, attributes)
.then(actual => expect(actual.getData()).to.deep.equal(expected))
.catch(err => {
console.log(err);
.then((actual) => expect(actual.getData()).to.deep.equal(expected))
.catch((err) => {
console.error(err);
throw err;
});
});

View File

@ -7,16 +7,13 @@ const time = new Date();
const schemaurl = "http://example.com/registry/v01/myschema.json";
const contenttype = "application/json";
const data = {};
const extensions = {};
var cloudevent = new Cloudevent()
.type(type)
.source(source);
describe("CloudEvents Spec 0.1 - JavaScript SDK", () => {
describe("Object properties", () => {
describe("Attribute getters", () => {
it("returns 'type'", () => {
expect(cloudevent.getType()).to.equal(type);
@ -29,7 +26,6 @@ describe("CloudEvents Spec 0.1 - JavaScript SDK", () => {
});
describe("JSON Format", () => {
describe("Required context attributes", () => {
it("requires 'eventType'", () => {
expect(cloudevent.format()).to.have.property("eventType");
@ -84,7 +80,6 @@ describe("CloudEvents Spec 0.1 - JavaScript SDK", () => {
expect(cloudevent.format().extensions)
.to.have.property("foo");
});
});
describe("The Constraints check", () => {
@ -111,11 +106,9 @@ describe("CloudEvents Spec 0.1 - JavaScript SDK", () => {
describe("'eventTime'", () => {
it("must adhere to the format specified in RFC 3339", () => {
cloudevent.time(time);
expect(cloudevent.format()["eventTime"]).to.equal(time.toISOString());
expect(cloudevent.format().eventTime).to.equal(time.toISOString());
});
});
});
});
});

View File

@ -7,7 +7,6 @@ const time = new Date();
const schemaurl = "http://example.com/registry/myschema.json";
const contenttype = "application/json";
const data = {};
const extensions = {};
var cloudevent =
new Cloudevent(Cloudevent.specs["0.2"])
@ -15,9 +14,7 @@ var cloudevent =
.source(source);
describe("CloudEvents Spec 0.2 - JavaScript SDK", () => {
describe("Object properties", () => {
describe("Attribute getters", () => {
it("returns 'type'", () => {
expect(cloudevent.getType()).to.equal(type);
@ -30,7 +27,6 @@ describe("CloudEvents Spec 0.2 - JavaScript SDK", () => {
});
describe("JSON Format", () => {
describe("Required context attributes", () => {
it("requires 'type'", () => {
expect(cloudevent.format()).to.have.property("type");
@ -93,11 +89,10 @@ describe("CloudEvents Spec 0.2 - JavaScript SDK", () => {
it("'extension2' should have value equals to 'value1'", () => {
cloudevent.addExtension("extension2", "value2");
expect(cloudevent.format()["extension2"]).to.equal("value2");
expect(cloudevent.format().extension2).to.equal("value2");
});
it("should throw an error when employ reserved name as extension", () => {
var cevt =
new Cloudevent(Cloudevent.specs["0.2"])
.type(type)
@ -129,7 +124,7 @@ describe("CloudEvents Spec 0.2 - JavaScript SDK", () => {
describe("'specversion'", () => {
it("compliant event producers must use a value of '0.2'", () => {
expect(cloudevent.format()["specversion"]).to.equal("0.2");
expect(cloudevent.format().specversion).to.equal("0.2");
});
it("should throw an error when is an empty string", () => {
@ -165,10 +160,9 @@ describe("CloudEvents Spec 0.2 - JavaScript SDK", () => {
describe("'time'", () => {
it("must adhere to the format specified in RFC 3339", () => {
cloudevent.time(time);
expect(cloudevent.format()["time"]).to.equal(time.toISOString());
expect(cloudevent.format().time).to.equal(time.toISOString());
});
});
});
});
});

View File

@ -4,8 +4,6 @@ var Cloudevent = require("../../../index.js");
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";
const now = new Date();
const schemaurl = "http://cloudevents.io/schema.json";
@ -16,7 +14,6 @@ const data = {
};
describe("JSON Event Format Parser", () => {
it("Throw error when payload is an integer", () => {
// setup
var payload = 83;

View File

@ -32,13 +32,13 @@ describe("Functional approach", () => {
describe("isBase64", () => {
it("should return false when is not base64 string", () => {
let actual = fun.isBase64("non base 64");
const actual = fun.isBase64("non base 64");
expect(actual).to.equal(false);
});
it("should return true when is a base64 string", () => {
let actual = fun.isBase64("Y2xvdWRldmVudHMK");
const actual = fun.isBase64("Y2xvdWRldmVudHMK");
expect(actual).to.equal(true);
});
@ -46,7 +46,7 @@ describe("Functional approach", () => {
describe("asData", () => {
it("should throw error when data is not a valid json", () => {
let data = "not a json";
const data = "not a json";
expect(fun.asData.bind(fun, data, "application/json"))
.to
@ -54,37 +54,37 @@ describe("Functional approach", () => {
});
it("should parse string content type as string", () => {
let expected = "a string";
const expected = "a string";
let actual = fun.asData(expected, "text/plain");
const actual = fun.asData(expected, "text/plain");
expect((typeof actual)).to.equal("string");
expect(actual).to.equal(expected);
});
it("should parse 'application/json' as json object", () => {
let expected = {
const expected = {
much: "wow",
myext: {
ext: "x04"
}
};
let actual = fun.asData(JSON.stringify(expected), "application/json");
const actual = fun.asData(JSON.stringify(expected), "application/json");
expect((typeof actual)).to.equal("object");
expect(actual).to.deep.equal(expected);
});
it("should parse 'application/cloudevents+json' as json object", () => {
let expected = {
const expected = {
much: "wow",
myext: {
ext: "x04"
}
};
let actual = fun.asData(JSON.stringify(expected),
const actual = fun.asData(JSON.stringify(expected),
"application/cloudevents+json");
expect((typeof actual)).to.equal("object");
@ -92,14 +92,14 @@ describe("Functional approach", () => {
});
it("should parse 'text/json' as json object", () => {
let expected = {
const expected = {
much: "wow",
myext: {
ext: "x04"
}
};
let actual = fun.asData(JSON.stringify(expected),
const actual = fun.asData(JSON.stringify(expected),
"text/json");
expect((typeof actual)).to.equal("object");

View File

@ -38,7 +38,7 @@ cloudevent.eventTypeVersion("1.0.0");
var httpcfg = {
method: "POST",
url : webhook + "/json"
url: `${webhook}/json`
};
var httpstructured01 = new Structured01(httpcfg);
@ -54,105 +54,92 @@ describe("HTTP Transport Binding - Version 0.1", () => {
describe("Structured", () => {
describe("JSON Format", () => {
it("requires '" + contentType + "' Content-Type in the header", () => {
return httpstructured01.emit(cloudevent)
it(`requires '${contentType}' Content-Type in the header`,
() => httpstructured01.emit(cloudevent)
.then((response) => {
expect(response.config.headers["Content-Type"])
.to.equal(contentType);
});
});
}));
it("the request payload should be correct", () => {
return httpstructured01.emit(cloudevent)
it("the request payload should be correct",
() => httpstructured01.emit(cloudevent)
.then((response) => {
expect(JSON.parse(response.config.data))
.to.deep.equal(cloudevent.format());
});
});
}));
});
});
describe("Binary", () => {
describe("JSON Format", () => {
it("requires '" + cloudevent.getContenttype() + "' Content-Type in the header", () => {
return httpbinary01.emit(cloudevent)
it(`requires '${cloudevent.getContenttype()}' Content-Type in the header`,
() => httpbinary01.emit(cloudevent)
.then((response) => {
expect(response.config.headers["Content-Type"])
.to.equal(cloudevent.getContenttype());
});
});
}));
it("the request payload should be correct", () => {
return httpbinary01.emit(cloudevent)
it("the request payload should be correct",
() => httpbinary01.emit(cloudevent)
.then((response) => {
expect(JSON.parse(response.config.data))
.to.deep.equal(cloudevent.getData());
});
});
}));
it("HTTP Header contains 'CE-EventType'", () => {
return httpbinary01.emit(cloudevent)
it("HTTP Header contains 'CE-EventType'",
() => httpbinary01.emit(cloudevent)
.then((response) => {
expect(response.config.headers)
.to.have.property("CE-EventType");
});
});
}));
it("HTTP Header contains 'CE-EventTypeVersion'", () => {
return httpbinary01.emit(cloudevent)
it("HTTP Header contains 'CE-EventTypeVersion'",
() => httpbinary01.emit(cloudevent)
.then((response) => {
expect(response.config.headers)
.to.have.property("CE-EventTypeVersion");
});
});
}));
it("HTTP Header contains 'CE-CloudEventsVersion'", () => {
return httpbinary01.emit(cloudevent)
it("HTTP Header contains 'CE-CloudEventsVersion'",
() => httpbinary01.emit(cloudevent)
.then((response) => {
expect(response.config.headers)
.to.have.property("CE-CloudEventsVersion");
});
});
}));
it("HTTP Header contains 'CE-Source'", () => {
return httpbinary01.emit(cloudevent)
it("HTTP Header contains 'CE-Source'", () => httpbinary01.emit(cloudevent)
.then((response) => {
expect(response.config.headers)
.to.have.property("CE-Source");
});
});
}));
it("HTTP Header contains 'CE-EventID'", () => {
return httpbinary01.emit(cloudevent)
it("HTTP Header contains 'CE-EventID'",
() => httpbinary01.emit(cloudevent)
.then((response) => {
expect(response.config.headers)
.to.have.property("CE-EventID");
});
});
}));
it("HTTP Header contains 'CE-EventTime'", () => {
return httpbinary01.emit(cloudevent)
it("HTTP Header contains 'CE-EventTime'",
() => httpbinary01.emit(cloudevent)
.then((response) => {
expect(response.config.headers)
.to.have.property("CE-EventTime");
});
});
}));
it("HTTP Header contains 'CE-SchemaURL'", () => {
return httpbinary01.emit(cloudevent)
it("HTTP Header contains 'CE-SchemaURL'",
() => httpbinary01.emit(cloudevent)
.then((response) => {
expect(response.config.headers)
.to.have.property("CE-SchemaURL");
});
});
}));
it("HTTP Header contains 'CE-X-Extension1' as extension", () => {
return httpbinary01.emit(cloudevent)
it("HTTP Header contains 'CE-X-Extension1' as extension",
() => httpbinary01.emit(cloudevent)
.then((response) => {
expect(response.config.headers)
.to.have.property("CE-X-Extension1");
});
});
}));
});
});
});

View File

@ -1,9 +1,6 @@
var expect = require("chai").expect;
var Cloudevent = require("../index.js");
var nock = require("nock");
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";
@ -23,12 +20,6 @@ const ext1Value = "foobar";
const ext2Name = "extension2";
const ext2Value = "acme";
const receiverConfig = {
path : "/events",
port : 10300,
method : "POST"
};
const Structured02 = Cloudevent.bindings["http-structured0.2"];
const Binary02 = Cloudevent.bindings["http-binary0.2"];
@ -45,7 +36,7 @@ var cloudevent =
var httpcfg = {
method: "POST",
url : webhook + "/json"
url: `${webhook}/json`
};
var httpstructured02 = new Structured02(httpcfg);
@ -61,105 +52,89 @@ describe("HTTP Transport Binding - Version 0.2", () => {
describe("Structured", () => {
describe("JSON Format", () => {
it("requires '" + contentType + "' Content-Type in the header", () => {
return httpstructured02.emit(cloudevent)
it(`requires '${contentType}' Content-Type in the header`,
() => httpstructured02.emit(cloudevent)
.then((response) => {
expect(response.config.headers["Content-Type"])
.to.equal(contentType);
});
});
}));
it("the request payload should be correct", () => {
return httpstructured02.emit(cloudevent)
it("the request payload should be correct",
() => httpstructured02.emit(cloudevent)
.then((response) => {
expect(JSON.parse(response.config.data))
.to.deep.equal(cloudevent.format());
});
});
}));
});
});
describe("Binary", () => {
describe("JSON Format", () => {
it("requires '" + cloudevent.getContenttype() + "' Content-Type in the header", () => {
return httpbinary02.emit(cloudevent)
it(`requires ${cloudevent.getContenttype()} Content-Type in the header`,
() => httpbinary02.emit(cloudevent)
.then((response) => {
expect(response.config.headers["Content-Type"])
.to.equal(cloudevent.getContenttype());
});
});
}));
it("the request payload should be correct", () => {
return httpbinary02.emit(cloudevent)
it("the request payload should be correct",
() => httpbinary02.emit(cloudevent)
.then((response) => {
expect(JSON.parse(response.config.data))
.to.deep.equal(cloudevent.getData());
});
});
}));
it("HTTP Header contains 'ce-type'", () => {
return httpbinary02.emit(cloudevent)
it("HTTP Header contains 'ce-type'", () => httpbinary02.emit(cloudevent)
.then((response) => {
expect(response.config.headers)
.to.have.property("ce-type");
});
});
}));
it("HTTP Header contains 'ce-specversion'", () => {
return httpbinary02.emit(cloudevent)
it("HTTP Header contains 'ce-specversion'",
() => httpbinary02.emit(cloudevent)
.then((response) => {
expect(response.config.headers)
.to.have.property("ce-specversion");
});
});
}));
it("HTTP Header contains 'ce-source'", () => {
return httpbinary02.emit(cloudevent)
it("HTTP Header contains 'ce-source'", () => httpbinary02.emit(cloudevent)
.then((response) => {
expect(response.config.headers)
.to.have.property("ce-source");
});
});
}));
it("HTTP Header contains 'ce-id'", () => {
return httpbinary02.emit(cloudevent)
it("HTTP Header contains 'ce-id'", () => httpbinary02.emit(cloudevent)
.then((response) => {
expect(response.config.headers)
.to.have.property("ce-id");
});
});
}));
it("HTTP Header contains 'ce-time'", () => {
return httpbinary02.emit(cloudevent)
it("HTTP Header contains 'ce-time'", () => httpbinary02.emit(cloudevent)
.then((response) => {
expect(response.config.headers)
.to.have.property("ce-time");
});
});
}));
it("HTTP Header contains 'ce-schemaurl'", () => {
return httpbinary02.emit(cloudevent)
it("HTTP Header contains 'ce-schemaurl'",
() => httpbinary02.emit(cloudevent)
.then((response) => {
expect(response.config.headers)
.to.have.property("ce-schemaurl");
});
});
}));
it("HTTP Header contains 'ce-" + ext1Name + "'", () => {
return httpbinary02.emit(cloudevent)
it(`HTTP Header contains 'ce-${ext1Name}'`,
() => httpbinary02.emit(cloudevent)
.then((response) => {
expect(response.config.headers)
.to.have.property("ce-" + ext1Name);
});
});
.to.have.property(`ce-${ext1Name}`);
}));
it("HTTP Header contains 'ce-" + ext2Name + "'", () => {
return httpbinary02.emit(cloudevent)
it(`HTTP Header contains 'ce-${ext2Name}'`,
() => httpbinary02.emit(cloudevent)
.then((response) => {
expect(response.config.headers)
.to.have.property("ce-" + ext2Name);
});
});
.to.have.property(`ce-${ext2Name}`);
}));
});
});
});

View File

@ -48,11 +48,10 @@ const cebase64 =
.addExtension(ext1Name, ext1Value)
.addExtension(ext2Name, ext2Value);
const webhook = "https://cloudevents.io/webhook";
const httpcfg = {
method: "POST",
url : webhook + "/json"
url: `${webhook}/json`
};
const binary = new BinaryHTTPEmitter(httpcfg);
@ -68,214 +67,175 @@ describe("HTTP Transport Binding - Version 0.3", () => {
describe("Structured", () => {
describe("JSON Format", () => {
it("requires '" + contentType + "' Content-Type in the header", () => {
return structured.emit(cloudevent)
it(`requires '${contentType}' Content-Type in the header`,
() => structured.emit(cloudevent)
.then((response) => {
expect(response.config.headers["Content-Type"])
.to.equal(contentType);
});
});
}));
it("the request payload should be correct", () => {
return structured.emit(cloudevent)
it("the request payload should be correct",
() => structured.emit(cloudevent)
.then((response) => {
expect(JSON.parse(response.config.data))
.to.deep.equal(cloudevent.format());
});
});
}));
describe("'data' attribute with 'base64' encoding", () => {
it("the request payload should be correct", () => {
return structured.emit(cebase64)
it("the request payload should be correct",
() => structured.emit(cebase64)
.then((response) => {
expect(JSON.parse(response.config.data).data)
.to.equal(cebase64.format().data);
});
});
}));
});
});
});
describe("Binary", () => {
describe("JSON Format", () => {
it("requires '" + cloudevent.getDataContentType() + "' Content-Type in the header", () => {
return binary.emit(cloudevent)
it(`requires ${cloudevent.getDataContentType()} in the header`,
() => binary.emit(cloudevent)
.then((response) => {
expect(response.config.headers["Content-Type"])
.to.equal(cloudevent.getDataContentType());
});
});
}));
it("the request payload should be correct", () => {
return binary.emit(cloudevent)
it("the request payload should be correct", () => binary.emit(cloudevent)
.then((response) => {
expect(JSON.parse(response.config.data))
.to.deep.equal(cloudevent.getData());
});
});
}));
it("HTTP Header contains 'ce-type'", () => {
return binary.emit(cloudevent)
it("HTTP Header contains 'ce-type'", () => binary.emit(cloudevent)
.then((response) => {
expect(response.config.headers)
.to.have.property("ce-type");
});
});
}));
it("HTTP Header contains 'ce-specversion'", () => {
return binary.emit(cloudevent)
it("HTTP Header contains 'ce-specversion'", () => binary.emit(cloudevent)
.then((response) => {
expect(response.config.headers)
.to.have.property("ce-specversion");
});
});
}));
it("HTTP Header contains 'ce-source'", () => {
return binary.emit(cloudevent)
it("HTTP Header contains 'ce-source'", () => binary.emit(cloudevent)
.then((response) => {
expect(response.config.headers)
.to.have.property("ce-source");
});
});
}));
it("HTTP Header contains 'ce-id'", () => {
return binary.emit(cloudevent)
it("HTTP Header contains 'ce-id'", () => binary.emit(cloudevent)
.then((response) => {
expect(response.config.headers)
.to.have.property("ce-id");
});
});
}));
it("HTTP Header contains 'ce-time'", () => {
return binary.emit(cloudevent)
it("HTTP Header contains 'ce-time'", () => binary.emit(cloudevent)
.then((response) => {
expect(response.config.headers)
.to.have.property("ce-time");
});
});
}));
it("HTTP Header contains 'ce-schemaurl'", () => {
return binary.emit(cloudevent)
it("HTTP Header contains 'ce-schemaurl'", () => binary.emit(cloudevent)
.then((response) => {
expect(response.config.headers)
.to.have.property("ce-schemaurl");
});
});
}));
it("HTTP Header contains 'ce-" + ext1Name + "'", () => {
return binary.emit(cloudevent)
it(`HTTP Header contains 'ce-${ext1Name}'`, () => binary.emit(cloudevent)
.then((response) => {
expect(response.config.headers)
.to.have.property("ce-" + ext1Name);
});
});
.to.have.property(`ce-${ext1Name}`);
}));
it("HTTP Header contains 'ce-" + ext2Name + "'", () => {
return binary.emit(cloudevent)
it(`HTTP Header contains 'ce-${ext2Name}'`, () => binary.emit(cloudevent)
.then((response) => {
expect(response.config.headers)
.to.have.property("ce-" + ext2Name);
});
});
.to.have.property(`ce-${ext2Name}`);
}));
it("HTTP Header contains 'ce-subject'", () => {
return binary.emit(cloudevent)
it("HTTP Header contains 'ce-subject'", () => binary.emit(cloudevent)
.then((response) => {
expect(response.config.headers)
.to.have.property("ce-subject");
});
});
}));
it("should 'ce-type' have the right value", () => {
return binary.emit(cloudevent)
it("should 'ce-type' have the right value", () => binary.emit(cloudevent)
.then((response) => {
expect(cloudevent.getType())
.to.equal(response.config.headers["ce-type"]);
});
});
}));
it("should 'ce-specversion' have the right value", () => {
return binary.emit(cloudevent)
it("should 'ce-specversion' have the right value",
() => binary.emit(cloudevent)
.then((response) => {
expect(cloudevent.getSpecversion())
.to.equal(response.config.headers["ce-specversion"]);
});
});
}));
it("should 'ce-source' have the right value", () => {
return binary.emit(cloudevent)
it("should 'ce-source' have the right value",
() => binary.emit(cloudevent)
.then((response) => {
expect(cloudevent.getSource())
.to.equal(response.config.headers["ce-source"]);
});
});
}));
it("should 'ce-id' have the right value", () => {
return binary.emit(cloudevent)
it("should 'ce-id' have the right value", () => binary.emit(cloudevent)
.then((response) => {
expect(cloudevent.getId())
.to.equal(response.config.headers["ce-id"]);
});
});
}));
it("should 'ce-time' have the right value", () => {
return binary.emit(cloudevent)
it("should 'ce-time' have the right value", () => binary.emit(cloudevent)
.then((response) => {
expect(cloudevent.getTime())
.to.equal(response.config.headers["ce-time"]);
});
});
}));
it("should 'ce-schemaurl' have the right value", () => {
return binary.emit(cloudevent)
it("should 'ce-schemaurl' have the right value",
() => binary.emit(cloudevent)
.then((response) => {
expect(cloudevent.getSchemaurl())
.to.equal(response.config.headers["ce-schemaurl"]);
});
});
}));
it("should 'ce-" + ext1Name + "' have the right value", () => {
return binary.emit(cloudevent)
it(`should 'ce-${ext1Name}' have the right value`,
() => binary.emit(cloudevent)
.then((response) => {
expect(cloudevent.getExtensions()[ext1Name])
.to.equal(response.config.headers["ce-" + ext1Name]);
});
});
.to.equal(response.config.headers[`ce-${ext1Name}`]);
}));
it("should 'ce-" + ext2Name + "' have the right value", () => {
return binary.emit(cloudevent)
it(`should 'ce-${ext2Name}' have the right value`,
() => binary.emit(cloudevent)
.then((response) => {
expect(cloudevent.getExtensions()[ext2Name])
.to.equal(response.config.headers["ce-" + ext2Name]);
});
});
.to.equal(response.config.headers[`ce-${ext2Name}`]);
}));
it("should 'ce-subject' have the right value", () => {
return binary.emit(cloudevent)
it("should 'ce-subject' have the right value",
() => binary.emit(cloudevent)
.then((response) => {
expect(cloudevent.getSubject())
.to.equal(response.config.headers["ce-subject"]);
});
});
}));
describe("'data' attribute with 'base64' encoding", () => {
it("HTTP Header contains 'ce-datacontentencoding'", () => {
return binary.emit(cebase64)
it("HTTP Header contains 'ce-datacontentencoding'",
() => binary.emit(cebase64)
.then((response) => {
expect(response.config.headers)
.to.have.property("ce-datacontentencoding");
});
});
}));
it("should 'ce-datacontentencoding' have the right value", () => {
return binary.emit(cloudevent)
it("should 'ce-datacontentencoding' have the right value",
() => binary.emit(cloudevent)
.then((response) => {
expect(cloudevent.getDataContentEncoding())
.to.equal(response.config.headers["ce-datacontentencoding"]);
}));
});
});
});
});
});
});

View File

@ -39,12 +39,12 @@ const cloudevent =
.addExtension(ext1Name, ext1Value)
.addExtension(ext2Name, ext2Value);
const dataString = ")(*~^my data for ce#@#$%"
const dataString = ")(*~^my data for ce#@#$%";
const webhook = "https://cloudevents.io/webhook/v1";
const httpcfg = {
method: "POST",
url : webhook + "/json"
url: `${webhook}/json`
};
const binary = new BinaryHTTPEmitter(httpcfg);
@ -59,43 +59,41 @@ describe("HTTP Transport Binding - Version 1.0", () => {
});
describe("Structured", () => {
it('works with mTLS authentication', () => {
it("works with mTLS authentication", () => {
const event = new StructuredHTTPEmitter({
method: 'POST',
method: "POST",
url: `${webhook}/json`,
httpsAgent: new https.Agent({
cert: 'some value',
key: 'other value'
cert: "some value",
key: "other value"
})
})
return event.emit(cloudevent).then(response => {
expect(response.config.headers['Content-Type'])
.to.equal(contentType);
});
});
describe("JSON Format", () => {
it("requires '" + contentType + "' Content-Type in the header", () => {
return structured.emit(cloudevent)
.then((response) => {
return event.emit(cloudevent).then((response) => {
expect(response.config.headers["Content-Type"])
.to.equal(contentType);
});
});
it("the request payload should be correct", () => {
return structured.emit(cloudevent)
describe("JSON Format", () => {
it(`requires '${contentType}' Content-Type in the header`,
() => structured.emit(cloudevent)
.then((response) => {
expect(response.config.headers["Content-Type"])
.to.equal(contentType);
}));
it("the request payload should be correct",
() => structured.emit(cloudevent)
.then((response) => {
expect(JSON.parse(response.config.data))
.to.deep.equal(cloudevent.format());
});
});
}));
describe("Binary event data", () => {
it("the request payload should be correct when data is binary", () => {
let bindata = Uint32Array.from(dataString, (c) => c.codePointAt(0));
let expected = asBase64(bindata);
let binevent =
const bindata = Uint32Array.from(dataString, (c) => c.codePointAt(0));
const expected = asBase64(bindata);
const binevent =
new Cloudevent(Spec)
.type(type)
.source(source)
@ -112,7 +110,7 @@ describe("HTTP Transport Binding - Version 1.0", () => {
});
it("the payload must have 'data_base64' when data is binary", () => {
let binevent =
const binevent =
new Cloudevent(Spec)
.type(type)
.source(source)
@ -132,42 +130,40 @@ describe("HTTP Transport Binding - Version 1.0", () => {
});
describe("Binary", () => {
it('works with mTLS authentication', () => {
it("works with mTLS authentication", () => {
const event = new BinaryHTTPEmitter({
method: 'POST',
method: "POST",
url: `${webhook}/json`,
httpsAgent: new https.Agent({
cert: 'some value',
key: 'other value'
cert: "some value",
key: "other value"
})
})
return event.emit(cloudevent).then(response => {
expect(response.config.headers['Content-Type'])
.to.equal(cloudevent.getDataContentType());
});
});
describe("JSON Format", () => {
it("requires '" + cloudevent.getDataContentType() + "' Content-Type in the header", () => {
return binary.emit(cloudevent)
.then((response) => {
return event.emit(cloudevent).then((response) => {
expect(response.config.headers["Content-Type"])
.to.equal(cloudevent.getDataContentType());
});
});
it("the request payload should be correct", () => {
return binary.emit(cloudevent)
describe("JSON Format", () => {
it(`requires '${cloudevent.getDataContentType()}' in the header`,
() => binary.emit(cloudevent)
.then((response) => {
expect(response.config.headers["Content-Type"])
.to.equal(cloudevent.getDataContentType());
}));
it("the request payload should be correct", () => binary.emit(cloudevent)
.then((response) => {
expect(JSON.parse(response.config.data))
.to.deep.equal(cloudevent.getData());
});
});
}));
it("the request payload should be correct when event data is binary", () => {
let bindata = Uint32Array.from(dataString, (c) => c.codePointAt(0));
let expected = asBase64(bindata);
let binevent =
it("the request payload should be correct when event data is binary",
() => {
const bindata = Uint32Array.from(dataString, (c) => c.codePointAt(0));
const expected = asBase64(bindata);
const binevent =
new Cloudevent(Spec)
.type(type)
.source(source)
@ -183,149 +179,119 @@ describe("HTTP Transport Binding - Version 1.0", () => {
});
});
it("HTTP Header contains 'ce-type'", () => {
return binary.emit(cloudevent)
it("HTTP Header contains 'ce-type'", () => binary.emit(cloudevent)
.then((response) => {
expect(response.config.headers)
.to.have.property("ce-type");
});
});
}));
it("HTTP Header contains 'ce-specversion'", () => {
return binary.emit(cloudevent)
it("HTTP Header contains 'ce-specversion'", () => binary.emit(cloudevent)
.then((response) => {
expect(response.config.headers)
.to.have.property("ce-specversion");
});
});
}));
it("HTTP Header contains 'ce-source'", () => {
return binary.emit(cloudevent)
it("HTTP Header contains 'ce-source'", () => binary.emit(cloudevent)
.then((response) => {
expect(response.config.headers)
.to.have.property("ce-source");
});
});
}));
it("HTTP Header contains 'ce-id'", () => {
return binary.emit(cloudevent)
it("HTTP Header contains 'ce-id'", () => binary.emit(cloudevent)
.then((response) => {
expect(response.config.headers)
.to.have.property("ce-id");
});
});
}));
it("HTTP Header contains 'ce-time'", () => {
return binary.emit(cloudevent)
it("HTTP Header contains 'ce-time'", () => binary.emit(cloudevent)
.then((response) => {
expect(response.config.headers)
.to.have.property("ce-time");
});
});
}));
it("HTTP Header contains 'ce-dataschema'", () => {
return binary.emit(cloudevent)
it("HTTP Header contains 'ce-dataschema'", () => binary.emit(cloudevent)
.then((response) => {
expect(response.config.headers)
.to.have.property("ce-dataschema");
});
});
}));
it("HTTP Header contains 'ce-" + ext1Name + "'", () => {
return binary.emit(cloudevent)
it(`HTTP Header contains 'ce-${ext1Name}'`, () => binary.emit(cloudevent)
.then((response) => {
expect(response.config.headers)
.to.have.property("ce-" + ext1Name);
});
});
.to.have.property(`ce-${ext1Name}`);
}));
it("HTTP Header contains 'ce-" + ext2Name + "'", () => {
return binary.emit(cloudevent)
it(`HTTP Header contains 'ce-${ext2Name}'`, () => binary.emit(cloudevent)
.then((response) => {
expect(response.config.headers)
.to.have.property("ce-" + ext2Name);
});
});
.to.have.property(`ce-${ext2Name}`);
}));
it("HTTP Header contains 'ce-subject'", () => {
return binary.emit(cloudevent)
it("HTTP Header contains 'ce-subject'", () => binary.emit(cloudevent)
.then((response) => {
expect(response.config.headers)
.to.have.property("ce-subject");
});
});
}));
it("should 'ce-type' have the right value", () => {
return binary.emit(cloudevent)
it("should 'ce-type' have the right value", () => binary.emit(cloudevent)
.then((response) => {
expect(cloudevent.getType())
.to.equal(response.config.headers["ce-type"]);
});
});
}));
it("should 'ce-specversion' have the right value", () => {
return binary.emit(cloudevent)
it("should 'ce-specversion' have the right value",
() => binary.emit(cloudevent)
.then((response) => {
expect(cloudevent.getSpecversion())
.to.equal(response.config.headers["ce-specversion"]);
});
});
}));
it("should 'ce-source' have the right value", () => {
return binary.emit(cloudevent)
it("should 'ce-source' have the right value",
() => binary.emit(cloudevent)
.then((response) => {
expect(cloudevent.getSource())
.to.equal(response.config.headers["ce-source"]);
});
});
}));
it("should 'ce-id' have the right value", () => {
return binary.emit(cloudevent)
it("should 'ce-id' have the right value", () => binary.emit(cloudevent)
.then((response) => {
expect(cloudevent.getId())
.to.equal(response.config.headers["ce-id"]);
});
});
}));
it("should 'ce-time' have the right value", () => {
return binary.emit(cloudevent)
it("should 'ce-time' have the right value", () => binary.emit(cloudevent)
.then((response) => {
expect(cloudevent.getTime())
.to.equal(response.config.headers["ce-time"]);
});
});
}));
it("should 'ce-dataschema' have the right value", () => {
return binary.emit(cloudevent)
it("should 'ce-dataschema' have the right value",
() => binary.emit(cloudevent)
.then((response) => {
expect(cloudevent.getDataschema())
.to.equal(response.config.headers["ce-dataschema"]);
});
});
}));
it("should 'ce-" + ext1Name + "' have the right value", () => {
return binary.emit(cloudevent)
it(`should 'ce-${ext1Name}' have the right value`,
() => binary.emit(cloudevent)
.then((response) => {
expect(cloudevent.getExtensions()[ext1Name])
.to.equal(response.config.headers["ce-" + ext1Name]);
});
});
.to.equal(response.config.headers[`ce-${ext1Name}`]);
}));
it("should 'ce-" + ext2Name + "' have the right value", () => {
return binary.emit(cloudevent)
it(`should 'ce-${ext2Name}' have the right value`,
() => binary.emit(cloudevent)
.then((response) => {
expect(cloudevent.getExtensions()[ext2Name])
.to.equal(response.config.headers["ce-" + ext2Name]);
});
});
.to.equal(response.config.headers[`ce-${ext2Name}`]);
}));
it("should 'ce-subject' have the right value", () => {
return binary.emit(cloudevent)
it("should 'ce-subject' have the right value",
() => binary.emit(cloudevent)
.then((response) => {
expect(cloudevent.getSubject())
.to.equal(response.config.headers["ce-subject"]);
});
});
}));
});
});
});

View File

@ -13,7 +13,6 @@ const dataContentType = "application/json";
const data = {
much: "wow"
};
const extensions = {};
const subject = "subject-x0";
var cloudevent =
@ -28,7 +27,6 @@ var cloudevent =
.data(data);
describe("CloudEvents Spec v0.3", () => {
describe("REQUIRED Attributes", () => {
it("Should have 'id'", () => {
expect(cloudevent.getId()).to.equal(id);
@ -83,7 +81,7 @@ describe("CloudEvents Spec v0.3", () => {
it("Should have the 'extension1'", () => {
cloudevent.addExtension("extension1", "value1");
expect(cloudevent.spec.payload["extension1"])
expect(cloudevent.spec.payload.extension1)
.to.equal("value1");
});
@ -177,7 +175,6 @@ describe("CloudEvents Spec v0.3", () => {
it("should throw an error when 'data' does not carry base64",
() => {
cloudevent
.data("no base 64 value")
.dataContentEncoding("base64")
@ -232,9 +229,8 @@ describe("CloudEvents Spec v0.3", () => {
describe("'time'", () => {
it("must adhere to the format specified in RFC 3339", () => {
cloudevent.time(time);
expect(cloudevent.format()["time"]).to.equal(time.toISOString());
expect(cloudevent.format().time).to.equal(time.toISOString());
});
});
});
});

View File

@ -13,7 +13,6 @@ const dataContentType = "application/json";
const data = {
much: "wow"
};
const extensions = {};
const subject = "subject-x0";
const cloudevent =
@ -78,13 +77,13 @@ describe("CloudEvents Spec v1.0", () => {
});
it("should be ok when type is 'string'", () => {
cloudevent.addExtension("ext-string", 'an-string');
cloudevent.addExtension("ext-string", "an-string");
expect(cloudevent.spec.payload["ext-string"])
.to.equal("an-string");
});
it("should be ok when type is 'Uint32Array' for 'Binary'", () => {
let myBinary = new Uint32Array(2019);
const myBinary = new Uint32Array(2019);
cloudevent.addExtension("ext-binary", myBinary);
expect(cloudevent.spec.payload["ext-binary"])
.to.equal(myBinary);
@ -93,7 +92,7 @@ describe("CloudEvents Spec v1.0", () => {
// URI
it("should be ok when type is 'Date' for 'Timestamp'", () => {
let myDate = new Date();
const myDate = new Date();
cloudevent.addExtension("ext-date", myDate);
expect(cloudevent.spec.payload["ext-date"])
.to.equal(myDate);
@ -101,7 +100,7 @@ describe("CloudEvents Spec v1.0", () => {
it("Should have the 'extension1'", () => {
cloudevent.addExtension("extension1", "value1");
expect(cloudevent.spec.payload["extension1"])
expect(cloudevent.spec.payload.extension1)
.to.equal("value1");
});
@ -111,7 +110,8 @@ describe("CloudEvents Spec v1.0", () => {
});
it("should throw an error when use an invalid type", () => {
expect(cloudevent.addExtension.bind(cloudevent, "invalid-val", {cool:'nice'}))
expect(cloudevent
.addExtension.bind(cloudevent, "invalid-val", { cool: "nice" }))
.to.throw("Invalid type of extension value");
});
});
@ -199,7 +199,7 @@ describe("CloudEvents Spec v1.0", () => {
describe("'time'", () => {
it("must adhere to the format specified in RFC 3339", () => {
cloudevent.time(time);
expect(cloudevent.format()["time"]).to.equal(time.toISOString());
expect(cloudevent.format().time).to.equal(time.toISOString());
});
});
});
@ -226,11 +226,11 @@ describe("CloudEvents Spec v1.0", () => {
});
it("should be ok when type is 'Uint32Array' for 'Binary'", () => {
let dataString = ")(*~^my data for ce#@#$%"
const dataString = ")(*~^my data for ce#@#$%";
let dataBinary = Uint32Array.from(dataString, (c) => c.codePointAt(0));
let expected = asBase64(dataBinary);
let olddct = cloudevent.getDataContentType();
const dataBinary = Uint32Array.from(dataString, (c) => c.codePointAt(0));
const expected = asBase64(dataBinary);
const olddct = cloudevent.getDataContentType();
cloudevent
.dataContentType("text/plain")