Skip to content

Commit 6a19794

Browse files
committed
Support for binary format
Signed-off-by: Fabio José <[email protected]>
1 parent 8e0371b commit 6a19794

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

lib/bindings/http/receiver_binary_1.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ const BinaryHTTPReceiver = require("./receiver_binary.js");
88

99
const {
1010
isDefinedOrThrow,
11-
isStringOrObjectOrThrow
11+
isStringOrObjectOrThrow,
12+
isString,
13+
isBase64
1214
} = require("../../utils/fun.js");
1315

1416
const parserByType = {};
@@ -100,6 +102,10 @@ Receiver.prototype.parse = function(payload, headers) {
100102
// firstly specific local checks
101103
this.check(payload, headers);
102104

105+
payload = isString(payload) && isBase64(payload)
106+
? Buffer.from(payload, "base64").toString()
107+
: payload;
108+
103109
return this.receiver.parse(payload, headers);
104110
};
105111

test/bindings/http/receiver_binary_1_tests.js

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
var expect = require("chai").expect;
1+
const expect = require("chai").expect;
2+
const {asBase64} = require("../../../lib/utils/fun.js");
23

3-
var HTTPBinaryReceiver =
4+
const HTTPBinaryReceiver =
45
require("../../../lib/bindings/http/receiver_binary_1.js");
56

6-
var receiver = new HTTPBinaryReceiver();
7+
const receiver = new HTTPBinaryReceiver();
78

89
describe("HTTP Transport Binding Binary Receiver for CloudEvents v1.0", () => {
910
describe("Check", () => {
@@ -262,7 +263,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v1.0", () => {
262263
.to.equal("2019-06-16T11:42:00.000Z");
263264
});
264265

265-
it("Cloudevent contains 'schemaurl'", () => {
266+
it("Cloudevent contains 'dataschema'", () => {
266267
// setup
267268
var payload = {
268269
"data" : "dataString"
@@ -373,6 +374,33 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v1.0", () => {
373374
.to.deep.equal(payload);
374375
});
375376

377+
it("The content of 'data' is base64 for binary", () => {
378+
// setup
379+
var expected = {
380+
"data" : "dataString"
381+
};
382+
383+
let bindata = Uint32Array.from(JSON.stringify(expected), (c) => c.codePointAt(0));
384+
let payload = asBase64(bindata);
385+
386+
var attributes = {
387+
"ce-type" : "type",
388+
"ce-specversion" : "1.0",
389+
"ce-source" : "/source",
390+
"ce-id" : "id",
391+
"ce-time" : "2019-06-16T11:42:00Z",
392+
"ce-dataschema" : "http://schema.registry/v1",
393+
"Content-Type" : "application/json"
394+
};
395+
396+
// act
397+
var actual = receiver.parse(payload, attributes);
398+
399+
// assert
400+
expect(actual.getData())
401+
.to.deep.equal(expected);
402+
});
403+
376404
it("No error when all attributes are in place", () => {
377405
// setup
378406
var payload = {

0 commit comments

Comments
 (0)