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