Closes #74: Only validate with Joi in unit tests.
This commit is contained in:
Родитель
84085a102e
Коммит
ee73c683e2
|
@ -1,8 +1,6 @@
|
|||
/*global platform_require*/
|
||||
"use strict";
|
||||
|
||||
const commonSchema = require("./schemas/commonSchema");
|
||||
const Joi = require("joi-browser");
|
||||
const uuid = require("uuid");
|
||||
const config = require("./config");
|
||||
|
||||
|
@ -15,8 +13,6 @@ class PingCentre {
|
|||
this._clientID = clientID || uuid();
|
||||
this._pingEndpoint = pingEndpoint || config.endpoint;
|
||||
|
||||
const schema = require(`./schemas/${topic}`);
|
||||
this._schema = schema || commonSchema;
|
||||
this._setGlobalFetch();
|
||||
}
|
||||
|
||||
|
@ -29,37 +25,22 @@ class PingCentre {
|
|||
require("isomorphic-fetch");
|
||||
}
|
||||
|
||||
validate(payload) {
|
||||
return new Promise((resolve, reject) => {
|
||||
Joi.validate(payload, this._schema, (err, value) => {
|
||||
if (err) {
|
||||
reject(new Error(`Invalid payload ${JSON.stringify(value)}: ${err}.`));
|
||||
} else {
|
||||
resolve(value);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
sendPing(data) {
|
||||
if (!data || typeof data !== "object" || Object.keys(data).length === 0) {
|
||||
throw new Error("Malformed or empty data object");
|
||||
}
|
||||
|
||||
sendPing(data, validate = true) {
|
||||
const payload = Object.assign({
|
||||
topic: this._topic,
|
||||
client_id: this._clientID
|
||||
}, data);
|
||||
|
||||
let validatePromise = Promise.resolve();
|
||||
if (validate) {
|
||||
validatePromise = this.validate(payload);
|
||||
}
|
||||
|
||||
return validatePromise.then(() => {
|
||||
return fetch(this._pingEndpoint, {method: "POST", body: JSON.stringify(payload)}).then(response => {
|
||||
if (!response.ok) {
|
||||
throw new Error(`Ping failure with response code: ${response.status}`);
|
||||
} else {
|
||||
return response;
|
||||
}
|
||||
});
|
||||
return fetch(this._pingEndpoint, {method: "POST", body: JSON.stringify(payload)}).then(response => {
|
||||
if (!response.ok) {
|
||||
throw new Error(`Ping failure with response code: ${response.status}`);
|
||||
} else {
|
||||
return response;
|
||||
}
|
||||
}).catch(e => {
|
||||
console.error(e.message);
|
||||
throw e;
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
/* global assert */
|
||||
"use strict";
|
||||
|
||||
const Joi = require("joi-browser");
|
||||
const fetchMock = require("fetch-mock");
|
||||
const PingCentre = require("../../src/ping-centre");
|
||||
const commonSchema = require("../../src/schemas/commonSchema.js");
|
||||
|
||||
const topic = "sample";
|
||||
let pingClient = new PingCentre(topic);
|
||||
|
@ -21,16 +23,18 @@ describe("Ping Centre Throws", function() {
|
|||
});
|
||||
});
|
||||
|
||||
describe("Joi Handles Various Cases", function() {
|
||||
describe("Handling malformed data", function() {
|
||||
it("rejects undefined data", function(done) {
|
||||
const promise = pingClient.sendPing();
|
||||
promise.should.be.rejected.notify(done);
|
||||
assert.throws(() => pingClient.sendPing());
|
||||
done();
|
||||
});
|
||||
it("rejects if data is not an object", function(done) {
|
||||
pingClient.sendPing(45).should.be.rejected.notify(done);
|
||||
assert.throws(() => pingClient.sendPing(45));
|
||||
done();
|
||||
});
|
||||
it("rejects if data is an empty object", function(done) {
|
||||
pingClient.sendPing({}).should.be.rejected.notify(done);
|
||||
assert.throws(() => pingClient.sendPing({}));
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -41,9 +45,7 @@ describe("Ping Centre Common Properties", function() {
|
|||
event_type: event_type,
|
||||
value: true,
|
||||
}).then(result => {
|
||||
assert.equal(JSON.parse(fetchMock.lastOptions("*").body).topic, topic, "topic exists in payload");
|
||||
assert.isNotNull(JSON.parse(fetchMock.lastOptions("*").body).client_id, "client_id exists in payload");
|
||||
assert.equal(JSON.parse(fetchMock.lastOptions("*").body).event_type, event_type, "event_type exists in payload");
|
||||
Joi.validate(JSON.parse(fetchMock.lastOptions("*").body), commonSchema);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
@ -78,12 +80,6 @@ describe("Setting Global Fetch", function() {
|
|||
});
|
||||
});
|
||||
|
||||
describe("Validation handling", function() {
|
||||
it("Invalid data is passed when validation is turned off", function(done) {
|
||||
pingClient.sendPing({}, false).should.be.fulfilled.notify(done);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Ping Centre Handles Server Errors", function() {
|
||||
it("handles 400 error", function(done) {
|
||||
fetchMock.restore();
|
||||
|
|
Загрузка…
Ссылка в новой задаче