Support for data_base64 when event data is Binary
Signed-off-by: Fabio José <fabiojose@gmail.com>
This commit is contained in:
parent
16d9bfb7e3
commit
1f8940c8a7
|
@ -9,7 +9,8 @@ const {
|
|||
isInteger,
|
||||
isString,
|
||||
isDate,
|
||||
isBinary
|
||||
isBinary,
|
||||
clone
|
||||
} = require("../utils/fun.js");
|
||||
|
||||
const isValidType = (v) =>
|
||||
|
@ -77,6 +78,32 @@ function Spec1(_caller) {
|
|||
this.caller.prototype.getSubject = function(){
|
||||
return this.spec.getSubject();
|
||||
};
|
||||
|
||||
// format() method override
|
||||
this.caller.prototype.format = function(){
|
||||
// Check the constraints
|
||||
this.spec.check();
|
||||
|
||||
// Check before getData() call
|
||||
let isbin = isBinary(this.spec.payload[RESERVED_ATTRIBUTES.data]);
|
||||
|
||||
// May be used, if isbin==true
|
||||
let payload = clone(this.spec.payload);
|
||||
|
||||
// To run asData()
|
||||
this.getData();
|
||||
|
||||
// Handle when is binary, creating the data_base64
|
||||
if(isbin) {
|
||||
payload[RESERVED_ATTRIBUTES.data_base64] = this.spec.payload[RESERVED_ATTRIBUTES.data];
|
||||
delete payload[RESERVED_ATTRIBUTES.data];
|
||||
|
||||
return this.formatter.format(payload);
|
||||
}
|
||||
|
||||
// Then, format
|
||||
return this.formatter.format(this.spec.payload);
|
||||
};
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
|
@ -2,6 +2,7 @@ const expect = require("chai").expect;
|
|||
const nock = require("nock");
|
||||
const http = require("http");
|
||||
const request = require("request");
|
||||
const {asBase64} = require("../lib/utils/fun.js");
|
||||
|
||||
const BinaryHTTPEmitter =
|
||||
require("../lib/bindings/http/emitter_binary_1.js");
|
||||
|
@ -38,6 +39,8 @@ const cloudevent =
|
|||
.addExtension(ext1Name, ext1Value)
|
||||
.addExtension(ext2Name, ext2Value);
|
||||
|
||||
const dataString = ")(*~^my data for ce#@#$%"
|
||||
|
||||
const webhook = "https://cloudevents.io/webhook/v1";
|
||||
const httpcfg = {
|
||||
method : "POST",
|
||||
|
@ -45,6 +48,7 @@ const httpcfg = {
|
|||
};
|
||||
|
||||
const binary = new BinaryHTTPEmitter(httpcfg);
|
||||
const structured = new v1.StructuredHTTPEmitter(httpcfg);
|
||||
|
||||
describe("HTTP Transport Binding - Version 1.0", () => {
|
||||
beforeEach(() => {
|
||||
|
@ -54,6 +58,65 @@ describe("HTTP Transport Binding - Version 1.0", () => {
|
|||
.reply(201, {status: "accepted"});
|
||||
});
|
||||
|
||||
describe("Structured", () => {
|
||||
describe("JSON Format", () => {
|
||||
it("requires '" + contentType + "' Content-Type in the header", () => {
|
||||
return structured.emit(cloudevent)
|
||||
.then((response) => {
|
||||
expect(response.config.headers["Content-Type"])
|
||||
.to.equal(contentType);
|
||||
});
|
||||
});
|
||||
|
||||
it("the request payload should be correct", () => {
|
||||
return structured.emit(cloudevent)
|
||||
.then((response) => {
|
||||
expect(JSON.parse(response.config.data))
|
||||
.to.deep.equal(cloudevent.format());
|
||||
});
|
||||
});
|
||||
|
||||
describe("Binary event data", () => {
|
||||
it("the request payload should be correct when data is binary", () => {
|
||||
let bindata = Uint32Array.from(dataString, (c) => c.codePointAt(0));
|
||||
let expected = asBase64(bindata);
|
||||
let binevent =
|
||||
new Cloudevent(v1.Spec)
|
||||
.type(type)
|
||||
.source(source)
|
||||
.dataContentType("text/plain")
|
||||
.data(bindata)
|
||||
.addExtension(ext1Name, ext1Value)
|
||||
.addExtension(ext2Name, ext2Value);
|
||||
|
||||
return structured.emit(binevent)
|
||||
.then((response) => {
|
||||
expect(JSON.parse(response.config.data).data_base64)
|
||||
.to.equal(expected);
|
||||
});
|
||||
});
|
||||
|
||||
it("the payload must have 'data_base64' when data is binary", () => {
|
||||
let binevent =
|
||||
new Cloudevent(v1.Spec)
|
||||
.type(type)
|
||||
.source(source)
|
||||
.dataContentType("text/plain")
|
||||
.data(Uint32Array.from(dataString, (c) => c.codePointAt(0)))
|
||||
.addExtension(ext1Name, ext1Value)
|
||||
.addExtension(ext2Name, ext2Value);
|
||||
|
||||
return structured.emit(binevent)
|
||||
.then((response) => {
|
||||
console.log(response.config.data);
|
||||
expect(JSON.parse(response.config.data))
|
||||
.to.have.property("data_base64");
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("Binary", () => {
|
||||
describe("JSON Format", () => {
|
||||
it("requires '" + cloudevent.getDataContentType() + "' Content-Type in the header", () => {
|
||||
|
|
|
@ -2,6 +2,7 @@ const expect = require("chai").expect;
|
|||
const Spec1 = require("../lib/specs/spec_1.js");
|
||||
const Cloudevent = require("../index.js");
|
||||
const uuid = require("uuid/v4");
|
||||
const {asBase64} = require("../lib/utils/fun.js");
|
||||
|
||||
const id = uuid();
|
||||
const type = "com.github.pull.create";
|
||||
|
@ -227,12 +228,13 @@ describe("CloudEvents Spec v1.0", () => {
|
|||
it("should be ok when type is 'Uint32Array' for 'Binary'", () => {
|
||||
let dataString = ")(*~^my data for ce#@#$%"
|
||||
|
||||
let expected = Uint32Array.from(dataString, (c) => c.codePointAt(0));;
|
||||
let dataBinary = Uint32Array.from(dataString, (c) => c.codePointAt(0));
|
||||
let expected = asBase64(dataBinary);
|
||||
let olddct = cloudevent.getDataContentType();
|
||||
|
||||
cloudevent
|
||||
.dataContentType("text/plain")
|
||||
.data(expected);
|
||||
.data(dataBinary);
|
||||
expect(cloudevent.getData()).to.deep.equal(expected);
|
||||
|
||||
cloudevent.dataContentType(olddct);
|
||||
|
|
Loading…
Reference in New Issue