diff --git a/ext/spec_0_3.json b/ext/spec_0_3.json deleted file mode 100644 index 2c362698..00000000 --- a/ext/spec_0_3.json +++ /dev/null @@ -1,88 +0,0 @@ -{ - "$ref": "#/definitions/event", - "definitions": { - "specversion": { - "type": "string", - "minLength": 1, - "const": "0.3" - }, - "datacontenttype": { - "type": "string" - }, - "data": { - "type": [ - "object", - "string" - ] - }, - "event": { - "properties": { - "specversion": { - "$ref": "#/definitions/specversion" - }, - "datacontenttype": { - "$ref": "#/definitions/datacontenttype" - }, - "data": { - "$ref": "#/definitions/data" - }, - "id": { - "$ref": "#/definitions/id" - }, - "time": { - "$ref": "#/definitions/time" - }, - "schemaurl": { - "$ref": "#/definitions/schemaurl" - }, - "subject": { - "$ref": "#/definitions/subject" - }, - "type": { - "$ref": "#/definitions/type" - }, - "extensions": { - "$ref": "#/definitions/extensions" - }, - "source": { - "$ref": "#/definitions/source" - } - }, - "required": [ - "specversion", - "id", - "type", - "source" - ], - "type": "object" - }, - "id": { - "type": "string", - "minLength": 1 - }, - "time": { - "format": "date-time", - "type": "string" - }, - "schemaurl": { - "type": "string", - "format": "uri-reference" - }, - "subject": { - "type": "string", - "minLength": 1 - }, - "type": { - "type": "string", - "minLength": 1 - }, - "extensions": { - "type": "object" - }, - "source": { - "format": "uri-reference", - "type": "string" - } - }, - "type": "object" -} diff --git a/lib/bindings/http/emitter_binary_0_3.js b/lib/bindings/http/emitter_binary_0_3.js deleted file mode 100644 index 5aa6c022..00000000 --- a/lib/bindings/http/emitter_binary_0_3.js +++ /dev/null @@ -1,64 +0,0 @@ -const BinaryHTTPEmitter = require("./emitter_binary.js"); - -const Constants = require("./constants.js"); - -const headerByGetter = {}; - -headerByGetter.getDataContentType = { - name: Constants.HEADER_CONTENT_TYPE, - parser: (v) => v -}; - -headerByGetter.getDataContentEncoding = { - name: Constants.BINARY_HEADERS_03.CONTENT_ENCONDING, - parser: (v) => v -}; - -headerByGetter.getSubject = { - name: Constants.BINARY_HEADERS_03.SUBJECT, - parser: (v) => v -}; - -headerByGetter.getType = { - name: Constants.BINARY_HEADERS_03.TYPE, - parser: (v) => v -}; - -headerByGetter.getSpecversion = { - name: Constants.BINARY_HEADERS_03.SPEC_VERSION, - parser: (v) => v -}; - -headerByGetter.getSource = { - name: Constants.BINARY_HEADERS_03.SOURCE, - parser: (v) => v -}; - -headerByGetter.getId = { - name: Constants.BINARY_HEADERS_03.ID, - parser: (v) => v -}; - -headerByGetter.getTime = { - name: Constants.BINARY_HEADERS_03.TIME, - parser: (v) => v -}; - -headerByGetter.getSchemaurl = { - name: Constants.BINARY_HEADERS_03.SCHEMA_URL, - parser: (v) => v -}; - -function HTTPBinary(configuration) { - this.emitter = new BinaryHTTPEmitter( - configuration, - headerByGetter, - Constants.BINARY_HEADERS_03.EXTENSIONS_PREFIX - ); -} - -HTTPBinary.prototype.emit = function(cloudevent) { - return this.emitter.emit(cloudevent); -}; - -module.exports = HTTPBinary; diff --git a/lib/bindings/http/receiver_binary_0_3.js b/lib/bindings/http/receiver_binary_0_3.js deleted file mode 100644 index 3f763298..00000000 --- a/lib/bindings/http/receiver_binary_0_3.js +++ /dev/null @@ -1,120 +0,0 @@ -const Constants = require("./constants.js"); -const Spec = require("../../specs/spec_0_3.js"); - -const JSONParser = require("../../formats/json/parser.js"); -const Base64Parser = require("../../formats/base64.js"); - -const BinaryHTTPReceiver = require("./receiver_binary.js"); - -const parserByType = {}; -parserByType[Constants.MIME_JSON] = new JSONParser(); -parserByType[Constants.MIME_OCTET_STREAM] = { - parse(payload) { return payload; } -}; - -const parsersByEncoding = {}; -parsersByEncoding.null = parserByType; -parsersByEncoding[undefined] = parserByType; - -// base64 -parsersByEncoding[Constants.ENCODING_BASE64] = {}; -parsersByEncoding[Constants.ENCODING_BASE64][Constants.MIME_JSON] = - new JSONParser(new Base64Parser()); -parsersByEncoding[Constants.ENCODING_BASE64][Constants.MIME_OCTET_STREAM] = { - parse(payload) { return payload; } -}; - -const allowedContentTypes = []; -allowedContentTypes.push(Constants.MIME_JSON); -allowedContentTypes.push(Constants.MIME_OCTET_STREAM); - -const allowedEncodings = []; -allowedEncodings.push(Constants.ENCODING_BASE64); - -const requiredHeaders = []; -requiredHeaders.push(Constants.BINARY_HEADERS_03.TYPE); -requiredHeaders.push(Constants.BINARY_HEADERS_03.SPEC_VERSION); -requiredHeaders.push(Constants.BINARY_HEADERS_03.SOURCE); -requiredHeaders.push(Constants.BINARY_HEADERS_03.ID); - -const setterByHeader = {}; -setterByHeader[Constants.BINARY_HEADERS_03.TYPE] = { - name: "type", - parser: (v) => v -}; -setterByHeader[Constants.BINARY_HEADERS_03.SPEC_VERSION] = { - name: "specversion", - parser: () => "0.3" -}; -setterByHeader[Constants.BINARY_HEADERS_03.SOURCE] = { - name: "source", - parser: (v) => v -}; -setterByHeader[Constants.BINARY_HEADERS_03.ID] = { - name: "id", - parser: (v) => v -}; -setterByHeader[Constants.BINARY_HEADERS_03.TIME] = { - name: "time", - parser: (v) => new Date(Date.parse(v)) -}; -setterByHeader[Constants.BINARY_HEADERS_03.SCHEMA_URL] = { - name: "schemaurl", - parser: (v) => v -}; -setterByHeader[Constants.HEADER_CONTENT_TYPE] = { - name: "dataContentType", - parser: (v) => v -}; -setterByHeader[Constants.BINARY_HEADERS_03.CONTENT_ENCONDING] = { - name: "dataContentEncoding", - parser: (v) => v -}; -setterByHeader[Constants.BINARY_HEADERS_03.SUBJECT] = { - name: "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")) - .filter((header) => - header === Constants.BINARY_HEADERS_03.CONTENT_ENCONDING) - .filter((header) => !allowedEncodings.includes(headers[header])) - .forEach((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, - setterByHeader, - allowedContentTypes, - requiredHeaders, - Spec, - Constants.SPEC_V03, - Constants.BINARY_HEADERS_03.EXTENSIONS_PREFIX, - checkDecorator - ); -} - -Receiver.prototype.check = function(payload, headers) { - this.receiver.check(payload, headers); -}; - -Receiver.prototype.parse = function(payload, headers) { - // firstly specific local checks - this.check(payload, headers); - - return this.receiver.parse(payload, headers); -}; - -module.exports = Receiver; diff --git a/lib/bindings/http/receiver_structured_0_3.js b/lib/bindings/http/receiver_structured_0_3.js deleted file mode 100644 index 35cbfc40..00000000 --- a/lib/bindings/http/receiver_structured_0_3.js +++ /dev/null @@ -1,77 +0,0 @@ -const Constants = require("./constants.js"); -const Spec = require("../../specs/spec_0_3.js"); -const JSONParser = require("../../formats/json/parser.js"); - -const StructuredHTTPReceiver = require("./receiver_structured.js"); - -const jsonParserSpec = new JSONParser(); - -const parserByMime = {}; -parserByMime[Constants.MIME_JSON] = jsonParserSpec; -parserByMime[Constants.MIME_CE_JSON] = jsonParserSpec; - -const allowedContentTypes = []; -allowedContentTypes.push(Constants.MIME_CE_JSON); - -const setterByAttribute = {}; -setterByAttribute[Constants.STRUCTURED_ATTRS_03.TYPE] = { - name: "type", - parser: (v) => v -}; -setterByAttribute[Constants.STRUCTURED_ATTRS_03.SPEC_VERSION] = { - name: "specversion", - parser: (v) => v -}; -setterByAttribute[Constants.STRUCTURED_ATTRS_03.SOURCE] = { - name: "source", - parser: (v) => v -}; -setterByAttribute[Constants.STRUCTURED_ATTRS_03.ID] = { - name: "id", - parser: (v) => v -}; -setterByAttribute[Constants.STRUCTURED_ATTRS_03.TIME] = { - name: "time", - parser: (v) => new Date(Date.parse(v)) -}; -setterByAttribute[Constants.STRUCTURED_ATTRS_03.SCHEMA_URL] = { - name: "schemaurl", - parser: (v) => v -}; -setterByAttribute[Constants.STRUCTURED_ATTRS_03.CONTENT_ENCONDING] = { - name: "dataContentEncoding", - parser: (v) => v -}; -setterByAttribute[Constants.STRUCTURED_ATTRS_03.CONTENT_TYPE] = { - name: "dataContentType", - parser: (v) => v -}; -setterByAttribute[Constants.STRUCTURED_ATTRS_03.SUBJECT] = { - name: "subject", - parser: (v) => v -}; -setterByAttribute[Constants.STRUCTURED_ATTRS_03.DATA] = { - name: "data", - parser: (v) => v -}; - -// Leaving this in place for now. TODO: fixme -// eslint-disable-next-line -function Receiver(configuration) { - this.receiver = new StructuredHTTPReceiver( - parserByMime, - setterByAttribute, - allowedContentTypes, - Spec - ); -} - -Receiver.prototype.check = function(payload, headers) { - this.receiver.check(payload, headers); -}; - -Receiver.prototype.parse = function(payload, headers) { - return this.receiver.parse(payload, headers); -}; - -module.exports = Receiver; diff --git a/lib/bindings/http/unmarshaller_0_3.js b/lib/bindings/http/unmarshaller_0_3.js deleted file mode 100644 index a6bf300c..00000000 --- a/lib/bindings/http/unmarshaller_0_3.js +++ /dev/null @@ -1,19 +0,0 @@ -const GenericUnmarshaller = require("./unmarshaller.js"); - -const StructuredReceiver = require("./receiver_structured_0_3.js"); -const BinaryReceiver = require("./receiver_binary_0_3.js"); - -const RECEIVER_BY_BINDING = { - structured: new StructuredReceiver(), - binary: new BinaryReceiver() -}; - -const Unmarshaller = function() { - this.unmarshaller = new GenericUnmarshaller(RECEIVER_BY_BINDING); -}; - -Unmarshaller.prototype.unmarshall = function(payload, headers) { - return this.unmarshaller.unmarshall(payload, headers); -}; - -module.exports = Unmarshaller; diff --git a/lib/specs/spec_0_3.js b/lib/specs/spec_0_3.js deleted file mode 100644 index 4e5c6cd2..00000000 --- a/lib/specs/spec_0_3.js +++ /dev/null @@ -1,227 +0,0 @@ -const { v4: uuidv4 } = require("uuid"); -const Ajv = require("ajv"); - -const { - isBase64, - clone, - asData -} = require("../utils/fun.js"); - -const RESERVED_ATTRIBUTES = { - type: "type", - specversion: "specversion", - source: "source", - id: "id", - time: "time", - schemaurl: "schemaurl", - datacontentencoding: "datacontentencoding", - datacontenttype: "datacontenttype", - subject: "subject", - data: "data" -}; - -const SUPPORTED_CONTENT_ENCODING = {}; -SUPPORTED_CONTENT_ENCODING.base64 = { - check: (data) => isBase64(data) -}; - -const schema = require("../../ext/spec_0_3.json"); - -const ajv = new Ajv({ - extendRefs: true -}); - -const isValidAgainstSchema = ajv.compile(schema); - -function Spec03(_caller) { - this.payload = { - specversion: "0.3", - id: uuidv4() - }; - - if (!_caller) { - _caller = require("../cloudevent.js"); - } - - /* - * Used to inject compatibility methods or attributes - */ - this.caller = _caller; - - /* - * Inject compatibility methods - */ - this.caller.prototype.dataContentEncoding = function(encoding) { - this.spec.dataContentEncoding(encoding); - return this; - }; - this.caller.prototype.getDataContentEncoding = function() { - return this.spec.getDataContentEncoding(); - }; - - this.caller.prototype.dataContentType = function(contentType) { - this.spec.dataContentType(contentType); - return this; - }; - this.caller.prototype.getDataContentType = function() { - return this.spec.getDataContentType(); - }; - - this.caller.prototype.subject = function(_subject) { - this.spec.subject(_subject); - return this; - }; - this.caller.prototype.getSubject = function() { - return this.spec.getSubject(); - }; -} - -/* - * Check the spec constraints - */ -Spec03.prototype.check = function(ce) { - const toCheck = (!ce ? this.payload : ce); - - if (!isValidAgainstSchema(toCheck)) { - 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((dce) => !Object.keys(SUPPORTED_CONTENT_ENCODING).includes(dce)) - .forEach((dce) => { - const err = new TypeError("invalid payload"); - err.errors = [ - `Unsupported content encoding: ${dce}` - ]; - throw err; - }); - - Array.of(toCheck) - .filter((tc) => tc.datacontentencoding) - .filter((tc) => (typeof tc.data) === "string") - .map((tc) => { - const newtc = clone(tc); - newtc.datacontentencoding = - newtc.datacontentencoding.toLocaleLowerCase("en-US"); - - return newtc; - }) - .filter((tc) => Object.keys(SUPPORTED_CONTENT_ENCODING) - .includes(tc.datacontentencoding)) - .filter((tc) => !SUPPORTED_CONTENT_ENCODING[tc.datacontentencoding] - .check(tc.data)) - .forEach((tc) => { - 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; - return this; -}; - -Spec03.prototype.getId = function() { - return this.payload.id; -}; - -Spec03.prototype.source = function(_source) { - this.payload.source = _source; - return this; -}; - -Spec03.prototype.getSource = function() { - return this.payload.source; -}; - -Spec03.prototype.specversion = function() { - // does not set! This is right - return this; -}; - -Spec03.prototype.getSpecversion = function() { - return this.payload.specversion; -}; - -Spec03.prototype.type = function(_type) { - this.payload.type = _type; - return this; -}; - -Spec03.prototype.getType = function() { - return this.payload.type; -}; - -Spec03.prototype.dataContentEncoding = function(encoding) { - this.payload.datacontentencoding = encoding; - return this; -}; - -Spec03.prototype.getDataContentEncoding = function() { - return this.payload.datacontentencoding; -}; - -Spec03.prototype.dataContentType = function(_contenttype) { - this.payload.datacontenttype = _contenttype; - return this; -}; -Spec03.prototype.getDataContentType = function() { - return this.payload.datacontenttype; -}; - -Spec03.prototype.schemaurl = function(_schemaurl) { - this.payload.schemaurl = _schemaurl; - return this; -}; -Spec03.prototype.getSchemaurl = function() { - return this.payload.schemaurl; -}; - -Spec03.prototype.subject = function(_subject) { - this.payload.subject = _subject; - return this; -}; -Spec03.prototype.getSubject = function() { - return this.payload.subject; -}; - -Spec03.prototype.time = function(_time) { - this.payload.time = _time.toISOString(); - return this; -}; -Spec03.prototype.getTime = function() { - return this.payload.time; -}; - -Spec03.prototype.data = function(_data) { - this.payload.data = _data; - return this; -}; -Spec03.prototype.getData = function() { - const dct = this.payload.datacontenttype; - const dce = this.payload.datacontentencoding; - - if (dct && !dce) { - this.payload.data = asData(this.payload.data, dct); - } - - return this.payload.data; -}; - -Spec03.prototype.addExtension = function(key, value) { - if (!Object.prototype.hasOwnProperty.call(RESERVED_ATTRIBUTES, key)) { - this.payload[key] = value; - } else { - throw new TypeError(`Reserved attribute name: '${key}'`); - } - return this; -}; - -module.exports = Spec03; diff --git a/test/bindings/http/receiver_binary_0_3_tests.js b/test/bindings/http/receiver_binary_0_3_tests.js deleted file mode 100644 index 86467c14..00000000 --- a/test/bindings/http/receiver_binary_0_3_tests.js +++ /dev/null @@ -1,429 +0,0 @@ -const expect = require("chai").expect; - -const HTTPBinaryReceiver = - require("../../../lib/bindings/http/receiver_binary_0_3.js"); - -const receiver = new HTTPBinaryReceiver(); - -describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.3", () => { - describe("Check", () => { - it("Throw error when payload arg is null or undefined", () => { - // setup - const payload = null; - const attributes = {}; - - // act and assert - expect(receiver.check.bind(receiver, payload, attributes)) - .to.throw("payload is null or undefined"); - }); - - it("Throw error when attributes arg is null or undefined", () => { - // setup - const payload = {}; - const attributes = null; - - // act and assert - expect(receiver.check.bind(receiver, payload, attributes)) - .to.throw("attributes is null or undefined"); - }); - - it("Throw error when payload is not an object or string", () => { - // setup - const payload = 1.2; - const attributes = {}; - - // act and assert - expect(receiver.check.bind(receiver, payload, attributes)) - .to.throw("payload must be an object or a string"); - }); - - it("Throw error when headers has no 'ce-type'", () => { - // setup - const payload = {}; - const attributes = { - "ce-specversion": "specversion", - "ce-source": "source", - "ce-id": "id", - "Content-Type": "application/json" - }; - - // act and assert - expect(receiver.check.bind(receiver, payload, attributes)) - .to.throw("header 'ce-type' not found"); - }); - - it("Throw error when headers has no 'ce-specversion'", () => { - // setup - const payload = {}; - const attributes = { - "ce-type": "type", - "ce-source": "source", - "ce-id": "id", - "Content-Type": "application/json" - }; - - // act and assert - expect(receiver.check.bind(receiver, payload, attributes)) - .to.throw("header 'ce-specversion' not found"); - }); - - it("Throw error when headers has no 'ce-source'", () => { - // setup - const payload = {}; - const attributes = { - "ce-type": "type", - "ce-specversion": "specversion", - "ce-id": "id", - "Content-Type": "application/json" - }; - - // act and assert - expect(receiver.check.bind(receiver, payload, attributes)) - .to.throw("header 'ce-source' not found"); - }); - - it("Throw error when headers has no 'ce-id'", () => { - // setup - const payload = {}; - const attributes = { - "ce-type": "type", - "ce-specversion": "specversion", - "ce-source": "source", - "Content-Type": "application/json" - }; - - // act and assert - expect(receiver.check.bind(receiver, payload, attributes)) - .to.throw("header 'ce-id' not found"); - }); - - it("Throw error when spec is not 0.3", () => { - // setup - const payload = {}; - const attributes = { - "ce-type": "type", - "ce-specversion": "0.2", - "ce-source": "source", - "ce-id": "id", - "Content-Type": "application/json" - }; - - // act and assert - expect(receiver.parse.bind(receiver, payload, attributes)) - .to.throw("invalid spec version"); - }); - - it("Throw error when the content-type is invalid", () => { - // setup - const payload = {}; - const attributes = { - "ce-type": "type", - "ce-specversion": "specversion", - "ce-source": "source", - "ce-id": "id", - "Content-Type": "text/html" - }; - - // act and assert - expect(receiver.check.bind(receiver, payload, attributes)) - .to.throw("invalid content type"); - }); - - it("No error when all required headers are in place", () => { - // setup - const payload = {}; - const attributes = { - "ce-type": "type", - "ce-specversion": "0.3", - "ce-source": "source", - "ce-id": "id", - "Content-Type": "application/json" - }; - - // act and assert - expect(receiver.check.bind(receiver, payload, attributes)) - .to.not.throw(); - }); - }); - - describe("Parse", () => { - it("CloudEvent contains 'type'", () => { - // setup - const payload = { - data: "dataString" - }; - const attributes = { - "ce-type": "type", - "ce-specversion": "0.3", - "ce-source": "source", - "ce-id": "id", - "ce-time": "2019-06-16T11:42:00Z", - "ce-schemaurl": "http://schema.registry/v1", - "Content-Type": "application/json" - }; - - // act - const actual = receiver.parse(payload, attributes); - - // assert - expect(actual.getType()) - .to.equal("type"); - }); - - it("CloudEvent contains 'specversion'", () => { - // setup - const payload = { - data: "dataString" - }; - const attributes = { - "ce-type": "type", - "ce-specversion": "0.3", - "ce-source": "source", - "ce-id": "id", - "ce-time": "2019-06-16T11:42:00Z", - "ce-schemaurl": "http://schema.registry/v1", - "Content-Type": "application/json" - }; - - // act - const actual = receiver.parse(payload, attributes); - - // assert - expect(actual.getSpecversion()) - .to.equal("0.3"); - }); - - it("CloudEvent contains 'source'", () => { - // setup - const payload = { - data: "dataString" - }; - const attributes = { - "ce-type": "type", - "ce-specversion": "0.3", - "ce-source": "/source", - "ce-id": "id", - "ce-time": "2019-06-16T11:42:00Z", - "ce-schemaurl": "http://schema.registry/v1", - "Content-Type": "application/json" - }; - - // act - const actual = receiver.parse(payload, attributes); - - // assert - expect(actual.getSource()) - .to.equal("/source"); - }); - - it("CloudEvent contains 'id'", () => { - // setup - const payload = { - data: "dataString" - }; - const attributes = { - "ce-type": "type", - "ce-specversion": "0.3", - "ce-source": "/source", - "ce-id": "id", - "ce-time": "2019-06-16T11:42:00Z", - "ce-schemaurl": "http://schema.registry/v1", - "Content-Type": "application/json" - }; - - // act - const actual = receiver.parse(payload, attributes); - - // assert - expect(actual.getId()) - .to.equal("id"); - }); - - it("CloudEvent contains 'time'", () => { - // setup - const payload = { - data: "dataString" - }; - const attributes = { - "ce-type": "type", - "ce-specversion": "0.3", - "ce-source": "/source", - "ce-id": "id", - "ce-time": "2019-06-16T11:42:00.000Z", - "ce-schemaurl": "http://schema.registry/v1", - "Content-Type": "application/json" - }; - - // act - const actual = receiver.parse(payload, attributes); - - // assert - expect(actual.getTime()) - .to.equal("2019-06-16T11:42:00.000Z"); - }); - - it("CloudEvent contains 'schemaurl'", () => { - // setup - const payload = { - data: "dataString" - }; - const attributes = { - "ce-type": "type", - "ce-specversion": "0.3", - "ce-source": "/source", - "ce-id": "id", - "ce-time": "2019-06-16T11:42:00Z", - "ce-schemaurl": "http://schema.registry/v1", - "Content-Type": "application/json" - }; - - // act - const actual = receiver.parse(payload, attributes); - - // assert - expect(actual.getSchemaurl()) - .to.equal("http://schema.registry/v1"); - }); - - it("CloudEvent contains 'datacontenttype' (application/json)", () => { - // setup - const payload = { - data: "dataString" - }; - const attributes = { - "ce-type": "type", - "ce-specversion": "0.3", - "ce-source": "/source", - "ce-id": "id", - "ce-time": "2019-06-16T11:42:00Z", - "ce-schemaurl": "http://schema.registry/v1", - "Content-Type": "application/json" - }; - - // act - const actual = receiver.parse(payload, attributes); - - // assert - expect(actual.getDataContentType()) - .to.equal("application/json"); - }); - - it("CloudEvent contains 'datacontenttype' (application/octet-stream)", - () => { - // setup - const payload = "The payload is binary data"; - const attributes = { - "ce-type": "type", - "ce-specversion": "0.3", - "ce-source": "/source", - "ce-id": "id", - "ce-time": "2019-06-16T11:42:00Z", - "ce-schemaurl": "http://schema.registry/v1", - "Content-Type": "application/octet-stream" - }; - - // act - const actual = receiver.parse(payload, attributes); - - // assert - expect(actual.getDataContentType()) - .to.equal("application/octet-stream"); - }); - - it("CloudEvent contains 'data' (application/json)", () => { - // setup - const payload = { - data: "dataString" - }; - const attributes = { - "ce-type": "type", - "ce-specversion": "0.3", - "ce-source": "/source", - "ce-id": "id", - "ce-time": "2019-06-16T11:42:00Z", - "ce-schemaurl": "http://schema.registry/v1", - "Content-Type": "application/json" - }; - - // act - const actual = receiver.parse(payload, attributes); - - // assert - expect(actual.getData()) - .to.deep.equal(payload); - }); - - it("CloudEvent contains 'data' (application/octet-stream)", () => { - // setup - const payload = "The payload is binary data"; - const attributes = { - "ce-type": "type", - "ce-specversion": "0.3", - "ce-source": "/source", - "ce-id": "id", - "ce-time": "2019-06-16T11:42:00Z", - "ce-schemaurl": "http://schema.registry/v1", - "Content-Type": "application/octet-stream" - }; - - // act - const actual = receiver.parse(payload, attributes); - - // assert - expect(actual.getData()) - .to.deep.equal(payload); - }); - - it("No error when all attributes are in place", () => { - // setup - const payload = { - data: "dataString" - }; - const attributes = { - "ce-type": "type", - "ce-specversion": "0.3", - "ce-source": "source", - "ce-id": "id", - "ce-time": "2019-06-16T11:42:00Z", - "ce-schemaurl": "http://schema.registry/v1", - "Content-Type": "application/json" - }; - - // act - const actual = receiver.parse(payload, attributes); - - // assert - expect(actual) - .to.be.an("object"); - - expect(actual) - .to.have.property("format"); - }); - - it("Should accept 'extension1'", () => { - // setup - const extension1 = "mycuston-ext1"; - const payload = { - data: "dataString" - }; - const attributes = { - "ce-type": "type", - "ce-specversion": "0.3", - "ce-source": "source", - "ce-id": "id", - "ce-time": "2019-06-16T11:42:00Z", - "ce-schemaurl": "http://schema.registry/v1", - "Content-Type": "application/json", - "ce-extension1": extension1 - }; - - // act - const actual = receiver.parse(payload, attributes); - const actualExtensions = actual.getExtensions(); - - // assert - expect(actualExtensions.extension1) - .to.equal(extension1); - }); - }); -}); diff --git a/test/bindings/http/receiver_structured_0_3_test.js b/test/bindings/http/receiver_structured_0_3_test.js deleted file mode 100644 index a3742403..00000000 --- a/test/bindings/http/receiver_structured_0_3_test.js +++ /dev/null @@ -1,219 +0,0 @@ -const expect = require("chai").expect; -const v03 = require("../../../v03/index.js"); - -const HTTPStructuredReceiver = - require("../../../lib/bindings/http/receiver_structured_0_3.js"); - -const receiver = new HTTPStructuredReceiver(); - -const type = "com.github.pull.create"; -const source = "urn:event:from:myapi/resourse/123"; -const now = new Date(); -const schemaurl = "http://cloudevents.io/schema.json"; - -const ceContentType = "application/json"; - -const data = { - foo: "bar" -}; - -const ext1Name = "extension1"; -const ext1Value = "foobar"; -const ext2Name = "extension2"; -const ext2Value = "acme"; - -describe("HTTP Transport Binding Structured Receiver CloudEvents v0.3", () => { - describe("Check", () => { - it("Throw error when payload arg is null or undefined", () => { - // setup - const payload = null; - const attributes = {}; - - // act and assert - expect(receiver.check.bind(receiver, payload, attributes)) - .to.throw("payload is null or undefined"); - }); - - it("Throw error when attributes arg is null or undefined", () => { - // setup - const payload = {}; - const attributes = null; - - // act and assert - expect(receiver.check.bind(receiver, payload, attributes)) - .to.throw("attributes is null or undefined"); - }); - - it("Throw error when payload is not an object or string", () => { - // setup - const payload = 1.0; - const attributes = {}; - - // act and assert - expect(receiver.check.bind(receiver, payload, attributes)) - .to.throw("payload must be an object or string"); - }); - - it("Throw error when the content-type is invalid", () => { - // setup - const payload = {}; - const attributes = { - "Content-Type": "text/html" - }; - - // act and assert - expect(receiver.check.bind(receiver, payload, attributes)) - .to.throw("invalid content type"); - }); - - it("Throw error data content encoding is base64, but 'data' is not", - () => { - // setup - const payload = v03.event() - .type(type) - .source(source) - .dataContentType("text/plain") - .dataContentEncoding("base64") - .time(now) - .schemaurl(schemaurl) - .data("No base 64 value") - .addExtension(ext1Name, ext1Value) - .addExtension(ext2Name, ext2Value) - .toString(); - - const attributes = { - "Content-Type": "application/cloudevents+json" - }; - - // act and assert - expect(receiver.parse.bind(receiver, payload, attributes)) - .to.throw("invalid payload"); - }); - - it("No error when all required stuff are in place", () => { - // setup - const payload = {}; - const attributes = { - "Content-Type": "application/cloudevents+json" - }; - - // act and assert - expect(receiver.check.bind(receiver, payload, attributes)) - .to.not.throw(); - }); - }); - - describe("Parse", () => { - it("Throw error when the event does not follow the spec", () => { - // setup - const payload = {}; - const attributes = { - "Content-Type": "application/cloudevents+json" - }; - - // act and assert - expect(receiver.check.bind(receiver, payload, attributes)) - .to.not.throw(); - }); - }); - - describe("Parse", () => { - it("Throw error when the event does not follow the spec", () => { - // setup - const payload = - v03.event() - .type(type) - .source(source) - .time(now) - .schemaurl(schemaurl) - .data(data) - .toString(); - - const headers = { - "Content-Type": "application/cloudevents+xml" - }; - - // act and assert - expect(receiver.parse.bind(receiver, payload, headers)) - .to.throw("invalid content type"); - }); - - it("Should accept event that follows the spec", () => { - // setup - const id = "id-x0dk"; - const payload = v03.event() - .type(type) - .source(source) - .id(id) - .dataContentType(ceContentType) - .time(now) - .schemaurl(schemaurl) - .data(data) - .toString(); - const headers = { - "content-type": "application/cloudevents+json" - }; - - // act - const actual = receiver.parse(payload, headers); - - // assert - expect(actual) - .to.be.an("object"); - - expect(actual) - .to.have.property("format"); - - expect(actual.getId()) - .to.equals(id); - }); - - it("Should accept 'extension1'", () => { - // setup - const extension1 = "mycuston-ext1"; - const payload = v03.event() - .type(type) - .source(source) - .dataContentType(ceContentType) - .time(now) - .schemaurl(schemaurl) - .data(data) - .addExtension("extension1", extension1) - .toString(); - - const headers = { - "content-type": "application/cloudevents+json" - }; - - // act - const actual = receiver.parse(payload, headers); - const actualExtensions = actual.getExtensions(); - - // assert - expect(actualExtensions.extension1) - .to.equal(extension1); - }); - - it("Should parse 'data' stringfied json to json object", () => { - // setup - const payload = v03.event() - .type(type) - .source(source) - .dataContentType(ceContentType) - .time(now) - .schemaurl(schemaurl) - .data(JSON.stringify(data)) - .toString(); - - const headers = { - "content-type": "application/cloudevents+json" - }; - - // act - const actual = receiver.parse(payload, headers); - - // assert - expect(actual.getData()).to.deep.equal(data); - }); - }); -}); diff --git a/test/bindings/http/unmarshaller_0_3_tests.js b/test/bindings/http/unmarshaller_0_3_tests.js deleted file mode 100644 index 6bc3deb6..00000000 --- a/test/bindings/http/unmarshaller_0_3_tests.js +++ /dev/null @@ -1,302 +0,0 @@ -const expect = require("chai").expect; -const Unmarshaller = require("../../../lib/bindings/http/unmarshaller_0_3.js"); -const CloudEvent = require("../../../index.js"); -const v03 = require("../../../v03/index.js"); - -const type = "com.github.pull.create"; -const source = "urn:event:from:myapi/resourse/123"; -const now = new Date(); -const schemaurl = "http://cloudevents.io/schema.json"; -const subject = "subject.ext"; -const { - BINARY_HEADERS_03, - HEADER_CONTENT_TYPE -} = require("../../../lib/bindings/http/constants.js"); - -const ceContentType = "application/json"; - -const data = { - foo: "bar" -}; - -describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => { - it("Throw error when payload is null", () => { - // setup - const payload = null; - const un = new Unmarshaller(); - - // act and assert - return un.unmarshall(payload) - .then(() => { throw new Error("failed"); }) - .catch((err) => - expect(err.message).to.equal("payload is null or undefined")); - }); - - it("Throw error when headers is null", () => { - // setup - const payload = {}; - const headers = null; - const un = new Unmarshaller(); - - // act and assert - return un.unmarshall(payload, headers) - .then(() => { throw new Error("failed"); }) - .catch((err) => - expect(err.message).to.equal("headers is null or undefined")); - }); - - it("Throw error when there is no content-type header", () => { - // setup - const payload = {}; - const headers = {}; - const un = new Unmarshaller(); - - // act and assert - un.unmarshall(payload, headers) - .then(() => { throw new Error("failed"); }) - .catch((err) => - expect(err.message).to.equal("content-type header not found")); - }); - - it("Throw error when content-type is not allowed", () => { - // setup - const payload = {}; - const headers = { - "content-type": "text/xml" - }; - const un = new Unmarshaller(); - - // act and assert - un.unmarshall(payload, headers) - .then(() => { throw new Error("failed"); }) - .catch((err) => - expect(err.message).to.equal("content type not allowed")); - }); - - describe("Structured", () => { - it("Throw error when has not allowed mime", () => { - // setup - const payload = {}; - const headers = { - "content-type": "application/cloudevents+zip" - }; - const un = new Unmarshaller(); - - // act and assert - un.unmarshall(payload, headers) - .then(() => { throw new Error("failed"); }) - .catch((err) => - expect(err.message).to.equal("structured+type not allowed")); - }); - - it("Throw error when the event does not follow the spec 0.3", () => { - // setup - const payload = - new v03.CloudEvent(v03.Spec) - .type(type) - .source(source) - .dataContentType(ceContentType) - .time(now) - .schemaurl(schemaurl) - .data(data) - .toString(); - - const headers = { - "content-type": "application/cloudevents+json" - }; - - const un = new Unmarshaller(); - - // act and assert - un.unmarshall(payload, headers) - .then(() => { throw new Error("failed"); }) - .catch((err) => - expect(err.message).to.equal("invalid payload")); - }); - - it("Should accept event that follow the spec 0.3", () => { - // setup - const payload = - new CloudEvent(v03.Spec) - .type(type) - .source(source) - .dataContentType(ceContentType) - .time(now) - .schemaurl(schemaurl) - .subject(subject) - .data(data) - .toString(); - - const headers = { - "content-type": "application/cloudevents+json" - }; - - const un = new Unmarshaller(); - - // act and assert - return un.unmarshall(payload, headers) - .then((actual) => - expect(actual).to.be.an("object")) - .catch((err) => { - console.error(err); - throw err; - }); - }); - - it("Should parse 'data' stringfied json to json object", () => { - // setup - const payload = - new CloudEvent(v03.Spec) - .type(type) - .source(source) - .dataContentType(ceContentType) - .time(now) - .schemaurl(schemaurl) - .subject(subject) - .data(JSON.stringify(data)) - .toString(); - - const headers = { - "content-type": "application/cloudevents+json" - }; - - const un = new Unmarshaller(); - - // act and assert - return un.unmarshall(payload, headers) - .then((actual) => { - expect(actual.getData()).to.deep.equal(data); - }) - .catch((err) => { - console.error(err); - throw err; - }); - }); - }); - - describe("Binary", () => { - it("Throw error when has not allowed mime", () => { - // setup - const payload = { - data: "dataString" - }; - const attributes = { - [BINARY_HEADERS_03.TYPE]: "type", - [BINARY_HEADERS_03.SPEC_VERSION]: "0.3", - [BINARY_HEADERS_03.SOURCE]: "source", - [BINARY_HEADERS_03.ID]: "id", - [BINARY_HEADERS_03.TIME]: "2019-06-16T11:42:00Z", - [BINARY_HEADERS_03.SCHEMA_URL]: "http://schema.registry/v1", - [HEADER_CONTENT_TYPE]: "text/html" - }; - - const un = new Unmarshaller(); - - // act and assert - un.unmarshall(payload, attributes) - .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 - const payload = { - data: "dataString" - }; - const attributes = { - [BINARY_HEADERS_03.TYPE]: "type", - "CE-CloudEventsVersion": "0.1", - [BINARY_HEADERS_03.SOURCE]: "source", - [BINARY_HEADERS_03.ID]: "id", - [BINARY_HEADERS_03.TIME]: "2019-06-16T11:42:00Z", - [BINARY_HEADERS_03.SCHEMA_URL]: "http://schema.registry/v1", - [HEADER_CONTENT_TYPE]: "application/json" - }; - - const un = new Unmarshaller(); - - // act and assert - un.unmarshall(payload, attributes) - .then(() => { throw new Error("failed"); }) - .catch((err) => - expect(err.message).to.not.empty); - }); - - it("No error when all attributes are in place", () => { - // setup - const payload = { - data: "dataString" - }; - const attributes = { - [BINARY_HEADERS_03.TYPE]: "type", - [BINARY_HEADERS_03.SPEC_VERSION]: "0.3", - [BINARY_HEADERS_03.SOURCE]: "source", - [BINARY_HEADERS_03.ID]: "id", - [BINARY_HEADERS_03.TIME]: "2019-06-16T11:42:00Z", - [BINARY_HEADERS_03.SCHEMA_URL]: "http://schema.registry/v1", - [HEADER_CONTENT_TYPE]: "application/json" - }; - - const un = new Unmarshaller(); - - // act and assert - un.unmarshall(payload, attributes) - .then((actual) => expect(actual).to.be.an("object")); - }); - - it("Throw error when 'ce-datacontentencoding' is not allowed", () => { - // setup - const payload = "eyJtdWNoIjoid293In0="; - - const attributes = { - [BINARY_HEADERS_03.TYPE]: "type", - [BINARY_HEADERS_03.SPEC_VERSION]: "0.3", - [BINARY_HEADERS_03.SOURCE]: "source", - [BINARY_HEADERS_03.ID]: "id", - [BINARY_HEADERS_03.TIME]: "2019-06-16T11:42:00Z", - [BINARY_HEADERS_03.SCHEMA_URL]: "http://schema.registry/v1", - [HEADER_CONTENT_TYPE]: "application/json", - [BINARY_HEADERS_03.CONTENT_ENCONDING]: "binary" - }; - - const un = new Unmarshaller(); - - // act and assert - return un.unmarshall(payload, attributes) - .then(() => { throw new Error("failed"); }) - .catch((err) => { - expect(err.message).to.equal("unsupported datacontentencoding"); - }); - }); - - it("No error when 'ce-datacontentencoding' is base64", () => { - // setup - const payload = "eyJtdWNoIjoid293In0="; - const expected = { - much: "wow" - }; - - const attributes = { - [BINARY_HEADERS_03.TYPE]: "type", - [BINARY_HEADERS_03.SPEC_VERSION]: "0.3", - [BINARY_HEADERS_03.SOURCE]: "source", - [BINARY_HEADERS_03.ID]: "id", - [BINARY_HEADERS_03.TIME]: "2019-06-16T11:42:00Z", - [BINARY_HEADERS_03.SCHEMA_URL]: "http://schema.registry/v1", - [HEADER_CONTENT_TYPE]: "application/json", - [BINARY_HEADERS_03.CONTENT_ENCONDING]: "base64" - }; - - const un = new Unmarshaller(); - - // act and assert - return un.unmarshall(payload, attributes) - .then((actual) => expect(actual.getData()).to.deep.equal(expected)) - .catch((err) => { - console.error(err); - throw err; - }); - }); - }); -}); diff --git a/test/http_binding_0_3.js b/test/http_binding_0_3.js deleted file mode 100644 index d1e47cee..00000000 --- a/test/http_binding_0_3.js +++ /dev/null @@ -1,241 +0,0 @@ -const expect = require("chai").expect; -const nock = require("nock"); -const BinaryHTTPEmitter = - require("../lib/bindings/http/emitter_binary_0_3.js"); -const CloudEvent = require("../lib/cloudevent.js"); -const v03 = require("../v03/index.js"); - -const type = "com.github.pull.create"; -const source = "urn:event:from:myapi/resourse/123"; -const contentEncoding = "base64"; -const contentType = "application/cloudevents+json; charset=utf-8"; -const now = new Date(); -const schemaurl = "http://cloudevents.io/schema.json"; - -const ceContentType = "application/json"; - -const data = { - foo: "bar" -}; -const dataBase64 = "Y2xvdWRldmVudHMK"; - -const ext1Name = "extension1"; -const ext1Value = "foobar"; -const ext2Name = "extension2"; -const ext2Value = "acme"; - -const cloudevent = - new CloudEvent(v03.Spec) - .type(type) - .source(source) - .dataContentType(ceContentType) - .subject("subject.ext") - .time(now) - .schemaurl(schemaurl) - .data(data) - .addExtension(ext1Name, ext1Value) - .addExtension(ext2Name, ext2Value); - -const cebase64 = - new CloudEvent(v03.Spec) - .type(type) - .source(source) - .dataContentType(ceContentType) - .dataContentEncoding(contentEncoding) - .time(now) - .schemaurl(schemaurl) - .data(dataBase64) - .addExtension(ext1Name, ext1Value) - .addExtension(ext2Name, ext2Value); - -const webhook = "https://cloudevents.io/webhook"; -const httpcfg = { - method: "POST", - url: `${webhook}/json` -}; - -const binary = new BinaryHTTPEmitter(httpcfg); -const structured = new v03.StructuredHTTPEmitter(httpcfg); - -describe("HTTP Transport Binding - Version 0.3", () => { - beforeEach(() => { - // Mocking the webhook - nock(webhook) - .post("/json") - .reply(201, { status: "accepted" }); - }); - - describe("Structured", () => { - 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("'data' attribute with 'base64' encoding", () => { - 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()} 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("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'", () => binary.emit(cloudevent) - .then((response) => { - expect(response.config.headers) - .to.have.property("ce-specversion"); - })); - - 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'", () => binary.emit(cloudevent) - .then((response) => { - expect(response.config.headers) - .to.have.property("ce-id"); - })); - - 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'", () => binary.emit(cloudevent) - .then((response) => { - expect(response.config.headers) - .to.have.property("ce-schemaurl"); - })); - - it(`HTTP Header contains 'ce-${ext1Name}'`, () => binary.emit(cloudevent) - .then((response) => { - expect(response.config.headers) - .to.have.property(`ce-${ext1Name}`); - })); - - it(`HTTP Header contains 'ce-${ext2Name}'`, () => binary.emit(cloudevent) - .then((response) => { - expect(response.config.headers) - .to.have.property(`ce-${ext2Name}`); - })); - - 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", () => binary.emit(cloudevent) - .then((response) => { - expect(cloudevent.getType()) - .to.equal(response.config.headers["ce-type"]); - })); - - 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", - () => binary.emit(cloudevent) - .then((response) => { - expect(cloudevent.getSource()) - .to.equal(response.config.headers["ce-source"]); - })); - - 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", () => binary.emit(cloudevent) - .then((response) => { - expect(cloudevent.getTime()) - .to.equal(response.config.headers["ce-time"]); - })); - - 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`, - () => binary.emit(cloudevent) - .then((response) => { - expect(cloudevent.getExtensions()[ext1Name]) - .to.equal(response.config.headers[`ce-${ext1Name}`]); - })); - - it(`should 'ce-${ext2Name}' have the right value`, - () => binary.emit(cloudevent) - .then((response) => { - expect(cloudevent.getExtensions()[ext2Name]) - .to.equal(response.config.headers[`ce-${ext2Name}`]); - })); - - 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'", - () => binary.emit(cebase64) - .then((response) => { - expect(response.config.headers) - .to.have.property("ce-datacontentencoding"); - })); - - it("should 'ce-datacontentencoding' have the right value", - () => binary.emit(cloudevent) - .then((response) => { - expect(cloudevent.getDataContentEncoding()) - .to.equal(response.config.headers["ce-datacontentencoding"]); - })); - }); - }); - }); -}); diff --git a/test/sdk_test.js b/test/sdk_test.js index 90e03870..fb3f7ee1 100644 --- a/test/sdk_test.js +++ b/test/sdk_test.js @@ -1,42 +1,7 @@ const expect = require("chai").expect; -const v03 = require("../v03/index.js"); const v1 = require("../v1/index.js"); describe("The SDK Requirements", () => { - describe("v0.3", () => { - it("should create an event using the right spec version", () => { - expect(v03.event().spec.payload.specversion).to.equal("0.3"); - }); - - it("should exports 'Spec'", () => { - expect(v03).to.have.property("Spec"); - }); - - it("should exports 'StructuredHTTPEmitter'", () => { - expect(v03).to.have.property("StructuredHTTPEmitter"); - }); - - it("should exports 'StructuredHTTPReceiver'", () => { - expect(v03).to.have.property("StructuredHTTPReceiver"); - }); - - it("should exports 'BinaryHTTPEmitter'", () => { - expect(v03).to.have.property("BinaryHTTPEmitter"); - }); - - it("should exports 'BinaryHTTPReceiver'", () => { - expect(v03).to.have.property("BinaryHTTPReceiver"); - }); - - it("should exports 'HTTPUnmarshaller'", () => { - expect(v03).to.have.property("HTTPUnmarshaller"); - }); - - it("should exports 'event'", () => { - expect(v03).to.have.property("event"); - }); - }); - describe("v1.0", () => { it("should create an event using the right spec version", () => { expect(v1.event().spec.payload.specversion).to.equal("1.0"); diff --git a/test/spec_0_3_tests.js b/test/spec_0_3_tests.js deleted file mode 100644 index ad6cfbfe..00000000 --- a/test/spec_0_3_tests.js +++ /dev/null @@ -1,230 +0,0 @@ -const expect = require("chai").expect; -const Spec03 = require("../lib/specs/spec_0_3.js"); -const CloudEvent = require("../index.js"); -const { v4: uuidv4 } = require("uuid"); - -const id = uuidv4(); -const type = "com.github.pull.create"; -const source = "urn:event:from:myapi/resourse/123"; -const time = new Date(); -const schemaurl = "http://example.com/registry/myschema.json"; -const dataContentEncoding = "base64"; -const dataContentType = "application/json"; -const data = { - much: "wow" -}; -const subject = "subject-x0"; - -const cloudevent = - new CloudEvent(Spec03) - .id(id) - .source(source) - .type(type) - .dataContentType(dataContentType) - .schemaurl(schemaurl) - .subject(subject) - .time(time) - .data(data); - -describe("CloudEvents Spec v0.3", () => { - describe("REQUIRED Attributes", () => { - it("Should have 'id'", () => { - expect(cloudevent.getId()).to.equal(id); - }); - - it("Should have 'source'", () => { - expect(cloudevent.getSource()).to.equal(source); - }); - - it("Should have 'specversion'", () => { - expect(cloudevent.getSpecversion()).to.equal("0.3"); - }); - - it("Should have 'type'", () => { - expect(cloudevent.getType()).to.equal(type); - }); - }); - - describe("OPTIONAL Attributes", () => { - it("Should have 'datacontentencoding'", () => { - cloudevent.dataContentEncoding(dataContentEncoding); - expect(cloudevent.spec.payload.datacontentencoding) - .to.equal(dataContentEncoding); - delete cloudevent.spec.payload.datacontentencoding; - }); - - it("Should have 'datacontenttype'", () => { - expect(cloudevent.getDataContentType()).to.equal(dataContentType); - }); - - it("Should have 'schemaurl'", () => { - expect(cloudevent.getSchemaurl()).to.equal(schemaurl); - }); - - it("Should have 'subject'", () => { - expect(cloudevent.getSubject()).to.equal(subject); - }); - - it("Should have 'time'", () => { - expect(cloudevent.getTime()).to.equal(time.toISOString()); - }); - - it("Should have 'data'", () => { - expect(cloudevent.getData()).to.deep.equal(data); - }); - - it("Should have the 'extension1'", () => { - cloudevent.addExtension("extension1", "value1"); - expect(cloudevent.spec.payload.extension1) - .to.equal("value1"); - }); - - it("should throw an error when use a reserved name as extension", () => { - expect(cloudevent.addExtension.bind(cloudevent, "id")) - .to.throw("Reserved attribute name: 'id'"); - }); - }); - - describe("The Constraints check", () => { - describe("'id'", () => { - it("should throw an error when is absent", () => { - delete cloudevent.spec.payload.id; - expect(cloudevent.format.bind(cloudevent)) - .to - .throw("invalid payload"); - cloudevent.spec.payload.id = id; - }); - - it("should throw an erro when is empty", () => { - cloudevent.spec.payload.id = ""; - expect(cloudevent.format.bind(cloudevent)) - .to - .throw("invalid payload"); - cloudevent.spec.payload.id = id; - }); - }); - - describe("'source'", () => { - it("should throw an error when is absent", () => { - delete cloudevent.spec.payload.source; - expect(cloudevent.format.bind(cloudevent)) - .to - .throw("invalid payload"); - cloudevent.spec.payload.source = source; - }); - }); - - describe("'specversion'", () => { - it("should throw an error when is absent", () => { - delete cloudevent.spec.payload.specversion; - expect(cloudevent.format.bind(cloudevent)) - .to - .throw("invalid payload"); - cloudevent.spec.payload.specversion = "0.3"; - }); - - it("should throw an error when is empty", () => { - cloudevent.spec.payload.specversion = ""; - expect(cloudevent.format.bind(cloudevent)) - .to - .throw("invalid payload"); - cloudevent.spec.payload.specversion = "0.3"; - }); - }); - - describe("'type'", () => { - it("should throw an error when is absent", () => { - delete cloudevent.spec.payload.type; - expect(cloudevent.format.bind(cloudevent)) - .to - .throw("invalid payload"); - cloudevent.spec.payload.type = type; - }); - - it("should throw an error when is an empty string", () => { - cloudevent.type(""); - expect(cloudevent.format.bind(cloudevent)) - .to - .throw("invalid payload"); - cloudevent.type(type); - }); - - it("must be a non-empty string", () => { - cloudevent.type(type); - expect(cloudevent.spec.payload.type).to.equal(type); - }); - }); - - describe("'datacontentencoding'", () => { - it("should throw an error when is a unsupported encoding", () => { - cloudevent - .data("Y2xvdWRldmVudHMK") - .dataContentEncoding("binary"); - expect(cloudevent.format.bind(cloudevent)) - .to - .throw("invalid payload"); - delete cloudevent.spec.payload.datacontentencoding; - cloudevent.data(data); - }); - - it("should throw an error when 'data' does not carry base64", - () => { - cloudevent - .data("no base 64 value") - .dataContentEncoding("base64") - .dataContentType("text/plain"); - - expect(cloudevent.format.bind(cloudevent)) - .to - .throw("invalid payload"); - - delete cloudevent.spec.payload.datacontentencoding; - cloudevent.data(data); - }); - - it("should accept when 'data' is a string", () => { - cloudevent - .data("Y2xvdWRldmVudHMK") - .dataContentEncoding("base64"); - expect(cloudevent.format()).to.have.property("datacontentencoding"); - delete cloudevent.spec.payload.datacontentencoding; - cloudevent.data(data); - }); - }); - - describe("'data'", () => { - it("should maintain the type of data when no data content type", () => { - delete cloudevent.spec.payload.datacontenttype; - cloudevent - .data(JSON.stringify(data)); - - expect(typeof cloudevent.getData()).to.equal("string"); - cloudevent.dataContentType(dataContentType); - }); - - it("should convert data with stringified json to a json object", () => { - cloudevent - .dataContentType(dataContentType) - .data(JSON.stringify(data)); - expect(cloudevent.getData()).to.deep.equal(data); - }); - }); - - describe("'subject'", () => { - it("should throw an error when is an empty string", () => { - cloudevent.subject(""); - expect(cloudevent.format.bind(cloudevent)) - .to - .throw("invalid payload"); - cloudevent.subject(type); - }); - }); - - describe("'time'", () => { - it("must adhere to the format specified in RFC 3339", () => { - cloudevent.time(time); - expect(cloudevent.format().time).to.equal(time.toISOString()); - }); - }); - }); -}); diff --git a/v03/index.js b/v03/index.js deleted file mode 100644 index 0567070e..00000000 --- a/v03/index.js +++ /dev/null @@ -1,28 +0,0 @@ -const CloudEvent = require("../lib/cloudevent.js"); -const Spec = require("../lib/specs/spec_0_3.js"); -const StructuredHTTPEmitter = - require("../lib/bindings/http/emitter_structured.js"); -const BinaryHTTPEmitter = require("../lib/bindings/http/emitter_binary_0_3.js"); - -const StructuredHTTPReceiver = - require("../lib/bindings/http/receiver_structured_0_3.js"); - -const BinaryHTTPReceiver = - require("../lib/bindings/http/receiver_binary_0_3.js"); - -const HTTPUnmarshaller = require("../lib/bindings/http/unmarshaller_0_3.js"); - -function newEvent() { - return new CloudEvent(Spec); -} - -module.exports = { - Spec, - StructuredHTTPEmitter, - StructuredHTTPReceiver, - BinaryHTTPEmitter, - BinaryHTTPReceiver, - HTTPUnmarshaller, - CloudEvent: newEvent, - event: newEvent -};