Skip to content

Commit 9376b4d

Browse files
montymxbflovilmart
authored andcommitted
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.
1 parent 9745caf commit 9376b4d

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

spec/ParseServer.spec.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
/* Tests for ParseServer.js */
3+
const express = require('express');
4+
5+
import ParseServer from '../src/ParseServer';
6+
7+
describe('Server Url Checks', () => {
8+
9+
const app = express();
10+
app.get('/health', function(req, res){
11+
res.send('OK');
12+
});
13+
app.listen(13376);
14+
15+
it('validate good server url', (done) => {
16+
Parse.serverURL = 'http://localhost:13376';
17+
ParseServer.verifyServerUrl(function(result) {
18+
if(!result) {
19+
done.fail('Did not pass valid url');
20+
}
21+
done();
22+
});
23+
});
24+
25+
it('mark bad server url', (done) => {
26+
Parse.serverURL = 'notavalidurl';
27+
ParseServer.verifyServerUrl(function(result) {
28+
if(result) {
29+
done.fail('Did not mark invalid url');
30+
}
31+
done();
32+
});
33+
});
34+
});

src/ParseServer.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,9 @@ class ParseServer {
360360

361361
api.use(middlewares.handleParseErrors);
362362

363-
//This causes tests to spew some useless warnings, so disable in test
363+
// run the following when not testing
364364
if (!process.env.TESTING) {
365+
//This causes tests to spew some useless warnings, so disable in test
365366
process.on('uncaughtException', (err) => {
366367
if (err.code === "EADDRINUSE") { // user-friendly message for this common error
367368
/* eslint-disable no-console */
@@ -372,6 +373,10 @@ class ParseServer {
372373
throw err;
373374
}
374375
});
376+
// verify the server url after a 'mount' event is received
377+
api.on('mount', function() {
378+
ParseServer.verifyServerUrl();
379+
});
375380
}
376381
if (process.env.PARSE_SERVER_ENABLE_EXPERIMENTAL_DIRECT_ACCESS === '1') {
377382
Parse.CoreManager.setRESTController(ParseServerRESTController(appId, appRouter));
@@ -413,6 +418,27 @@ class ParseServer {
413418
static createLiveQueryServer(httpServer, config) {
414419
return new ParseLiveQueryServer(httpServer, config);
415420
}
421+
422+
static verifyServerUrl(callback) {
423+
// perform a health check on the serverURL value
424+
if(Parse.serverURL) {
425+
const request = require('request');
426+
request(Parse.serverURL.replace(/\/$/, "") + "/health", function (error, response, body) {
427+
if (error || response.statusCode !== 200 || body !== "OK") {
428+
/* eslint-disable no-console */
429+
console.warn(`\nWARNING, Unable to connect to '${Parse.serverURL}'.` +
430+
` Cloud code and push notifications may be unavailable!\n`);
431+
if(callback) {
432+
callback(false);
433+
}
434+
} else {
435+
if(callback) {
436+
callback(true);
437+
}
438+
}
439+
});
440+
}
441+
}
416442
}
417443

418444
function addParseCloud() {

0 commit comments

Comments
 (0)