* fix: detect if the caller is accessing us via local or parse for batch requests (#6980) * chore: minor cleanup from PR
This commit is contained in:
@@ -9,6 +9,7 @@ const serverURL1 = 'http://localhost:1234/1';
|
|||||||
const serverURLNaked = 'http://localhost:1234/';
|
const serverURLNaked = 'http://localhost:1234/';
|
||||||
const publicServerURL = 'http://domain.com/parse';
|
const publicServerURL = 'http://domain.com/parse';
|
||||||
const publicServerURLNaked = 'http://domain.com/';
|
const publicServerURLNaked = 'http://domain.com/';
|
||||||
|
const publicServerURLLong = 'https://domain.com/something/really/long';
|
||||||
|
|
||||||
const headers = {
|
const headers = {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
@@ -24,6 +25,26 @@ describe('batch', () => {
|
|||||||
expect(internalURL).toEqual('/classes/Object');
|
expect(internalURL).toEqual('/classes/Object');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should return the proper url given a public url-only path', () => {
|
||||||
|
const originalURL = '/something/really/long/batch';
|
||||||
|
const internalURL = batch.makeBatchRoutingPathFunction(
|
||||||
|
originalURL,
|
||||||
|
serverURL,
|
||||||
|
publicServerURLLong
|
||||||
|
)('/parse/classes/Object');
|
||||||
|
expect(internalURL).toEqual('/classes/Object');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return the proper url given a server url-only path', () => {
|
||||||
|
const originalURL = '/parse/batch';
|
||||||
|
const internalURL = batch.makeBatchRoutingPathFunction(
|
||||||
|
originalURL,
|
||||||
|
serverURL,
|
||||||
|
publicServerURLLong
|
||||||
|
)('/parse/classes/Object');
|
||||||
|
expect(internalURL).toEqual('/classes/Object');
|
||||||
|
});
|
||||||
|
|
||||||
it('should return the proper url same public/local endpoint', () => {
|
it('should return the proper url same public/local endpoint', () => {
|
||||||
const originalURL = '/parse/batch';
|
const originalURL = '/parse/batch';
|
||||||
const internalURL = batch.makeBatchRoutingPathFunction(
|
const internalURL = batch.makeBatchRoutingPathFunction(
|
||||||
|
|||||||
17
src/batch.js
17
src/batch.js
@@ -36,12 +36,23 @@ function makeBatchRoutingPathFunction(originalUrl, serverURL, publicServerURL) {
|
|||||||
if (serverURL && publicServerURL && serverURL.path != publicServerURL.path) {
|
if (serverURL && publicServerURL && serverURL.path != publicServerURL.path) {
|
||||||
const localPath = serverURL.path;
|
const localPath = serverURL.path;
|
||||||
const publicPath = publicServerURL.path;
|
const publicPath = publicServerURL.path;
|
||||||
|
|
||||||
// Override the api prefix
|
// Override the api prefix
|
||||||
apiPrefix = localPath;
|
apiPrefix = localPath;
|
||||||
return function (requestPath) {
|
return function (requestPath) {
|
||||||
// Build the new path by removing the public path
|
// Figure out which server url was used by figuring out which
|
||||||
// and joining with the local path
|
// path more closely matches requestPath
|
||||||
const newPath = path.posix.join('/', localPath, '/', requestPath.slice(publicPath.length));
|
const startsWithLocal = requestPath.startsWith(localPath);
|
||||||
|
const startsWithPublic = requestPath.startsWith(publicPath);
|
||||||
|
const pathLengthToUse =
|
||||||
|
startsWithLocal && startsWithPublic
|
||||||
|
? Math.max(localPath.length, publicPath.length)
|
||||||
|
: startsWithLocal
|
||||||
|
? localPath.length
|
||||||
|
: publicPath.length;
|
||||||
|
|
||||||
|
const newPath = path.posix.join('/', localPath, '/', requestPath.slice(pathLengthToUse));
|
||||||
|
|
||||||
// Use the method for local routing
|
// Use the method for local routing
|
||||||
return makeRoutablePath(newPath);
|
return makeRoutablePath(newPath);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user