Validate serverURL on Start (#4204)

* Added basic validation of publicServerURL

* Fixed up 'verifyServerUrl' and added tests

* Use Parse.serverURL instead, general cleanup.

* Test server port moved to 13376

* Removed reconfigureServer calls with simple changing of Parse.serverURL

* changed var to const

* Disabled automatic serverURL verification during testing, moved verification call into app.on('mount') callback, removed setTimeout from verification.
This commit is contained in:
Benjamin Wilson Friedman
2017-10-17 11:49:28 -07:00
committed by Florent Vilmart
parent 9745caf9fb
commit 9376b4d04a
2 changed files with 61 additions and 1 deletions

34
spec/ParseServer.spec.js Normal file
View File

@@ -0,0 +1,34 @@
'use strict';
/* Tests for ParseServer.js */
const express = require('express');
import ParseServer from '../src/ParseServer';
describe('Server Url Checks', () => {
const app = express();
app.get('/health', function(req, res){
res.send('OK');
});
app.listen(13376);
it('validate good server url', (done) => {
Parse.serverURL = 'http://localhost:13376';
ParseServer.verifyServerUrl(function(result) {
if(!result) {
done.fail('Did not pass valid url');
}
done();
});
});
it('mark bad server url', (done) => {
Parse.serverURL = 'notavalidurl';
ParseServer.verifyServerUrl(function(result) {
if(result) {
done.fail('Did not mark invalid url');
}
done();
});
});
});

View File

@@ -360,8 +360,9 @@ class ParseServer {
api.use(middlewares.handleParseErrors);
//This causes tests to spew some useless warnings, so disable in test
// run the following when not testing
if (!process.env.TESTING) {
//This causes tests to spew some useless warnings, so disable in test
process.on('uncaughtException', (err) => {
if (err.code === "EADDRINUSE") { // user-friendly message for this common error
/* eslint-disable no-console */
@@ -372,6 +373,10 @@ class ParseServer {
throw err;
}
});
// verify the server url after a 'mount' event is received
api.on('mount', function() {
ParseServer.verifyServerUrl();
});
}
if (process.env.PARSE_SERVER_ENABLE_EXPERIMENTAL_DIRECT_ACCESS === '1') {
Parse.CoreManager.setRESTController(ParseServerRESTController(appId, appRouter));
@@ -413,6 +418,27 @@ class ParseServer {
static createLiveQueryServer(httpServer, config) {
return new ParseLiveQueryServer(httpServer, config);
}
static verifyServerUrl(callback) {
// perform a health check on the serverURL value
if(Parse.serverURL) {
const request = require('request');
request(Parse.serverURL.replace(/\/$/, "") + "/health", function (error, response, body) {
if (error || response.statusCode !== 200 || body !== "OK") {
/* eslint-disable no-console */
console.warn(`\nWARNING, Unable to connect to '${Parse.serverURL}'.` +
` Cloud code and push notifications may be unavailable!\n`);
if(callback) {
callback(false);
}
} else {
if(callback) {
callback(true);
}
}
});
}
}
}
function addParseCloud() {