-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5a0cac6
commit 44bfe81
Showing
5 changed files
with
635 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { describe, it, expect, vi } from 'vitest'; | ||
import { normalizeUrlMiddleware } from './normalize-url-middleware.tsx'; | ||
|
||
describe('normalizeUrlMiddleware', () => { | ||
it('should remove double slash from http url', () => { | ||
const next = vi.fn(); | ||
normalizeUrlMiddleware(next)('http://localhost:3000//path/to/endpoint', {}); | ||
expect(next).toBeCalledWith('http://localhost:3000/path/to/endpoint', {}); | ||
}); | ||
|
||
it('should remove double slash from https url', () => { | ||
const next = vi.fn(); | ||
normalizeUrlMiddleware(next)( | ||
'https://localhost:3000//path/to/endpoint', | ||
{}, | ||
); | ||
expect(next).toBeCalledWith('https://localhost:3000/path/to/endpoint', {}); | ||
}); | ||
|
||
it('should keep single slash at http url', () => { | ||
const next = vi.fn(); | ||
normalizeUrlMiddleware(next)('http://localhost:3000/path/to/endpoint', {}); | ||
expect(next).toBeCalledWith('http://localhost:3000/path/to/endpoint', {}); | ||
}); | ||
|
||
it('should not modify the url when there is no path and no trailing slash', () => { | ||
const next = vi.fn(); | ||
normalizeUrlMiddleware(next)('http://localhost:3000', {}); | ||
expect(next).toBeCalledWith('http://localhost:3000', {}); | ||
}); | ||
|
||
it('should no modify the url when there is no path and a trailing slash', () => { | ||
const next = vi.fn(); | ||
normalizeUrlMiddleware(next)('http://localhost:3000/', {}); | ||
expect(next).toBeCalledWith('http://localhost:3000/', {}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { ConfiguredMiddleware } from 'wretch'; | ||
|
||
export const normalizeUrlMiddleware: ConfiguredMiddleware = (next) => { | ||
const matchDoubleSlashes = /^(http(s)?:\/\/[^/]*)(\/\/|\/)/; | ||
return (url, opts) => { | ||
return next(url.replace(matchDoubleSlashes, '$1/'), opts); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.