Spaces:
Paused
Paused
File size: 3,900 Bytes
9ada4bc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
import { it, expect } from 'vitest'
import { Agent as HttpAgent } from 'http'
import { RequestOptions, Agent as HttpsAgent } from 'https'
import { getUrlByRequestOptions } from './getUrlByRequestOptions'
it('returns a URL based on the basic RequestOptions', () => {
expect(
getUrlByRequestOptions({
protocol: 'https:',
host: '127.0.0.1',
path: '/resource',
}).href
).toBe('https://127.0.0.1/resource')
})
it('inherits protocol and port from http.Agent, if set', () => {
expect(
getUrlByRequestOptions({
host: '127.0.0.1',
path: '/',
agent: new HttpAgent(),
}).href
).toBe('http://127.0.0.1/')
})
it('inherits protocol and port from https.Agent, if set', () => {
expect(
getUrlByRequestOptions({
host: '127.0.0.1',
path: '/',
agent: new HttpsAgent({
port: 3080,
}),
}).href
).toBe('https://127.0.0.1:3080/')
})
it('resolves protocol to "http" given no explicit protocol and no certificate', () => {
expect(
getUrlByRequestOptions({
host: '127.0.0.1',
path: '/',
}).href
).toBe('http://127.0.0.1/')
})
it('resolves protocol to "https" given no explicit protocol, but certificate', () => {
expect(
getUrlByRequestOptions({
host: '127.0.0.1',
path: '/secure',
cert: '<!-- SSL certificate -->',
}).href
).toBe('https://127.0.0.1/secure')
})
it('resolves protocol to "https" given no explicit protocol, but port is 443', () => {
expect(
getUrlByRequestOptions({
host: '127.0.0.1',
port: 443,
path: '/resource',
}).href
).toBe('https://127.0.0.1/resource')
})
it('resolves protocol to "https" given no explicit protocol, but agent port is 443', () => {
expect(
getUrlByRequestOptions({
host: '127.0.0.1',
agent: new HttpsAgent({
port: 443,
}),
path: '/resource',
}).href
).toBe('https://127.0.0.1/resource')
})
it('respects explicitly provided port', () => {
expect(
getUrlByRequestOptions({
protocol: 'http:',
host: '127.0.0.1',
port: 4002,
path: '/',
}).href
).toBe('http://127.0.0.1:4002/')
})
it('inherits "username" and "password"', () => {
const url = getUrlByRequestOptions({
protocol: 'https:',
host: '127.0.0.1',
path: '/user',
auth: 'admin:abc-123',
})
expect(url).toBeInstanceOf(URL)
expect(url).toHaveProperty('username', 'admin')
expect(url).toHaveProperty('password', 'abc-123')
expect(url).toHaveProperty('href', 'https://admin:[email protected]/user')
})
it('resolves hostname to localhost if none provided', () => {
expect(getUrlByRequestOptions({}).hostname).toBe('localhost')
})
it('resolves host to localhost if none provided', () => {
expect(getUrlByRequestOptions({}).host).toBe('localhost')
})
it('supports "hostname" and "port"', () => {
const options: RequestOptions = {
protocol: 'https:',
hostname: '127.0.0.1',
port: 1234,
path: '/resource',
}
expect(getUrlByRequestOptions(options).href).toBe(
'https://127.0.0.1:1234/resource'
)
})
it('use "hostname" if both "hostname" and "host" are specified', () => {
const options: RequestOptions = {
protocol: 'https:',
host: 'host',
hostname: 'hostname',
path: '/resource',
}
expect(getUrlByRequestOptions(options).href).toBe(
'https://hostname/resource'
)
})
it('parses "host" in IPv6', () => {
expect(
getUrlByRequestOptions({
host: '::1',
path: '/resource',
}).href
).toBe('http://[::1]/resource')
expect(
getUrlByRequestOptions({
host: '[::1]',
path: '/resource',
}).href
).toBe('http://[::1]/resource')
})
it('parses "host" and "port" in IPv6', () => {
expect(
getUrlByRequestOptions({
host: '::1',
port: 3001,
path: '/resource',
}).href
).toBe('http://[::1]:3001/resource')
})
|