Skip to content

Commit 49b8ef0

Browse files
authored
Merge pull request #26 from pengsrc/master
Support receiving "application/octet-stream" content in binary transport mode
2 parents 0a56284 + 8418f77 commit 49b8ef0

File tree

4 files changed

+53
-5
lines changed

4 files changed

+53
-5
lines changed

lib/bindings/http/constants.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
module.exports = {
33
CHARSET_DEFAULT : "utf-8",
44

5-
MIME_JSON : "application/json",
6-
MIME_CE : "application/cloudevents",
7-
MIME_CE_JSON : "application/cloudevents+json",
5+
MIME_JSON : "application/json",
6+
MIME_OCTET_STREAM : "application/octet-stream",
7+
MIME_CE : "application/cloudevents",
8+
MIME_CE_JSON : "application/cloudevents+json",
89

910
HEADER_CONTENT_TYPE : "content-type",
1011

lib/bindings/http/receiver_binary_0_2.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ const {
1212

1313
const parserByType = {};
1414
parserByType[Constants.MIME_JSON] = new JSONParser();
15+
parserByType[Constants.MIME_OCTET_STREAM] = {
16+
parse(payload) { return payload; }
17+
};
1518

1619
const allowedContentTypes = [];
1720
allowedContentTypes.push(Constants.MIME_JSON);
21+
allowedContentTypes.push(Constants.MIME_OCTET_STREAM);
1822

1923
const requiredHeaders = [];
2024
requiredHeaders.push(Constants.BINARY_HEADERS_02.TYPE);

lib/bindings/http/unmarshaller_0_2.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const receiverByBinding = {
1414

1515
const allowedBinaryContentTypes = [];
1616
allowedBinaryContentTypes.push(Constants.MIME_JSON);
17+
allowedBinaryContentTypes.push(Constants.MIME_OCTET_STREAM);
1718

1819
const allowedStructuredContentTypes = [];
1920
allowedStructuredContentTypes.push(Constants.MIME_CE_JSON);

test/bindings/http/receiver_binary_0_2_tests.js

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.2", () => {
285285
.to.equal("http://schema.registry/v1");
286286
});
287287

288-
it("Cloudevent contains 'contenttype'", () => {
288+
it("Cloudevent contains 'contenttype' (application/json)", () => {
289289
// setup
290290
var payload = {
291291
"data" : "dataString"
@@ -308,7 +308,28 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.2", () => {
308308
.to.equal("application/json");
309309
});
310310

311-
it("Cloudevent contains 'data'", () => {
311+
it("Cloudevent contains 'contenttype' (application/octet-stream)", () => {
312+
// setup
313+
var payload = "The payload is binary data";
314+
var attributes = {
315+
"ce-type" : "type",
316+
"ce-specversion" : "0.2",
317+
"ce-source" : "/source",
318+
"ce-id" : "id",
319+
"ce-time" : "2019-06-16T11:42:00Z",
320+
"ce-schemaurl" : "http://schema.registry/v1",
321+
"Content-Type" : "application/octet-stream"
322+
};
323+
324+
// act
325+
var actual = receiver.parse(payload, attributes);
326+
327+
// assert
328+
expect(actual.getContenttype())
329+
.to.equal("application/json");
330+
});
331+
332+
it("Cloudevent contains 'data' (application/json)", () => {
312333
// setup
313334
var payload = {
314335
"data" : "dataString"
@@ -331,6 +352,27 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.2", () => {
331352
.to.deep.equal(payload);
332353
});
333354

355+
it("Cloudevent contains 'data' (application/octet-stream)", () => {
356+
// setup
357+
var payload = "The payload is binary data";
358+
var attributes = {
359+
"ce-type" : "type",
360+
"ce-specversion" : "0.2",
361+
"ce-source" : "/source",
362+
"ce-id" : "id",
363+
"ce-time" : "2019-06-16T11:42:00Z",
364+
"ce-schemaurl" : "http://schema.registry/v1",
365+
"Content-Type" : "application/octet-stream"
366+
};
367+
368+
// act
369+
var actual = receiver.parse(payload, attributes);
370+
371+
// assert
372+
expect(actual.getData())
373+
.to.deep.equal(payload);
374+
});
375+
334376
it("No error when all attributes are in place", () => {
335377
// setup
336378
var payload = {

0 commit comments

Comments
 (0)