-
-
Notifications
You must be signed in to change notification settings - Fork 959
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add tests specifically for removePort function includes http|ht…
…tps|wss|ws (#6874) * * chore(tests): add tests specifically for removePort function * chore(test): move removePort to it's own helper function * add support for more url types, http,https,ws,wss * chore(formatting): remove auto-formatting * chore(formatting): remove auto-formatting for test file * style: reformat --------- Co-authored-by: Frederick Engelhardt <frederick.engelhardt@bestbuy.com> Co-authored-by: Zoltan Kochan <z@kochan.io>
- Loading branch information
1 parent
e600056
commit 3c5aaaf
Showing
4 changed files
with
109 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { URL } from 'url' | ||
|
||
export function removePort (originalUrl: string) { | ||
const urlObj = new URL(originalUrl) | ||
if (urlObj.port === '') return urlObj.href | ||
urlObj.port = '' | ||
return urlObj.toString() | ||
} |
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
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,79 @@ | ||
import { removePort } from '../src/helpers/removePort' | ||
|
||
describe('removePort()', () => { | ||
it('does not mutate the url if no port is found', () => { | ||
const urlString = 'https://custom.domain.com/npm/-/foo-1.0.0.tgz' | ||
expect(removePort(urlString)).toEqual(urlString) | ||
|
||
const urlStringWithTrailingSlash = 'https://custom.domain.com/npm/' | ||
expect(removePort(urlStringWithTrailingSlash)).toEqual( | ||
urlStringWithTrailingSlash | ||
) | ||
}) | ||
|
||
it('removes ports from urls with https | https | ws | wss protocols', () => { | ||
const portsToTest = [1, 8888, 8080, 8081, 65535] | ||
const protocols = ['http', 'https', 'ws', 'wss'] | ||
|
||
const getUrl = (port: number, protocol: string) => | ||
`${protocol}://custom.domain.com:${port}/artifactory/api/npm/npm-virtual/-/foo-1.0.0.tgz` | ||
|
||
const expectedOutput = (protocol: string) => | ||
`${protocol}://custom.domain.com/artifactory/api/npm/npm-virtual/-/foo-1.0.0.tgz` | ||
|
||
portsToTest.forEach((port: number) => { | ||
protocols.forEach((protocol) => { | ||
expect(removePort(getUrl(port, protocol))).toEqual( | ||
expectedOutput(protocol) | ||
) | ||
}) | ||
}) | ||
}) | ||
|
||
it('removes ports from valid urls with http, https, ws, wss protocols', () => { | ||
const portsWithEmptyReturns = new Map([ | ||
['http', 80], | ||
['https', 443], | ||
['ws', 80], | ||
['wss', 443], | ||
]) | ||
|
||
const getUrl = (port: number, protocol: string) => | ||
`${protocol}://custom.domain.com:${port}/artifactory/api/npm/npm-virtual/-/foo-1.0.0.tgz` | ||
|
||
const expectedOutput = (protocol: string) => | ||
`${protocol}://custom.domain.com/artifactory/api/npm/npm-virtual/-/foo-1.0.0.tgz` | ||
|
||
portsWithEmptyReturns.forEach((value: number, protocol) => { | ||
expect(removePort(getUrl(value, protocol))).toEqual( | ||
expectedOutput(protocol) | ||
) | ||
}) | ||
}) | ||
|
||
/** | ||
* @description intentially mismatch the port | ||
* https|wss set to 443 | ||
* http|ws set to 80 | ||
* | ||
* @tests regexp loopholes of (80:443) | ||
*/ | ||
it('removes the ports from urls with protocol port mismatches', () => { | ||
const mistmatchProtocolPorts = new Map([ | ||
['http', 443], | ||
['ws', 443], | ||
['https', 80], | ||
['wss', 80], | ||
]) | ||
|
||
const getUrl = (port: number, protocol: string) => | ||
`${protocol}://custom.domain.com:${port}/artifactory/api/npm/npm-virtual/-/foo-1.0.0.tgz` | ||
const expectedOutput = (protocol: string) => | ||
`${protocol}://custom.domain.com/artifactory/api/npm/npm-virtual/-/foo-1.0.0.tgz` | ||
mistmatchProtocolPorts.forEach((value: number, protocol) => { | ||
expect(removePort(getUrl(value, protocol))).toEqual( | ||
expectedOutput(protocol) | ||
) | ||
}) | ||
}) | ||
}) |