Content type

Signed-off-by: Fabio José <fabiojose@gmail.com>
This commit is contained in:
Fabio José 2019-06-16 17:50:26 -03:00
parent 14e0583347
commit 21011b5a74
2 changed files with 71 additions and 18 deletions

View File

@ -2,6 +2,9 @@ const Constants = require("./constants.js");
const Cloudevent = require("../../cloudevent.js");
const Spec02 = require("../../specs/spec_0_2.js");
const allowed_content_types = [];
allowed_content_types.push(Constants.MIME_JSON);
const required_headers = [];
required_headers.push(Constants.BINARY_HEADERS_02.TYPE);
required_headers.push(Constants.BINARY_HEADERS_02.SPEC_VERSION);
@ -44,6 +47,16 @@ function validate_args(payload, attributes) {
}
}
function sanity(headers) {
var sanity_headers = JSON.parse(JSON.stringify(headers));
for(header in sanity_headers){
sanity_headers[header.toLowerCase()] = sanity_headers[header];
}
return sanity_headers;
}
function Receiver(configuration) {
}
@ -52,16 +65,26 @@ Receiver.prototype.check = function(payload, headers) {
// Validation Level 0
validate_args(payload, headers);
// Clone and low case all headers names
var sanity_headers = sanity(headers);
// Validation Level 1
if(!allowed_content_types
.includes(sanity_headers[Constants.HEADER_CONTENT_TYPE])){
throw {message: "invalid content type",
errors: [sanity_headers[Constants.HEADER_CONTENT_TYPE]]};
}
for(i in required_headers){
if(!headers[required_headers[i]]){
if(!sanity_headers[required_headers[i]]){
throw {message: "header '" + required_headers[i] + "' not found"};
}
}
if(headers[Constants.BINARY_HEADERS_02.SPEC_VERSION] !== "0.2"){
if(sanity_headers[Constants.BINARY_HEADERS_02.SPEC_VERSION] !== "0.2"){
throw {message: "invalid spec version",
errors: [headers[Constants.BINARY_HEADERS_02.SPEC_VERSION]]};
errors:
[sanity_headers[Constants.BINARY_HEADERS_02.SPEC_VERSION]]};
}
// No erros! Its contains the minimum required attributes
@ -82,7 +105,7 @@ Receiver.prototype.parse = function(payload, headers) {
}
}
// Sets the data
// Sets the data
cloudevent.data(payload);
// Checks the event spec

View File

@ -43,7 +43,8 @@ describe("HTTP Transport Binding Binary Receiver 0.2", () => {
var attributes = {
"ce-specversion" : "specversion",
"ce-source" : "source",
"ce-id" : "id"
"ce-id" : "id",
"Content-Type" : "application/json"
};
// act and assert
@ -57,7 +58,8 @@ describe("HTTP Transport Binding Binary Receiver 0.2", () => {
var attributes = {
"ce-type" : "type",
"ce-source" : "source",
"ce-id" : "id"
"ce-id" : "id",
"Content-Type" : "application/json"
};
// act and assert
@ -71,7 +73,8 @@ describe("HTTP Transport Binding Binary Receiver 0.2", () => {
var attributes = {
"ce-type" : "type",
"ce-specversion" : "specversion",
"ce-id" : "id"
"ce-id" : "id",
"Content-Type" : "application/json"
};
// act and assert
@ -85,7 +88,8 @@ describe("HTTP Transport Binding Binary Receiver 0.2", () => {
var attributes = {
"ce-type" : "type",
"ce-specversion" : "specversion",
"ce-source" : "source"
"ce-source" : "source",
"Content-Type" : "application/json"
};
// act and assert
@ -100,7 +104,8 @@ describe("HTTP Transport Binding Binary Receiver 0.2", () => {
"ce-type" : "type",
"ce-specversion" : "specversion",
"ce-source" : "source",
"ce-id" : "id"
"ce-id" : "id",
"Content-Type" : "application/json"
};
// act and assert
@ -108,6 +113,22 @@ describe("HTTP Transport Binding Binary Receiver 0.2", () => {
.to.throw("invalid spec version");
});
it("Throw error when the content-type is invalid", () => {
// setup
var payload = {};
var attributes = {
"ce-type" : "type",
"ce-specversion" : "specversion",
"ce-source" : "source",
"ce-id" : "id",
"Content-Type" : "text/html"
};
// act and assert
expect(receiver.check.bind(receiver, payload, attributes))
.to.throw("invalid content type");
});
it("No error when all required headers are in place", () => {
// setup
var payload = {};
@ -115,7 +136,8 @@ describe("HTTP Transport Binding Binary Receiver 0.2", () => {
"ce-type" : "type",
"ce-specversion" : "0.2",
"ce-source" : "source",
"ce-id" : "id"
"ce-id" : "id",
"Content-Type" : "application/json"
};
// act and assert
@ -136,7 +158,8 @@ describe("HTTP Transport Binding Binary Receiver 0.2", () => {
"ce-source" : "source",
"ce-id" : "id",
"ce-time" : "2019-06-16T11:42:00Z",
"ce-schemaurl" : "http://schema.registry/v1"
"ce-schemaurl" : "http://schema.registry/v1",
"Content-Type" : "application/json"
};
// act
@ -158,7 +181,8 @@ describe("HTTP Transport Binding Binary Receiver 0.2", () => {
"ce-source" : "source",
"ce-id" : "id",
"ce-time" : "2019-06-16T11:42:00Z",
"ce-schemaurl" : "http://schema.registry/v1"
"ce-schemaurl" : "http://schema.registry/v1",
"Content-Type" : "application/json"
};
// act
@ -180,7 +204,8 @@ describe("HTTP Transport Binding Binary Receiver 0.2", () => {
"ce-source" : "/source",
"ce-id" : "id",
"ce-time" : "2019-06-16T11:42:00Z",
"ce-schemaurl" : "http://schema.registry/v1"
"ce-schemaurl" : "http://schema.registry/v1",
"Content-Type" : "application/json"
};
// act
@ -202,7 +227,8 @@ describe("HTTP Transport Binding Binary Receiver 0.2", () => {
"ce-source" : "/source",
"ce-id" : "id",
"ce-time" : "2019-06-16T11:42:00Z",
"ce-schemaurl" : "http://schema.registry/v1"
"ce-schemaurl" : "http://schema.registry/v1",
"Content-Type" : "application/json"
};
// act
@ -224,7 +250,8 @@ describe("HTTP Transport Binding Binary Receiver 0.2", () => {
"ce-source" : "/source",
"ce-id" : "id",
"ce-time" : "2019-06-16T11:42:00.000Z",
"ce-schemaurl" : "http://schema.registry/v1"
"ce-schemaurl" : "http://schema.registry/v1",
"Content-Type" : "application/json"
};
// act
@ -246,7 +273,8 @@ describe("HTTP Transport Binding Binary Receiver 0.2", () => {
"ce-source" : "/source",
"ce-id" : "id",
"ce-time" : "2019-06-16T11:42:00Z",
"ce-schemaurl" : "http://schema.registry/v1"
"ce-schemaurl" : "http://schema.registry/v1",
"Content-Type" : "application/json"
};
// act
@ -268,7 +296,8 @@ describe("HTTP Transport Binding Binary Receiver 0.2", () => {
"ce-source" : "/source",
"ce-id" : "id",
"ce-time" : "2019-06-16T11:42:00Z",
"ce-schemaurl" : "http://schema.registry/v1"
"ce-schemaurl" : "http://schema.registry/v1",
"Content-Type" : "application/json"
};
// act
@ -290,7 +319,8 @@ describe("HTTP Transport Binding Binary Receiver 0.2", () => {
"ce-source" : "source",
"ce-id" : "id",
"ce-time" : "2019-06-16T11:42:00Z",
"ce-schemaurl" : "http://schema.registry/v1"
"ce-schemaurl" : "http://schema.registry/v1",
"Content-Type" : "application/json"
};
// act