Skip to content

Commit abdfe61

Browse files
authored
Properly handle serverURL and publicServerUrl in Batch requests #6980 (#7049)
* fix: detect if the caller is accessing us via local or parse for batch requests (#6980) * chore: minor cleanup from PR
1 parent ca1b782 commit abdfe61

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

spec/batch.spec.js

+21
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const serverURL1 = 'http://localhost:1234/1';
99
const serverURLNaked = 'http://localhost:1234/';
1010
const publicServerURL = 'http://domain.com/parse';
1111
const publicServerURLNaked = 'http://domain.com/';
12+
const publicServerURLLong = 'https://domain.com/something/really/long';
1213

1314
const headers = {
1415
'Content-Type': 'application/json',
@@ -24,6 +25,26 @@ describe('batch', () => {
2425
expect(internalURL).toEqual('/classes/Object');
2526
});
2627

28+
it('should return the proper url given a public url-only path', () => {
29+
const originalURL = '/something/really/long/batch';
30+
const internalURL = batch.makeBatchRoutingPathFunction(
31+
originalURL,
32+
serverURL,
33+
publicServerURLLong
34+
)('/parse/classes/Object');
35+
expect(internalURL).toEqual('/classes/Object');
36+
});
37+
38+
it('should return the proper url given a server url-only path', () => {
39+
const originalURL = '/parse/batch';
40+
const internalURL = batch.makeBatchRoutingPathFunction(
41+
originalURL,
42+
serverURL,
43+
publicServerURLLong
44+
)('/parse/classes/Object');
45+
expect(internalURL).toEqual('/classes/Object');
46+
});
47+
2748
it('should return the proper url same public/local endpoint', () => {
2849
const originalURL = '/parse/batch';
2950
const internalURL = batch.makeBatchRoutingPathFunction(

src/batch.js

+14-3
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,23 @@ function makeBatchRoutingPathFunction(originalUrl, serverURL, publicServerURL) {
3636
if (serverURL && publicServerURL && serverURL.path != publicServerURL.path) {
3737
const localPath = serverURL.path;
3838
const publicPath = publicServerURL.path;
39+
3940
// Override the api prefix
4041
apiPrefix = localPath;
4142
return function (requestPath) {
42-
// Build the new path by removing the public path
43-
// and joining with the local path
44-
const newPath = path.posix.join('/', localPath, '/', requestPath.slice(publicPath.length));
43+
// Figure out which server url was used by figuring out which
44+
// path more closely matches requestPath
45+
const startsWithLocal = requestPath.startsWith(localPath);
46+
const startsWithPublic = requestPath.startsWith(publicPath);
47+
const pathLengthToUse =
48+
startsWithLocal && startsWithPublic
49+
? Math.max(localPath.length, publicPath.length)
50+
: startsWithLocal
51+
? localPath.length
52+
: publicPath.length;
53+
54+
const newPath = path.posix.join('/', localPath, '/', requestPath.slice(pathLengthToUse));
55+
4556
// Use the method for local routing
4657
return makeRoutablePath(newPath);
4758
};

0 commit comments

Comments
 (0)