mirror of
https://github.com/pnpm/pnpm.git
synced 2026-05-16 12:51:45 -04:00
test: add tests specifically for removePort function includes http|https|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>
This commit is contained in:
committed by
GitHub
parent
e60005635d
commit
3c5aaaf84e
8
network/auth-header/src/helpers/removePort.ts
Normal file
8
network/auth-header/src/helpers/removePort.ts
Normal file
@@ -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()
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import nerfDart from 'nerf-dart'
|
||||
import { URL } from 'url'
|
||||
import { getAuthHeadersFromConfig } from './getAuthHeadersFromConfig'
|
||||
import { removePort } from './helpers/removePort'
|
||||
|
||||
export function createGetAuthHeaderByURI (
|
||||
opts: {
|
||||
@@ -35,11 +35,4 @@ function getAuthHeaderByURI (authHeaders: Record<string, string>, maxParts: numb
|
||||
return getAuthHeaderByURI(authHeaders, maxParts, urlWithoutPort)
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
function removePort (originalUrl: string) {
|
||||
const urlObj = new URL(originalUrl)
|
||||
if (urlObj.port === '') return urlObj.href
|
||||
urlObj.port = ''
|
||||
return urlObj.toString()
|
||||
}
|
||||
}
|
||||
@@ -1,20 +1,32 @@
|
||||
import { createGetAuthHeaderByURI } from '@pnpm/network.auth-header'
|
||||
|
||||
const opts = {
|
||||
allSettings: {
|
||||
'//reg.com/:_authToken': 'abc123',
|
||||
'//reg.co/tarballs/:_authToken': 'xxx',
|
||||
'//reg.gg:8888/:_authToken': '0000',
|
||||
'//custom.domain.com/artifactory/api/npm/npm-virtual/:_authToken': 'xyz',
|
||||
},
|
||||
userSettings: {},
|
||||
}
|
||||
|
||||
test('getAuthHeaderByURI()', () => {
|
||||
const getAuthHeaderByURI = createGetAuthHeaderByURI({
|
||||
allSettings: {
|
||||
'//reg.com/:_authToken': 'abc123',
|
||||
'//reg.co/tarballs/:_authToken': 'xxx',
|
||||
'//reg.gg:8888/:_authToken': '0000',
|
||||
},
|
||||
userSettings: {},
|
||||
})
|
||||
const getAuthHeaderByURI = createGetAuthHeaderByURI(opts)
|
||||
expect(getAuthHeaderByURI('https://reg.com/')).toBe('Bearer abc123')
|
||||
expect(getAuthHeaderByURI('https://reg.com/foo/-/foo-1.0.0.tgz')).toBe('Bearer abc123')
|
||||
expect(getAuthHeaderByURI('https://reg.com:8080/foo/-/foo-1.0.0.tgz')).toBe('Bearer abc123')
|
||||
expect(getAuthHeaderByURI('https://reg.io/foo/-/foo-1.0.0.tgz')).toBe(undefined)
|
||||
expect(getAuthHeaderByURI('https://reg.co/tarballs/foo/-/foo-1.0.0.tgz')).toBe('Bearer xxx')
|
||||
expect(getAuthHeaderByURI('https://reg.gg:8888/foo/-/foo-1.0.0.tgz')).toBe('Bearer 0000')
|
||||
expect(getAuthHeaderByURI('https://reg.gg:8888/foo/-/foo-1.0.0.tgz')).toBe('Bearer 0000')
|
||||
})
|
||||
|
||||
test('getAuthHeaderByURI() https port 443 checks', () => {
|
||||
const getAuthHeaderByURI = createGetAuthHeaderByURI(opts)
|
||||
expect(getAuthHeaderByURI('https://custom.domain.com:443/artifactory/api/npm/npm-virtual/')).toBe('Bearer xyz')
|
||||
expect(getAuthHeaderByURI('https://custom.domain.com:443/artifactory/api/npm/')).toBe(undefined)
|
||||
expect(getAuthHeaderByURI('https://custom.domain.com:443/artifactory/api/npm/-/@platform/device-utils-1.0.0.tgz')).toBe(undefined)
|
||||
expect(getAuthHeaderByURI('https://custom.domain.com:443/artifactory/api/npm/npm-virtual/@platform/device-utils/-/@platform/device-utils-1.0.0.tgz')).toBe('Bearer xyz')
|
||||
})
|
||||
|
||||
test('getAuthHeaderByURI() when default ports are specified', () => {
|
||||
|
||||
79
network/auth-header/test/removePort.test.ts
Normal file
79
network/auth-header/test/removePort.test.ts
Normal file
@@ -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)
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user