Skip to content

Commit b4c3846

Browse files
committed
Unmarshaller impl and tests
Signed-off-by: Fabio José <[email protected]>
1 parent 064b022 commit b4c3846

File tree

2 files changed

+89
-19
lines changed

2 files changed

+89
-19
lines changed

lib/bindings/http/unmarshaller_0_2.js

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
var Spec02 = require("../../specs/spec_0_2.js");
2-
var JSONParser = require("../../formats/json/parser.js");
1+
var StructuredReceiver = require("./receiver_structured_0_2.js");
2+
var BinaryReceiver = require("./receiver_binary_0_2.js");
33

44
const Constants = require("./constants.js");
5+
const Commons = require("./commons.js");
56

67
const structured = "structured";
78
const binary = "binary";
89

9-
const jsonParserSpec02 = new JSONParser(new Spec02());
10-
const parsers = {};
11-
parsers[Constants.MIME_JSON] = jsonParserSpec02;
12-
parsers[Constants.MIME_CE_JSON] = jsonParserSpec02;
10+
const receiver_by_binding = {
11+
structured : new StructuredReceiver(),
12+
binary : new BinaryReceiver(),
13+
};
1314

1415
const allowed_binary_content_types = [];
1516
allowed_binary_content_types.push(Constants.MIME_JSON);
@@ -25,10 +26,6 @@ function validate_args(payload, headers) {
2526
if(!headers){
2627
throw {message: "headers is null or undefined"};
2728
}
28-
29-
if(!headers[Constants.HEADER_CONTENT_TYPE]){
30-
throw {message: "content-type header not found"};
31-
}
3229
}
3330

3431
// Is it binary or structured?
@@ -59,19 +56,20 @@ var Unmarshaller = function() {
5956
Unmarshaller.prototype.unmarshall = function(payload, headers) {
6057
validate_args(payload, headers);
6158

62-
// Resolve the binding
63-
var bindingName = resolve_binding_name(payload, headers);
64-
var contentType = headers[Constants.HEADER_CONTENT_TYPE];
59+
var sanity_headers = Commons.sanity_and_clone(headers);
6560

66-
if(bindingName === structured){
67-
var parser = parsers[contentType];
68-
var cloudevent = parser.parse(payload);
61+
// Validation level 1
62+
if(!sanity_headers[Constants.HEADER_CONTENT_TYPE]){
63+
throw {message: "content-type header not found"};
64+
}
6965

70-
return cloudevent;
71-
} else {//binary
66+
// Resolve the binding
67+
var binding_name = resolve_binding_name(payload, sanity_headers);
7268

73-
}
69+
var cloudevent =
70+
receiver_by_binding[binding_name].parse(payload, sanity_headers);
7471

72+
return cloudevent;
7573
}
7674

7775
module.exports = Unmarshaller;

test/bindings/http/unmarshaller_0_2_tests.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,76 @@ describe("HTTP Transport Binding Unmarshaller", () => {
125125
.to.be.an("object");
126126
});
127127
});
128+
129+
describe("Binary", () => {
130+
it("Throw error when has not allowed mime", () => {
131+
// setup
132+
var payload = {
133+
"data" : "dataString"
134+
};
135+
var attributes = {
136+
"ce-type" : "type",
137+
"ce-specversion" : "0.2",
138+
"ce-source" : "source",
139+
"ce-id" : "id",
140+
"ce-time" : "2019-06-16T11:42:00Z",
141+
"ce-schemaurl" : "http://schema.registry/v1",
142+
"Content-Type" : "text/html"
143+
};
144+
145+
var un = new Unmarshaller();
146+
147+
// act and assert
148+
expect(un.unmarshall.bind(un, payload, attributes))
149+
.to.throw("content type not allowed");
150+
});
151+
152+
it("Throw error when the event does not follow the spec 0.2", () => {
153+
// setup
154+
var payload = {
155+
"data" : "dataString"
156+
};
157+
var attributes = {
158+
"ce-type" : "type",
159+
"CE-CloudEventsVersion" : "0.1",
160+
"ce-source" : "source",
161+
"ce-id" : "id",
162+
"ce-time" : "2019-06-16T11:42:00Z",
163+
"ce-schemaurl" : "http://schema.registry/v1",
164+
"Content-Type" : "application/json"
165+
};
166+
167+
var un = new Unmarshaller();
168+
169+
// act and assert
170+
expect(un.unmarshall.bind(un, payload, attributes))
171+
.to.throw();
172+
});
173+
174+
it("No error when all attributes are in place", () => {
175+
// setup
176+
var payload = {
177+
"data" : "dataString"
178+
};
179+
var attributes = {
180+
"ce-type" : "type",
181+
"ce-specversion" : "0.2",
182+
"ce-source" : "source",
183+
"ce-id" : "id",
184+
"ce-time" : "2019-06-16T11:42:00Z",
185+
"ce-schemaurl" : "http://schema.registry/v1",
186+
"Content-Type" : "application/json"
187+
};
188+
189+
var un = new Unmarshaller();
190+
191+
// act
192+
var actual = un.unmarshall(payload, attributes);
193+
194+
// assert
195+
expect(actual)
196+
.to.be.an("object");
197+
198+
});
199+
});
128200
});

0 commit comments

Comments
 (0)