https_test.go 700 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package vhost
  2. import (
  3. "crypto/tls"
  4. "net"
  5. "testing"
  6. "time"
  7. "github.com/stretchr/testify/require"
  8. )
  9. func TestGetHTTPSHostname(t *testing.T) {
  10. require := require.New(t)
  11. l, err := net.Listen("tcp", ":")
  12. require.NoError(err)
  13. defer l.Close()
  14. var conn net.Conn
  15. go func() {
  16. conn, _ = l.Accept()
  17. require.NotNil(conn)
  18. }()
  19. go func() {
  20. time.Sleep(100 * time.Millisecond)
  21. tls.Dial("tcp", l.Addr().String(), &tls.Config{
  22. InsecureSkipVerify: true,
  23. ServerName: "example.com",
  24. })
  25. }()
  26. time.Sleep(200 * time.Millisecond)
  27. _, infos, err := GetHTTPSHostname(conn)
  28. require.NoError(err)
  29. require.Equal("example.com", infos["Host"])
  30. require.Equal("https", infos["Scheme"])
  31. }