Skip to content

Commit ab18586

Browse files
committed
Adds ability to override mount with publicServerURL for production uses
1 parent 7afc08a commit ab18586

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

spec/index.spec.js

+34
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var request = require('request');
22
var parseServerPackage = require('../package.json');
33
var MockEmailAdapterWithOptions = require('./MockEmailAdapterWithOptions');
44
var ParseServer = require("../src/index");
5+
var Config = require('../src/Config');
56
var express = require('express');
67

78
describe('server', () => {
@@ -246,4 +247,37 @@ describe('server', () => {
246247
expect(ParseServer.FileSystemAdapter).toThrow();
247248
done();
248249
});
250+
251+
it('properly gives publicServerURL when set', done => {
252+
setServerConfiguration({
253+
serverURL: 'http://localhost:8378/1',
254+
appId: 'test',
255+
masterKey: 'test',
256+
publicServerURL: 'https://myserver.com/1'
257+
});
258+
var config = new Config('test', 'http://localhost:8378/1');
259+
expect(config.mount).toEqual('https://myserver.com/1');
260+
done();
261+
});
262+
263+
it('properly removes trailing slash in mount', done => {
264+
setServerConfiguration({
265+
serverURL: 'http://localhost:8378/1',
266+
appId: 'test',
267+
masterKey: 'test'
268+
});
269+
var config = new Config('test', 'http://localhost:8378/1/');
270+
expect(config.mount).toEqual('http://localhost:8378/1');
271+
done();
272+
});
273+
274+
it('should throw when getting invalid mount', done => {
275+
expect(() => setServerConfiguration({
276+
serverURL: 'http://localhost:8378/1',
277+
appId: 'test',
278+
masterKey: 'test',
279+
publicServerURL: 'blabla:/some'
280+
}) ).toThrow("publicServerURL should be a valid HTTPS URL starting with https://");
281+
done();
282+
});
249283
});

src/Config.js

+29-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@
44

55
import cache from './cache';
66

7+
function removeTrailingSlash(str) {
8+
if (!str) {
9+
return str;
10+
}
11+
if (str.endsWith("/")) {
12+
str = str.substr(0, str.length-1);
13+
}
14+
return str;
15+
}
16+
717
export class Config {
818
constructor(applicationId: string, mount: string) {
919
let DatabaseAdapter = require('./DatabaseAdapter');
@@ -24,7 +34,7 @@ export class Config {
2434
this.database = DatabaseAdapter.getDatabaseConnection(applicationId, cacheInfo.collectionPrefix);
2535

2636
this.serverURL = cacheInfo.serverURL;
27-
this.publicServerURL = cacheInfo.publicServerURL;
37+
this.publicServerURL = removeTrailingSlash(cacheInfo.publicServerURL);
2838
this.verifyUserEmails = cacheInfo.verifyUserEmails;
2939
this.appName = cacheInfo.appName;
3040

@@ -35,14 +45,19 @@ export class Config {
3545
this.userController = cacheInfo.userController;
3646
this.authDataManager = cacheInfo.authDataManager;
3747
this.customPages = cacheInfo.customPages || {};
38-
this.mount = mount;
48+
this.mount = removeTrailingSlash(mount);
3949
this.liveQueryController = cacheInfo.liveQueryController;
4050
}
4151

4252
static validate(options) {
4353
this.validateEmailConfiguration({verifyUserEmails: options.verifyUserEmails,
4454
appName: options.appName,
4555
publicServerURL: options.publicServerURL})
56+
if (options.publicServerURL) {
57+
if (!options.publicServerURL.startsWith("http://") && !options.publicServerURL.startsWith("https://")) {
58+
throw "publicServerURL should be a valid HTTPS URL starting with https://"
59+
}
60+
}
4661
}
4762

4863
static validateEmailConfiguration({verifyUserEmails, appName, publicServerURL}) {
@@ -56,6 +71,18 @@ export class Config {
5671
}
5772
}
5873

74+
get mount() {
75+
var mount = this._mount;
76+
if (this.publicServerURL) {
77+
mount = this.publicServerURL;
78+
}
79+
return mount;
80+
}
81+
82+
set mount(newValue) {
83+
this._mount = newValue;
84+
}
85+
5986
get invalidLinkURL() {
6087
return this.customPages.invalidLink || `${this.publicServerURL}/apps/invalid_link.html`;
6188
}

0 commit comments

Comments
 (0)