Skip to content

Commit 1f8940c

Browse files
committed
Support for data_base64 when event data is Binary
Signed-off-by: Fabio José <[email protected]>
1 parent 16d9bfb commit 1f8940c

File tree

3 files changed

+95
-3
lines changed

3 files changed

+95
-3
lines changed

lib/specs/spec_1.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ const {
99
isInteger,
1010
isString,
1111
isDate,
12-
isBinary
12+
isBinary,
13+
clone
1314
} = require("../utils/fun.js");
1415

1516
const isValidType = (v) =>
@@ -77,6 +78,32 @@ function Spec1(_caller) {
7778
this.caller.prototype.getSubject = function(){
7879
return this.spec.getSubject();
7980
};
81+
82+
// format() method override
83+
this.caller.prototype.format = function(){
84+
// Check the constraints
85+
this.spec.check();
86+
87+
// Check before getData() call
88+
let isbin = isBinary(this.spec.payload[RESERVED_ATTRIBUTES.data]);
89+
90+
// May be used, if isbin==true
91+
let payload = clone(this.spec.payload);
92+
93+
// To run asData()
94+
this.getData();
95+
96+
// Handle when is binary, creating the data_base64
97+
if(isbin) {
98+
payload[RESERVED_ATTRIBUTES.data_base64] = this.spec.payload[RESERVED_ATTRIBUTES.data];
99+
delete payload[RESERVED_ATTRIBUTES.data];
100+
101+
return this.formatter.format(payload);
102+
}
103+
104+
// Then, format
105+
return this.formatter.format(this.spec.payload);
106+
};
80107
}
81108

82109
/*

test/http_binding_1.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const expect = require("chai").expect;
22
const nock = require("nock");
33
const http = require("http");
44
const request = require("request");
5+
const {asBase64} = require("../lib/utils/fun.js");
56

67
const BinaryHTTPEmitter =
78
require("../lib/bindings/http/emitter_binary_1.js");
@@ -38,13 +39,16 @@ const cloudevent =
3839
.addExtension(ext1Name, ext1Value)
3940
.addExtension(ext2Name, ext2Value);
4041

42+
const dataString = ")(*~^my data for ce#@#$%"
43+
4144
const webhook = "https://cloudevents.io/webhook/v1";
4245
const httpcfg = {
4346
method : "POST",
4447
url : webhook + "/json"
4548
};
4649

4750
const binary = new BinaryHTTPEmitter(httpcfg);
51+
const structured = new v1.StructuredHTTPEmitter(httpcfg);
4852

4953
describe("HTTP Transport Binding - Version 1.0", () => {
5054
beforeEach(() => {
@@ -54,6 +58,65 @@ describe("HTTP Transport Binding - Version 1.0", () => {
5458
.reply(201, {status: "accepted"});
5559
});
5660

61+
describe("Structured", () => {
62+
describe("JSON Format", () => {
63+
it("requires '" + contentType + "' Content-Type in the header", () => {
64+
return structured.emit(cloudevent)
65+
.then((response) => {
66+
expect(response.config.headers["Content-Type"])
67+
.to.equal(contentType);
68+
});
69+
});
70+
71+
it("the request payload should be correct", () => {
72+
return structured.emit(cloudevent)
73+
.then((response) => {
74+
expect(JSON.parse(response.config.data))
75+
.to.deep.equal(cloudevent.format());
76+
});
77+
});
78+
79+
describe("Binary event data", () => {
80+
it("the request payload should be correct when data is binary", () => {
81+
let bindata = Uint32Array.from(dataString, (c) => c.codePointAt(0));
82+
let expected = asBase64(bindata);
83+
let binevent =
84+
new Cloudevent(v1.Spec)
85+
.type(type)
86+
.source(source)
87+
.dataContentType("text/plain")
88+
.data(bindata)
89+
.addExtension(ext1Name, ext1Value)
90+
.addExtension(ext2Name, ext2Value);
91+
92+
return structured.emit(binevent)
93+
.then((response) => {
94+
expect(JSON.parse(response.config.data).data_base64)
95+
.to.equal(expected);
96+
});
97+
});
98+
99+
it("the payload must have 'data_base64' when data is binary", () => {
100+
let binevent =
101+
new Cloudevent(v1.Spec)
102+
.type(type)
103+
.source(source)
104+
.dataContentType("text/plain")
105+
.data(Uint32Array.from(dataString, (c) => c.codePointAt(0)))
106+
.addExtension(ext1Name, ext1Value)
107+
.addExtension(ext2Name, ext2Value);
108+
109+
return structured.emit(binevent)
110+
.then((response) => {
111+
console.log(response.config.data);
112+
expect(JSON.parse(response.config.data))
113+
.to.have.property("data_base64");
114+
});
115+
});
116+
});
117+
});
118+
});
119+
57120
describe("Binary", () => {
58121
describe("JSON Format", () => {
59122
it("requires '" + cloudevent.getDataContentType() + "' Content-Type in the header", () => {

test/spec_1_tests.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const expect = require("chai").expect;
22
const Spec1 = require("../lib/specs/spec_1.js");
33
const Cloudevent = require("../index.js");
44
const uuid = require("uuid/v4");
5+
const {asBase64} = require("../lib/utils/fun.js");
56

67
const id = uuid();
78
const type = "com.github.pull.create";
@@ -227,12 +228,13 @@ describe("CloudEvents Spec v1.0", () => {
227228
it("should be ok when type is 'Uint32Array' for 'Binary'", () => {
228229
let dataString = ")(*~^my data for ce#@#$%"
229230

230-
let expected = Uint32Array.from(dataString, (c) => c.codePointAt(0));;
231+
let dataBinary = Uint32Array.from(dataString, (c) => c.codePointAt(0));
232+
let expected = asBase64(dataBinary);
231233
let olddct = cloudevent.getDataContentType();
232234

233235
cloudevent
234236
.dataContentType("text/plain")
235-
.data(expected);
237+
.data(dataBinary);
236238
expect(cloudevent.getData()).to.deep.equal(expected);
237239

238240
cloudevent.dataContentType(olddct);

0 commit comments

Comments
 (0)