Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[22523] Fix path calculation #25479

Merged
merged 4 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/libs/Url.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ function addTrailingForwardSlash(url) {
*/
function getPathFromURL(url) {
try {
const path = new URL(url).pathname;
const parsedUrl = new URL(url);
const path = parsedUrl.pathname + parsedUrl.search + parsedUrl.hash;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be better way to get full path just removing host. Can you research on that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@situchan We can obviously just slice the URL string and remove the host and just return the path but imo this is a better approach since we actually parse the URL to check if the URL is valid or not and also encodes some characters into valid URL characters like '%20'.

If we just slice the string and remove the host and return the rest of the string that isn't very efficient and that is how it was actually done previously.

return path.substring(1); // Remove the leading '/'
} catch (error) {
console.error('Error parsing URL:', error);
Expand Down
35 changes: 22 additions & 13 deletions tests/unit/UrlTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ describe('Url', () => {
expect(Url.getPathFromURL('http://www.foo.com')).toEqual('');
expect(Url.getPathFromURL('http://foo.com/blah_blah')).toEqual('blah_blah');
expect(Url.getPathFromURL('http://foo.com/blah_blah_(wikipedia)')).toEqual('blah_blah_(wikipedia)');
expect(Url.getPathFromURL('http://www.example.com/wpstyle/?p=364')).toEqual('wpstyle/');
expect(Url.getPathFromURL('https://www.example.com/foo/?bar=baz&inga=42&quux')).toEqual('foo/');
expect(Url.getPathFromURL('http://foo.com/(something)?after=parens')).toEqual('(something)');
expect(Url.getPathFromURL('http://code.google.com/events/#&product=browser')).toEqual('events/');
expect(Url.getPathFromURL('http://foo.bar/?q=Test%20URL-encoded%20stuff')).toEqual('');
expect(Url.getPathFromURL('http://www.test.com/path?param=123#123')).toEqual('path');
expect(Url.getPathFromURL('http://www.example.com/wpstyle/?p=364')).toEqual('wpstyle/?p=364');
expect(Url.getPathFromURL('https://www.example.com/foo/?bar=baz&inga=42&quux')).toEqual('foo/?bar=baz&inga=42&quux');
expect(Url.getPathFromURL('http://foo.com/(something)?after=parens')).toEqual('(something)?after=parens');
expect(Url.getPathFromURL('http://code.google.com/events/#&product=browser')).toEqual('events/#&product=browser');
expect(Url.getPathFromURL('http://foo.bar/?q=Test%20URL-encoded%20stuff')).toEqual('?q=Test%20URL-encoded%20stuff');
expect(Url.getPathFromURL('http://www.test.com/path?param=123#123')).toEqual('path?param=123#123');
expect(Url.getPathFromURL('http://1337.net')).toEqual('');
expect(Url.getPathFromURL('http://a.b-c.de/')).toEqual('');
expect(Url.getPathFromURL('https://sd1.sd2.docs.google.com/')).toEqual('');
expect(Url.getPathFromURL('https://expensify.cash/#/r/1234')).toEqual('');
expect(Url.getPathFromURL('https://expensify.cash/#/r/1234')).toEqual('#/r/1234');
expect(Url.getPathFromURL('https://github.com/Expensify/ReactNativeChat/pull/6.45')).toEqual('Expensify/ReactNativeChat/pull/6.45');
expect(Url.getPathFromURL('https://github.com/Expensify/Expensify/issues/143,231')).toEqual('Expensify/Expensify/issues/143,231');
expect(Url.getPathFromURL('testRareTLDs.beer')).toEqual('');
Expand All @@ -26,18 +26,23 @@ describe('Url', () => {
// eslint-disable-next-line max-len
'https://www.expensify.com/_devportal/tools/logSearch/#query=request_id:(%22Ufjjim%22)+AND+timestamp:[2021-01-08T03:48:10.389Z+TO+2021-01-08T05:48:10.389Z]&index=logs_expensify-008878)',
),
).toEqual('_devportal/tools/logSearch/');
expect(Url.getPathFromURL('http://necolas.github.io/react-native-web/docs/?path=/docs/components-pressable--disabled ')).toEqual('react-native-web/docs/');
expect(Url.getPathFromURL('https://github.com/Expensify/Expensify.cash/issues/123#:~:text=Please%20work/Expensify.cash ')).toEqual('Expensify/Expensify.cash/issues/123');
expect(Url.getPathFromURL('https://github.com/Expensify/Expensify.cash/issues/123#:~:text=Please%20work/Expensify.cash ')).toEqual('Expensify/Expensify.cash/issues/123');
).toEqual('_devportal/tools/logSearch/#query=request_id:(%22Ufjjim%22)+AND+timestamp:[2021-01-08T03:48:10.389Z+TO+2021-01-08T05:48:10.389Z]&index=logs_expensify-008878)');
expect(Url.getPathFromURL('http://necolas.github.io/react-native-web/docs/?path=/docs/components-pressable--disabled ')).toEqual(
'react-native-web/docs/?path=/docs/components-pressable--disabled',
);
expect(Url.getPathFromURL('https://github.com/Expensify/Expensify.cash/issues/123#:~:text=Please%20work/Expensify.cash ')).toEqual(
'Expensify/Expensify.cash/issues/123#:~:text=Please%20work/Expensify.cash',
);
expect(Url.getPathFromURL('mm..food ')).toEqual('');
expect(Url.getPathFromURL('https://upwork.com/jobs/~016781e062ce860b84 ')).toEqual('jobs/~016781e062ce860b84');
expect(
Url.getPathFromURL(
// eslint-disable-next-line max-len
"https://bastion1.sjc/logs/app/kibana#/discover?_g=()&_a=(columns:!(_source),index:'2125cbe0-28a9-11e9-a79c-3de0157ed580',interval:auto,query:(language:lucene,query:''),sort:!(timestamp,desc))",
),
).toEqual('logs/app/kibana');
).toEqual(
"logs/app/kibana#/discover?_g=()&_a=(columns:!(_source),index:'2125cbe0-28a9-11e9-a79c-3de0157ed580',interval:auto,query:(language:lucene,query:''),sort:!(timestamp,desc))",
);
expect(
Url.getPathFromURL("https://google.com/maps/place/The+Flying'+Saucer/@42.4043314,-86.2742418,15z/data=!4m5!3m4!1s0x0:0xe28f6108670216bc!8m2!3d42.4043316!4d-86.2742121"),
).toEqual("maps/place/The+Flying'+Saucer/@42.4043314,-86.2742418,15z/data=!4m5!3m4!1s0x0:0xe28f6108670216bc!8m2!3d42.4043316!4d-86.2742121");
Expand All @@ -57,9 +62,13 @@ describe('Url', () => {
).toEqual(
'maps/place/Taj+Mahal+@is~%22Awesome%22/@27.1751496,78.0399535,17z/data=!4m12!1m6!3m5!1s0x39747121d702ff6d:0xdd2ae4803f767dde!2sTaj+Mahal!8m2!3d27.1751448!4d78.0421422!3m4!1s0x39747121d702ff6d:0xdd2ae4803f767dde!8m2!3d27.1751448!4d78.0421422',
);
expect(
Url.getPathFromURL(
'https://new.expensify.com/r/443044983936732/attachment?source=https://www.expensify.com/chat-attachments/3915228701265930556/w_a758d3c8444a64f98d37205b17141388064d458e.jpg',
),
).toEqual('r/443044983936732/attachment?source=https://www.expensify.com/chat-attachments/3915228701265930556/w_a758d3c8444a64f98d37205b17141388064d458e.jpg');
});
});

describe('hasSameExpensifyOrigin()', () => {
describe('happy path', () => {
it('It should work correctly', () => {
Expand Down
Loading