Skip to content

Commit a4a8520

Browse files
committed
test: change tests to expect ValidationError
Signed-off-by: Lance Ball <[email protected]>
1 parent 1071843 commit a4a8520

14 files changed

+115
-152
lines changed

lib/bindings/http/http_receiver.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const V03Binary = require("./receiver_binary_0_3.js");
22
const V03Structured = require("./receiver_structured_0_3.js");
33
const V1Binary = require("./receiver_binary_1.js");
44
const V1Structured = require("./receiver_structured_1.js");
5+
const ValidationError = require("../../validation_error.js");
56
const {
67
SPEC_V03,
78
SPEC_V1,
@@ -63,7 +64,7 @@ function getMode(headers) {
6364
if (headers[BINARY_HEADERS_1.ID]) {
6465
return "binary";
6566
}
66-
throw new TypeError("no cloud event detected");
67+
throw new ValidationError("no cloud event detected");
6768
}
6869

6970
function getVersion(mode, headers, body) {

lib/bindings/http/receiver_binary.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,12 @@ const {
1111

1212
function validateArgs(payload, attributes) {
1313
Array.of(payload)
14-
.filter((p) => isDefinedOrThrow(p,
15-
{ message: "payload is null or undefined" }))
16-
.filter((p) => isStringOrObjectOrThrow(p,
17-
{ message: "payload must be an object or a string" }))
14+
.filter((p) => isDefinedOrThrow(p, new ValidationError("payload is null or undefined")))
15+
.filter((p) => isStringOrObjectOrThrow(p, new ValidationError("payload must be an object or a string")))
1816
.shift();
1917

2018
Array.of(attributes)
21-
.filter((a) => isDefinedOrThrow(a,
22-
{ message: "attributes is null or undefined" }))
19+
.filter((a) => isDefinedOrThrow(a, new ValidationError("attributes is null or undefined")))
2320
.shift();
2421
}
2522

lib/bindings/http/receiver_structured.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,12 @@ const {
1010

1111
function validateArgs(payload, attributes) {
1212
Array.of(payload)
13-
.filter((p) => isDefinedOrThrow(p,
14-
{ message: "payload is null or undefined" }))
15-
.filter((p) => isStringOrObjectOrThrow(p,
16-
{ message: "payload must be an object or string" }))
13+
.filter((p) => isDefinedOrThrow(p, new ValidationError("payload is null or undefined")))
14+
.filter((p) => isStringOrObjectOrThrow(p, new ValidationError("payload must be an object or string")))
1715
.shift();
1816

1917
Array.of(attributes)
20-
.filter((a) => isDefinedOrThrow(a,
21-
{ message: "attributes is null or undefined" }))
18+
.filter((a) => isDefinedOrThrow(a, new ValidationError("attributes is null or undefined")))
2219
.shift();
2320
}
2421

lib/bindings/http/unmarshaller.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const Constants = require("./constants.js");
22
const Commons = require("./commons.js");
3+
const ValidationError = require("../../validation_error.js");
34

45
const STRUCTURED = "structured";
56
const BINARY = "binary";
@@ -23,18 +24,18 @@ function resolveBindingName(payload, headers) {
2324
if (allowedStructuredContentTypes.includes(contentType)) {
2425
return STRUCTURED;
2526
}
26-
throwTypeError("structured+type not allowed", contentType);
27+
throwValidationError("structured+type not allowed", contentType);
2728
} else {
2829
// Binary
2930
if (allowedBinaryContentTypes.includes(contentType)) {
3031
return BINARY;
3132
}
32-
throwTypeError("content type not allowed", contentType);
33+
throwValidationError("content type not allowed", contentType);
3334
}
3435
}
3536

36-
function throwTypeError(msg, contentType) {
37-
const err = new TypeError(msg);
37+
function throwValidationError(msg, contentType) {
38+
const err = new ValidationError(msg);
3839
err.errors = [contentType];
3940
throw err;
4041
}
@@ -46,16 +47,16 @@ class Unmarshaller {
4647

4748
unmarshall(payload, headers) {
4849
if (!payload) {
49-
throw new TypeError("payload is null or undefined");
50+
throw new ValidationError("payload is null or undefined");
5051
}
5152
if (!headers) {
52-
throw new TypeError("headers is null or undefined");
53+
throw new ValidationError("headers is null or undefined");
5354
}
5455

5556
// Validation level 1
5657
const sanityHeaders = Commons.sanityAndClone(headers);
5758
if (!sanityHeaders[Constants.HEADER_CONTENT_TYPE]) {
58-
throw new TypeError("content-type header not found");
59+
throw new ValidationError("content-type header not found");
5960
}
6061

6162
// Resolve the binding

lib/formats/json/parser.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ const {
33
isDefinedOrThrow,
44
isStringOrObjectOrThrow
55
} = require("../../utils/fun.js");
6+
const ValidationError = require("../../validation_error.js");
67

7-
const invalidPayloadTypeError =
8-
new Error("invalid payload type, allowed are: string or object");
9-
const nullOrUndefinedPayload =
10-
new Error("null or undefined payload");
8+
const invalidPayloadTypeError = new ValidationError("invalid payload type, allowed are: string or object");
9+
const nullOrUndefinedPayload = new ValidationError("null or undefined payload");
1110

1211
const asJSON = (v) => (isString(v) ? JSON.parse(v) : v);
1312

test/bindings/http/promiscuous_receiver_test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const {
88
BINARY_HEADERS_03,
99
BINARY_HEADERS_1
1010
} = require("../../../lib/bindings/http/constants.js");
11+
const ValidationError = require("../../../lib/validation_error.js");
1112

1213
const receiver = new HTTPReceiver();
1314
const id = "1234";
@@ -30,7 +31,7 @@ describe("HTTP Transport Binding Receiver for CloudEvents", () => {
3031
};
3132

3233
expect(receiver.accept.bind(receiver, {}, payload))
33-
.to.throw("no cloud event detected");
34+
.to.throw(TypeError, "no cloud event detected");
3435
});
3536
});
3637

test/bindings/http/receiver_binary_0_3_tests.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const expect = require("chai").expect;
22

3-
const HTTPBinaryReceiver =
4-
require("../../../lib/bindings/http/receiver_binary_0_3.js");
3+
const HTTPBinaryReceiver = require("../../../lib/bindings/http/receiver_binary_0_3.js");
4+
const ValidationError = require("../../../lib/validation_error.js");
55
const {
66
BINARY_HEADERS_03,
77
SPEC_V03,
@@ -19,7 +19,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.3", () => {
1919

2020
// act and assert
2121
expect(receiver.check.bind(receiver, payload, attributes))
22-
.to.throw("payload is null or undefined");
22+
.to.throw(ValidationError, "payload is null or undefined");
2323
});
2424

2525
it("Throw error when attributes arg is null or undefined", () => {
@@ -29,7 +29,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.3", () => {
2929

3030
// act and assert
3131
expect(receiver.check.bind(receiver, payload, attributes))
32-
.to.throw("attributes is null or undefined");
32+
.to.throw(ValidationError, "attributes is null or undefined");
3333
});
3434

3535
it("Throw error when payload is not an object or string", () => {
@@ -39,7 +39,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.3", () => {
3939

4040
// act and assert
4141
expect(receiver.check.bind(receiver, payload, attributes))
42-
.to.throw("payload must be an object or a string");
42+
.to.throw(ValidationError, "payload must be an object or a string");
4343
});
4444

4545
it("Throw error when headers has no 'ce-type'", () => {
@@ -54,7 +54,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.3", () => {
5454

5555
// act and assert
5656
expect(receiver.check.bind(receiver, payload, attributes))
57-
.to.throw("header 'ce-type' not found");
57+
.to.throw(ValidationError, "header 'ce-type' not found");
5858
});
5959

6060
it("Throw error when headers has no 'ce-specversion'", () => {
@@ -69,7 +69,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.3", () => {
6969

7070
// act and assert
7171
expect(receiver.check.bind(receiver, payload, attributes))
72-
.to.throw("header 'ce-specversion' not found");
72+
.to.throw(ValidationError, "header 'ce-specversion' not found");
7373
});
7474

7575
it("Throw error when headers has no 'ce-source'", () => {
@@ -84,7 +84,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.3", () => {
8484

8585
// act and assert
8686
expect(receiver.check.bind(receiver, payload, attributes))
87-
.to.throw("header 'ce-source' not found");
87+
.to.throw(ValidationError, "header 'ce-source' not found");
8888
});
8989

9090
it("Throw error when headers has no 'ce-id'", () => {
@@ -99,7 +99,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.3", () => {
9999

100100
// act and assert
101101
expect(receiver.check.bind(receiver, payload, attributes))
102-
.to.throw("header 'ce-id' not found");
102+
.to.throw(ValidationError, "header 'ce-id' not found");
103103
});
104104

105105
it("Throw error when spec is not 0.3", () => {
@@ -115,7 +115,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.3", () => {
115115

116116
// act and assert
117117
expect(receiver.parse.bind(receiver, payload, attributes))
118-
.to.throw("invalid spec version");
118+
.to.throw(ValidationError, "invalid spec version");
119119
});
120120

121121
it("Throw error when the content-type is invalid", () => {
@@ -131,7 +131,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.3", () => {
131131

132132
// act and assert
133133
expect(receiver.check.bind(receiver, payload, attributes))
134-
.to.throw("invalid content type");
134+
.to.throw(ValidationError, "invalid content type");
135135
});
136136

137137
it("No error when all required headers are in place", () => {

0 commit comments

Comments
 (0)