Parses the payload
Signed-off-by: Fabio José <fabiojose@gmail.com>
This commit is contained in:
parent
a1e2381dc9
commit
68b9866ffb
|
@ -3,6 +3,14 @@ const Commons = require("./commons.js");
|
|||
const Cloudevent = require("../../cloudevent.js");
|
||||
const Spec02 = require("../../specs/spec_0_2.js");
|
||||
|
||||
var JSONParser = require("../../formats/json/parser.js");
|
||||
|
||||
const jsonParserSpec02 = new JSONParser(new Spec02());
|
||||
|
||||
const parsers = {};
|
||||
parsers[Constants.MIME_JSON] = jsonParserSpec02;
|
||||
parsers[Constants.MIME_CE_JSON] = jsonParserSpec02;
|
||||
|
||||
const allowed_content_types = [];
|
||||
allowed_content_types.push(Constants.MIME_CE_JSON);
|
||||
|
||||
|
@ -15,8 +23,9 @@ function validate_args(payload, attributes) {
|
|||
throw {message: "attributes is null or undefined"};
|
||||
}
|
||||
|
||||
if((typeof payload) !== "object"){
|
||||
throw {message: "payload must be an object", erros: [typeof payload]};
|
||||
if((typeof payload) !== "object" && (typeof payload) !== "string"){
|
||||
throw {message: "payload must be an object or string",
|
||||
erros: [typeof payload]};
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -41,6 +50,15 @@ Receiver.prototype.check = function(payload, headers) {
|
|||
|
||||
Receiver.prototype.parse = function(payload, headers) {
|
||||
this.check(payload, headers);
|
||||
|
||||
var sanity_headers = Commons.sanity_and_clone(headers);
|
||||
|
||||
var contentType = sanity_headers[Constants.HEADER_CONTENT_TYPE];
|
||||
|
||||
var parser = parsers[contentType];
|
||||
var cloudevent = parser.parse(payload);
|
||||
|
||||
return cloudevent;
|
||||
}
|
||||
|
||||
module.exports = Receiver;
|
||||
|
|
|
@ -1,10 +1,41 @@
|
|||
var expect = require("chai").expect;
|
||||
var expect = require("chai").expect;
|
||||
var Cloudevent = require("../../../index.js");
|
||||
var Spec02 = require("../../../lib/specs/spec_0_2.js");
|
||||
|
||||
var HTTPStructuredReceiver02 =
|
||||
require("../../../lib/bindings/http/receiver_structured_0_2.js");
|
||||
|
||||
var receiver = new HTTPStructuredReceiver02();
|
||||
|
||||
const type = "com.github.pull.create";
|
||||
const source = "urn:event:from:myapi/resourse/123";
|
||||
const webhook = "https://cloudevents.io/webhook";
|
||||
const contentType = "application/cloudevents+json; charset=utf-8";
|
||||
const now = new Date();
|
||||
const schemaurl = "http://cloudevents.io/schema.json";
|
||||
|
||||
const ceContentType = "application/json";
|
||||
|
||||
const data = {
|
||||
foo: "bar"
|
||||
};
|
||||
|
||||
const ext1Name = "extension1";
|
||||
const ext1Value = "foobar";
|
||||
const ext2Name = "extension2";
|
||||
const ext2Value = "acme";
|
||||
|
||||
var cloudevent =
|
||||
new Cloudevent(Spec02)
|
||||
.type(type)
|
||||
.source(source)
|
||||
.contenttype(ceContentType)
|
||||
.time(now)
|
||||
.schemaurl(schemaurl)
|
||||
.data(data)
|
||||
.addExtension(ext1Name, ext1Value)
|
||||
.addExtension(ext2Name, ext2Value);
|
||||
|
||||
describe("HTTP Transport Binding Structured Receiver 0.2", () => {
|
||||
describe("Check", () => {
|
||||
it("Throw error when payload arg is null or undefined", () => {
|
||||
|
@ -27,21 +58,21 @@ describe("HTTP Transport Binding Structured Receiver 0.2", () => {
|
|||
.to.throw("attributes is null or undefined");
|
||||
});
|
||||
|
||||
it("Throw error when payload is not an object", () => {
|
||||
it("Throw error when payload is not an object or string", () => {
|
||||
// setup
|
||||
var payload = "wow";
|
||||
var payload = 1.0;
|
||||
var attributes = {};
|
||||
|
||||
// act and assert
|
||||
expect(receiver.check.bind(receiver, payload, attributes))
|
||||
.to.throw("payload must be an object");
|
||||
.to.throw("payload must be an object or string");
|
||||
});
|
||||
|
||||
it("Throw error when the content-type is invalid", () => {
|
||||
// setup
|
||||
var payload = {};
|
||||
var attributes = {
|
||||
"Content-Type" : "text/html"
|
||||
"Content-Type" : "text/html"
|
||||
};
|
||||
|
||||
// act and assert
|
||||
|
@ -53,7 +84,7 @@ describe("HTTP Transport Binding Structured Receiver 0.2", () => {
|
|||
// setup
|
||||
var payload = {};
|
||||
var attributes = {
|
||||
"Content-Type" : "application/cloudevents+json"
|
||||
"Content-Type" : "application/cloudevents+json"
|
||||
};
|
||||
|
||||
// act and assert
|
||||
|
@ -61,4 +92,27 @@ describe("HTTP Transport Binding Structured Receiver 0.2", () => {
|
|||
.to.not.throw();
|
||||
});
|
||||
});
|
||||
|
||||
describe("Parse", () => {
|
||||
it("Throw error when the event does not follow the spec 0.2", () => {
|
||||
// setup
|
||||
var payload =
|
||||
new Cloudevent()
|
||||
.type(type)
|
||||
.source(source)
|
||||
.contenttype(ceContentType)
|
||||
.time(now)
|
||||
.schemaurl(schemaurl)
|
||||
.data(data)
|
||||
.toString();
|
||||
|
||||
var headers = {
|
||||
"Content-Type":"application/cloudevents+json"
|
||||
};
|
||||
|
||||
// act and assert
|
||||
expect(receiver.parse.bind(receiver, payload, headers))
|
||||
.to.throw("invalid payload");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue