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

Code before:
  // Test certificate containing domains: 'localhost', 'foo.localhost', 'vite.vite.localhost',
  const createWorkingCert = `-----BEGIN CERTIFICATE-----
MIID7zCCAtegAwIBAgIJS9D2rIN7tA8mMA0GCSqGSIb3DQEBCwUAMGkxFDASBgNV
BAMTC2V4YW1wbGUub3JnMQswCQYDVQQGEwJVUzERMA8GA1UECBMIVmlyZ2luaWEx
EzARBgNVBAcTCkJsYWNrc2J1cmcxDTALBgNVBAoTBFRlc3QxDTALBgNVBAsTBFRl
c3QwHhcNMjUwMTMwMDQxNTI1WhcNMjUwMzAxMDQxNTI1WjBpMRQwEgYDVQQDEwtl
eGFtcGxlLm9yZzELMAkGA1UEBhMCVVMxETAPBgNVBAgTCFZpcmdpbmlhMRMwEQYD
VQQHEwpCbGFja3NidXJnMQ0wCwYDVQQKEwRUZXN0MQ0wCwYDVQQLEwRUZXN0MIIB
IjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxNPlCqTmUZ7/F7GyFWDopqZ6
w19Y7/98B10JEeFGTAQIj/RP2UgZNcTABQDUvtkF7y+bOeoVJW7Zz8ozQYhRaDp8
CN2gXMcYeTUku/pKLXyCzHHVrOPAXTeU7sMRgLvPCrrJtx5OjvndW+O/PhohPRi3
iEpPvpM8gi7MVRGhnWVSx0/Ynx5c0+/vqyBTzrM2OX7Ufg8Nv7LaTXpCAnmIQp+f
Sqq7HZ7t6Y7laS4RApityvlnFHZ4f2cEibAKv/vXLED7bgAlGb8R1viPRdMtAPuI
MYvHBgGFjyX1fmq6Mz3aqlAscJILtbQlwty1oYyaENE0lq8+nZXQ+t6I+CIVLQID
AQABo4GZMIGWMAsGA1UdDwQEAwIC9DAxBgNVHSUEKjAoBggrBgEFBQcDAQYIKwYB
BQUHAwIGCCsGAQUFBwMDBggrBgEFBQcDCDBUBgNVHREETTBLgglsb2NhbGhvc3SC
DWZvby5sb2NhbGhvc3SCECoudml0ZS5sb2NhbGhvc3SCBVs6OjFdhwR/AAABhxD+
gAAAAAAAAAAAAAAAAAABMA0GCSqGSIb3DQEBCwUAA4IBAQBi302qLCgxWsUalgc2
olFxVKob1xCciS8yUVX6HX0vza0WJ7oGW6qZsBbQtfgDwB/dHv7rwsfpjRWvFhmq
gEUrewa1h0TIC+PPTYYz4M0LOwcLIWZLZr4am1eI7YP9NDgRdhfAfM4hw20vjf2a
kYLKyRTC5+3/ly5opMq+CGLQ8/gnFxhP3ho8JYrRnqLeh3KCTGen3kmbAhD4IOJ9
lxMwFPTTWLFFjxbXjXmt5cEiL2mpcq13VCF2HmheCen37CyYIkrwK9IfLhBd5QQh
WEIBLwjKCAscrtyayXWp6zUTmgvb8PQf//3Mh2DiEngAi3WI/nL+8Y0RkwbvxBar
X2JN
-----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',
    ])
  })
})

describe('resolveServerUrls', () => {

Review comment (suggestion):
Looks like this variable is declared twice here. Maybe it can be hoisted up to the top-level and be shared.

Also it would be nice to add a comment for how this cert was generated (the command used) so we can extend/tweak/create them in the future for testing.

Code after:
      const actual = getServerUrlByHost(urls, host)
      expect(actual).toBe(expected)
    })
  }
})

describe('extractHostnamesFromCerts', () => {
  test('should extract hostnames from certificate', () => {
    const certs = [WORKING_TEST_CERT]
    const result = extractHostnamesFromCerts(certs)
    expect(result).toStrictEqual([
      'localhost',
      'foo.localhost',
      'vite.vite.localhost',
    ])
  })

  test('should extract hostnames from multiple certificates', () => {
    const certs = [WORKING_TEST_CERT, WORKING_TEST_CERT]
    const result = extractHostnamesFromCerts(certs)
    expect(result).toStrictEqual([
      '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('should handle no certificate', () => {
    const mockServer = createMockServer()
    const { options, hostname, config } = createTestConfig()
    const httpsOptions = {}

    const result = resolveServerUrls(
      mockServer,
      options,