github.com/Andyfoo/golang/x/net@v0.0.0-20190901054642-57c1bf301704/websocket/dial_test.go (about)

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package websocket
     6  
     7  import (
     8  	"crypto/tls"
     9  	"fmt"
    10  	"log"
    11  	"net"
    12  	"net/http/httptest"
    13  	"testing"
    14  	"time"
    15  )
    16  
    17  // This test depend on Go 1.3+ because in earlier versions the Dialer won't be
    18  // used in TLS connections and a timeout won't be triggered.
    19  func TestDialConfigTLSWithDialer(t *testing.T) {
    20  	tlsServer := httptest.NewTLSServer(nil)
    21  	tlsServerAddr := tlsServer.Listener.Addr().String()
    22  	log.Print("Test TLS WebSocket server listening on ", tlsServerAddr)
    23  	defer tlsServer.Close()
    24  	config, _ := NewConfig(fmt.Sprintf("wss://%s/echo", tlsServerAddr), "http://localhost")
    25  	config.Dialer = &net.Dialer{
    26  		Deadline: time.Now().Add(-time.Minute),
    27  	}
    28  	config.TlsConfig = &tls.Config{
    29  		InsecureSkipVerify: true,
    30  	}
    31  	_, err := DialConfig(config)
    32  	dialerr, ok := err.(*DialError)
    33  	if !ok {
    34  		t.Fatalf("DialError expected, got %#v", err)
    35  	}
    36  	neterr, ok := dialerr.Err.(*net.OpError)
    37  	if !ok {
    38  		t.Fatalf("net.OpError error expected, got %#v", dialerr.Err)
    39  	}
    40  	if !neterr.Timeout() {
    41  		t.Fatalf("expected timeout error, got %#v", neterr)
    42  	}
    43  }