Repository: vitejs/vite
File: packages/vite/src/node/__tests__/utils.spec.ts (TypeScript)

Code before:
-----END CERTIFICATE-----
    `.trim() as any

  test('should extract hostnames from certificate', () => {
    const httpsOptions = { cert: [createWorkingCert] } as any
    const result = extractHostnamesFromCerts(httpsOptions.cert)

    expect(result).toStrictEqual([
      'localhost',
      'foo.localhost',
      'vite.vite.localhost',
    ])
  })

  test('should extract hostnames from multiple certificates', () => {
    const httpsOptions = { cert: [createWorkingCert, createWorkingCert] } as any
    const result = extractHostnamesFromCerts(httpsOptions.cert)

    expect(result).toStrictEqual([
      'localhost',
      'foo.localhost',
      'vite.vite.localhost',
      'localhost',
      'foo.localhost',
      'vite.vite.localhost',
    ])
  })
})

describe('resolveServerUrls', () => {
  const createMockServer = (
    family: 'IPv4' | 'IPv6' = 'IPv4',
    address: string = '127.0.0.1',
  ) =>
    ({
      address: () => ({ port: 3000, address, family }),
    }) as any

  const createTestConfig = () => ({
    options: { https: true } as any,
    hostname: { host: '127.0.0.1', name: 'localhost' } as any,
    config: { rawBase: '/' } as any,
  })

  // Test certificate containing domains: 'localhost', 'foo.localhost', 'vite.vite.localhost',
  const createWorkingCert = `-----BEGIN CERTIFICATE-----
MIID7zCCAtegAwIBAgIJS9D2rIN7tA8mMA0GCSqGSIb3DQEBCwUAMGkxFDASBgNV
BAMTC2V4YW1wbGUub3JnMQswCQYDVQQGEwJVUzERMA8GA1UECBMIVmlyZ2luaWEx
EzARBgNVBAcTCkJsYWNrc2J1cmcxDTALBgNVBAoTBFRlc3QxDTALBgNVBAsTBFRl
c3QwHhcNMjUwMTMwMDQxNTI1WhcNMjUwMzAxMDQxNTI1WjBpMRQwEgYDVQQDEwtl
eGFtcGxlLm9yZzELMAkGA1UEBhMCVVMxETAPBgNVBAgTCFZpcmdpbmlhMRMwEQYD

Review comment (refactor):
Should we make `extractHostnamesFromCerts` only return unique hosts? IIUC the duplicated ones would end up showing multiple times when printed otherwise.

Code after:
    ])
  })
})

describe('resolveServerUrls', () => {
  const createMockServer = (
    family: 'IPv4' | 'IPv6' = 'IPv4',
    address: string = '127.0.0.1',
  ) =>
    ({
      address: () => ({ port: 3000, address, family }),
    }) as any

  const createTestConfig = () => ({
    options: { https: true } as any,
    hostname: { host: '127.0.0.1', name: 'localhost' } as any,
    config: { rawBase: '/' } as any,
  })

  test('should handle no certificate', () => {
    const mockServer = createMockServer()
    const { options, hostname, config } = createTestConfig()
    const httpsOptions = {}

    const result = resolveServerUrls(
      mockServer,
      options,
      hostname,
      httpsOptions,
      config,
    )

    expect(result.local).toContain('https://localhost:3000/')
  })

  test('should handle IPv4 single certificate', () => {
    const mockServer = createMockServer()
    const { options, hostname, config } = createTestConfig()
    const httpsOptions = { cert: [WORKING_TEST_CERT] }

    const result = resolveServerUrls(
      mockServer,
      options,
      hostname,
      httpsOptions,
      config,
    )

    expect(result.local).toContain('https://localhost:3000/')
    expect(result.local).toContain('https://foo.localhost:3000/')
    expect(result.local).toContain('https://vite.vite.localhost:3000/')