Skip to content

test: use constants in spec_03_tests.js #144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 12, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions test/spec_0_3_tests.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
const expect = require("chai").expect;
const Spec03 = require("../lib/specs/spec_0_3.js");
const { CloudEvent } = require("../index.js");
const {
MIME_JSON,
ENCODING_BASE64,
SPEC_V03
} = require("../lib/bindings/http/constants.js");

const id = "97699ec2-a8d9-47c1-bfa0-ff7aa526f838";
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"
};
Expand All @@ -19,7 +22,7 @@ const cloudevent =
.id(id)
.source(source)
.type(type)
.dataContentType(dataContentType)
.dataContentType(MIME_JSON)
.schemaurl(schemaurl)
.subject(subject)
.time(time)
Expand All @@ -36,7 +39,7 @@ describe("CloudEvents Spec v0.3", () => {
});

it("Should have 'specversion'", () => {
expect(cloudevent.getSpecversion()).to.equal("0.3");
expect(cloudevent.getSpecversion()).to.equal(SPEC_V03);
});

it("Should have 'type'", () => {
Expand All @@ -46,14 +49,14 @@ describe("CloudEvents Spec v0.3", () => {

describe("OPTIONAL Attributes", () => {
it("Should have 'datacontentencoding'", () => {
cloudevent.dataContentEncoding(dataContentEncoding);
cloudevent.dataContentEncoding(ENCODING_BASE64);
expect(cloudevent.spec.payload.datacontentencoding)
.to.equal(dataContentEncoding);
.to.equal(ENCODING_BASE64);
delete cloudevent.spec.payload.datacontentencoding;
});

it("Should have 'datacontenttype'", () => {
expect(cloudevent.getDataContentType()).to.equal(dataContentType);
expect(cloudevent.getDataContentType()).to.equal(MIME_JSON);
});

it("Should have 'schemaurl'", () => {
Expand Down Expand Up @@ -119,15 +122,15 @@ describe("CloudEvents Spec v0.3", () => {
expect(cloudevent.format.bind(cloudevent))
.to
.throw("invalid payload");
cloudevent.spec.payload.specversion = "0.3";
cloudevent.spec.payload.specversion = SPEC_V03;
});

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";
cloudevent.spec.payload.specversion = SPEC_V03;
});
});

Expand Down Expand Up @@ -170,7 +173,7 @@ describe("CloudEvents Spec v0.3", () => {
() => {
cloudevent
.data("no base 64 value")
.dataContentEncoding("base64")
.dataContentEncoding(ENCODING_BASE64)
.dataContentType("text/plain");

expect(cloudevent.format.bind(cloudevent))
Expand All @@ -184,7 +187,7 @@ describe("CloudEvents Spec v0.3", () => {
it("should accept when 'data' is a string", () => {
cloudevent
.data("Y2xvdWRldmVudHMK")
.dataContentEncoding("base64");
.dataContentEncoding(ENCODING_BASE64);
expect(cloudevent.format()).to.have.property("datacontentencoding");
delete cloudevent.spec.payload.datacontentencoding;
cloudevent.data(data);
Expand All @@ -198,12 +201,12 @@ describe("CloudEvents Spec v0.3", () => {
.data(JSON.stringify(data));

expect(typeof cloudevent.getData()).to.equal("string");
cloudevent.dataContentType(dataContentType);
cloudevent.dataContentType(MIME_JSON);
});

it("should convert data with stringified json to a json object", () => {
cloudevent
.dataContentType(dataContentType)
.dataContentType(MIME_JSON)
.data(JSON.stringify(data));
expect(cloudevent.getData()).to.deep.equal(data);
});
Expand Down