Support receiving "application/octet-stream" content in binary transport mode

Signed-off-by: Jingwen Peng <pengsrc@outlook.com>
This commit is contained in:
Jingwen Peng 2019-07-28 14:57:41 +08:00
parent 0a56284df2
commit 8418f77a6b
4 changed files with 53 additions and 5 deletions

View File

@ -2,9 +2,10 @@
module.exports = {
CHARSET_DEFAULT : "utf-8",
MIME_JSON : "application/json",
MIME_CE : "application/cloudevents",
MIME_CE_JSON : "application/cloudevents+json",
MIME_JSON : "application/json",
MIME_OCTET_STREAM : "application/octet-stream",
MIME_CE : "application/cloudevents",
MIME_CE_JSON : "application/cloudevents+json",
HEADER_CONTENT_TYPE : "content-type",

View File

@ -12,9 +12,13 @@ const {
const parserByType = {};
parserByType[Constants.MIME_JSON] = new JSONParser();
parserByType[Constants.MIME_OCTET_STREAM] = {
parse(payload) { return payload; }
};
const allowedContentTypes = [];
allowedContentTypes.push(Constants.MIME_JSON);
allowedContentTypes.push(Constants.MIME_OCTET_STREAM);
const requiredHeaders = [];
requiredHeaders.push(Constants.BINARY_HEADERS_02.TYPE);

View File

@ -14,6 +14,7 @@ const receiverByBinding = {
const allowedBinaryContentTypes = [];
allowedBinaryContentTypes.push(Constants.MIME_JSON);
allowedBinaryContentTypes.push(Constants.MIME_OCTET_STREAM);
const allowedStructuredContentTypes = [];
allowedStructuredContentTypes.push(Constants.MIME_CE_JSON);

View File

@ -285,7 +285,7 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.2", () => {
.to.equal("http://schema.registry/v1");
});
it("Cloudevent contains 'contenttype'", () => {
it("Cloudevent contains 'contenttype' (application/json)", () => {
// setup
var payload = {
"data" : "dataString"
@ -308,7 +308,28 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.2", () => {
.to.equal("application/json");
});
it("Cloudevent contains 'data'", () => {
it("Cloudevent contains 'contenttype' (application/octet-stream)", () => {
// setup
var payload = "The payload is binary data";
var attributes = {
"ce-type" : "type",
"ce-specversion" : "0.2",
"ce-source" : "/source",
"ce-id" : "id",
"ce-time" : "2019-06-16T11:42:00Z",
"ce-schemaurl" : "http://schema.registry/v1",
"Content-Type" : "application/octet-stream"
};
// act
var actual = receiver.parse(payload, attributes);
// assert
expect(actual.getContenttype())
.to.equal("application/json");
});
it("Cloudevent contains 'data' (application/json)", () => {
// setup
var payload = {
"data" : "dataString"
@ -331,6 +352,27 @@ describe("HTTP Transport Binding Binary Receiver for CloudEvents v0.2", () => {
.to.deep.equal(payload);
});
it("Cloudevent contains 'data' (application/octet-stream)", () => {
// setup
var payload = "The payload is binary data";
var attributes = {
"ce-type" : "type",
"ce-specversion" : "0.2",
"ce-source" : "/source",
"ce-id" : "id",
"ce-time" : "2019-06-16T11:42:00Z",
"ce-schemaurl" : "http://schema.registry/v1",
"Content-Type" : "application/octet-stream"
};
// act
var actual = receiver.parse(payload, attributes);
// assert
expect(actual.getData())
.to.deep.equal(payload);
});
it("No error when all attributes are in place", () => {
// setup
var payload = {