Skip to content

Commit

Permalink
fix: handle redirect with multiple slashes correctly
Browse files Browse the repository at this point in the history
* fix: handle redirect with multiple slashes correctly

* chore: bump deprecated actions/upload-artifact@v2

* chore: add parseUrl test with multiple slashes when port is present

* chore: slightly change implementation to utilise url object due to missleading test case.

* chore: fix test formatting
  • Loading branch information
wRLSS authored Oct 9, 2024
1 parent 5877d8b commit e0f0c23
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
- run: npm run build
- run: npm run coverage
- if: matrix.node-version == '20.x'
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v3
with:
name: Code coverage
path: coverage/
Expand Down
4 changes: 4 additions & 0 deletions src/app/utils/parseAsFullyQualifiedURI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ export default function parseAsFullyQualifiedURI(uri: string) {
try {
const urlObj = new URL(uri);
origin = urlObj.origin;

// Apply replacement only to the pathname, leaving the rest (search, hash) intact
urlObj.pathname = urlObj.pathname.replace(/\/{2,}/g, '/');

uri = urlObj.pathname + urlObj.search + urlObj.hash;
} catch {}

Expand Down
20 changes: 20 additions & 0 deletions test/app/IlcIntl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ describe('IlcIntl', () => {
expect(IlcIntl.localizeUrl(baseConfig, 'http://tst.com/', { locale: 'es-ES' })).to.eq('http://tst.com/es/');
});

it('handles multiple slashes in the URL correctly', () => {
expect(IlcIntl.localizeUrl(baseConfig, 'http://tst.com/es///google.com', { locale: 'es-ES' })).to.eq(
'http://tst.com/es/google.com',
);
});

it('handles special cases', () => {
expect(IlcIntl.localizeUrl(baseConfig, '/')).to.equal('/');
expect(IlcIntl.localizeUrl(baseConfig, '/', { locale: 'es-ES' })).to.equal('/es/');
Expand Down Expand Up @@ -211,6 +217,20 @@ describe('IlcIntl', () => {
});
});

it('handles multiple slashes in the URL correctly', () => {
expect(IlcIntl.parseUrl(baseConfig, 'http://tst.com/es///google.com')).to.eql({
cleanUrl: 'http://tst.com/google.com',
locale: 'es-ES',
});
});

it('handles URL with multiple slashes and a port', () => {
expect(IlcIntl.parseUrl(baseConfig, 'http://tst.com:3000/es///google.com')).to.eql({
cleanUrl: 'http://tst.com:3000/google.com',
locale: 'es-ES',
});
});

it('handles corner cases', () => {
expect(IlcIntl.parseUrl(baseConfig, '/')).to.eql({
cleanUrl: '/',
Expand Down

0 comments on commit e0f0c23

Please sign in to comment.