Files
pnpm/network/auth-header/test/removePort.test.ts
Khải f071d00815 refactor: replace forEach with for-loops (#8535)
* refactor: replace `forEach` with `for`-loops

Changes:
* Most `Object.keys(o).forEach` are replaced by `for in`.
* Most `Array.filter(c).forEach` are replaced by `for of` + `if continue`.
* `return` in `forEach` callbacks are replaced by `continue`.

There may be minor improvement to memory footprint as this change would
reduce the creations of temporary arrays and temporary functions.

* fix: return -> continue

* refactor: remove the commented out code
2024-09-18 03:58:25 +02:00

80 lines
2.6 KiB
TypeScript

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(new URL(urlString))).toEqual(urlString)
const urlStringWithTrailingSlash = 'https://custom.domain.com/npm/'
expect(removePort(new URL(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) =>
new URL(`${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`
for (const port of portsToTest) {
for (const protocol of protocols) {
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) =>
new URL(`${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 intentionally 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 mismatchProtocolPorts = new Map([
['http', 443],
['ws', 443],
['https', 80],
['wss', 80],
])
const getUrl = (port: number, protocol: string) =>
new URL(`${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`
mismatchProtocolPorts.forEach((value: number, protocol) => {
expect(removePort(getUrl(value, protocol))).toEqual(
expectedOutput(protocol)
)
})
})
})