Skip to content
New issue

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

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

Already on GitHub? # to your account

feat(unmarshaller)!: remove asynchronous 0.3 unmarshaller API #126

Merged
merged 3 commits into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
34 changes: 16 additions & 18 deletions lib/bindings/http/unmarshaller.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,25 @@ class Unmarshaller {
}

unmarshall(payload, headers) {
return new Promise((resolve, reject) => {
if (!payload) {
return reject(new TypeError("payload is null or undefined"));
}
if (!headers) {
return reject(new TypeError("headers is null or undefined"));
}
if (!payload) {
throw new TypeError("payload is null or undefined");
}
if (!headers) {
throw new TypeError("headers is null or undefined");
}

// Validation level 1
const sanityHeaders = Commons.sanityAndClone(headers);
if (!sanityHeaders[Constants.HEADER_CONTENT_TYPE]) {
throw new TypeError("content-type header not found");
}
// Validation level 1
const sanityHeaders = Commons.sanityAndClone(headers);
if (!sanityHeaders[Constants.HEADER_CONTENT_TYPE]) {
throw new TypeError("content-type header not found");
}

// Resolve the binding
const bindingName = resolveBindingName(payload, sanityHeaders);
const cloudevent = this.receiverByBinding[bindingName]
.parse(payload, sanityHeaders);
// Resolve the binding
const bindingName = resolveBindingName(payload, sanityHeaders);
const cloudevent = this.receiverByBinding[bindingName]
.parse(payload, sanityHeaders);

resolve(cloudevent);
});
return cloudevent;
}
}

Expand Down
113 changes: 53 additions & 60 deletions test/bindings/http/unmarshaller_0_3_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
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"));
try {
lance marked this conversation as resolved.
Show resolved Hide resolved
un.unmarshall(payload);
} catch (err) {
expect(err.message).to.equal("payload is null or undefined");
}
});

it("Throw error when headers is null", () => {
Expand All @@ -39,10 +40,11 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
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"));
try {
un.unmarshall(payload, headers);
} catch (err) {
expect(err.message).to.equal("headers is null or undefined");
}
});

it("Throw error when there is no content-type header", () => {
Expand All @@ -52,10 +54,11 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
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"));
try {
un.unmarshall(payload, headers);
} catch (err) {
expect(err.message).to.equal("content-type header not found");
}
});

it("Throw error when content-type is not allowed", () => {
Expand All @@ -67,10 +70,11 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
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"));
try {
un.unmarshall(payload, headers);
} catch (err) {
expect(err.message).to.equal("content type not allowed");
}
});

describe("Structured", () => {
Expand All @@ -83,10 +87,11 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
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"));
try {
un.unmarshall(payload, headers);
} catch (err) {
expect(err.message).to.equal("structured+type not allowed");
}
});

it("Throw error when the event does not follow the spec 0.3", () => {
Expand All @@ -108,10 +113,11 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
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"));
try {
un.unmarshall(payload, headers);
} catch (err) {
expect(err.message).to.equal("invalid payload");
}
});

it("Should accept event that follow the spec 0.3", () => {
Expand All @@ -134,13 +140,8 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
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;
});
const event = un.unmarshall(payload, headers);
expect(event instanceof CloudEvent).to.equal(true);
});

it("Should parse 'data' stringfied json to json object", () => {
Expand All @@ -163,14 +164,8 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
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;
});
const event = un.unmarshall(payload, headers);
expect(event.getData()).to.deep.equal(data);
});
});

Expand All @@ -193,10 +188,11 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
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"));
try {
un.unmarshall(payload, attributes);
} catch (err) {
expect(err.message).to.equal("content type not allowed");
}
});

it("Throw error when the event does not follow the spec 0.3", () => {
Expand All @@ -217,10 +213,11 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
const un = new Unmarshaller();

// act and assert
un.unmarshall(payload, attributes)
.then(() => { throw new Error("failed"); })
.catch((err) =>
expect(err.message).to.not.empty);
try {
un.unmarshall(payload, attributes);
} catch (err) {
expect(err.message).to.equal("header 'ce-specversion' not found");
}
});

it("No error when all attributes are in place", () => {
Expand All @@ -241,8 +238,8 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
const un = new Unmarshaller();

// act and assert
un.unmarshall(payload, attributes)
.then((actual) => expect(actual).to.be.an("object"));
const event = un.unmarshall(payload, attributes);
expect(event instanceof CloudEvent).to.equal(true);
});

it("Throw error when 'ce-datacontentencoding' is not allowed", () => {
Expand All @@ -263,11 +260,11 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
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");
});
try {
un.unmarshall(payload, attributes);
} catch (err) {
expect(err.message).to.equal("unsupported datacontentencoding");
}
});

it("No error when 'ce-datacontentencoding' is base64", () => {
Expand All @@ -291,12 +288,8 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
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;
});
const event = un.unmarshall(payload, attributes);
expect(event.getData()).to.deep.equal(expected);
});
});
});