2018-05-05 23:48:56 +00:00
|
|
|
const assert = require('assert');
|
2018-05-05 17:22:08 +00:00
|
|
|
const functions = require('firebase-functions');
|
|
|
|
|
|
|
|
const TEST_DATA = require('./test-data');
|
|
|
|
|
|
|
|
exports.runTest = functions.https.onCall(data => {
|
2018-05-05 23:48:56 +00:00
|
|
|
console.log(Date.now(), data);
|
|
|
|
|
2018-05-05 17:32:56 +00:00
|
|
|
if (typeof data === 'undefined') {
|
|
|
|
return 'undefined';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof data === 'string') {
|
|
|
|
return 'string';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof data === 'number') {
|
|
|
|
return 'number';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof data === 'boolean') {
|
|
|
|
return 'boolean';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data === null) {
|
|
|
|
return 'null';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Array.isArray(data)) {
|
|
|
|
return 'array';
|
|
|
|
}
|
|
|
|
|
2018-05-05 17:22:08 +00:00
|
|
|
const { type, asError, inputData } = data;
|
|
|
|
if (!Object.hasOwnProperty.call(TEST_DATA, type)) {
|
|
|
|
throw new functions.https.HttpsError(
|
|
|
|
'invalid-argument',
|
|
|
|
'Invalid test requested.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const outputData = TEST_DATA[type];
|
|
|
|
|
2018-05-05 23:48:56 +00:00
|
|
|
try {
|
|
|
|
assert.deepEqual(outputData, inputData);
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
2018-05-05 17:22:08 +00:00
|
|
|
throw new functions.https.HttpsError(
|
|
|
|
'invalid-argument',
|
2018-05-05 23:48:56 +00:00
|
|
|
'Input and Output types did not match.',
|
|
|
|
e.message
|
2018-05-05 17:22:08 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// all good
|
|
|
|
if (asError) {
|
|
|
|
throw new functions.https.HttpsError(
|
|
|
|
'cancelled',
|
2018-05-05 23:48:56 +00:00
|
|
|
'Response data was requested to be sent as part of an Error payload, so here we are!',
|
2018-05-05 17:22:08 +00:00
|
|
|
outputData
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return outputData;
|
|
|
|
});
|