github.com/damirazo/docker@v1.9.0/pkg/timeoutconn/timeoutconn_test.go (about)

     1  package timeoutconn
     2  
     3  import (
     4  	"bufio"
     5  	"fmt"
     6  	"net"
     7  	"net/http"
     8  	"net/http/httptest"
     9  	"testing"
    10  	"time"
    11  )
    12  
    13  func TestRead(t *testing.T) {
    14  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    15  		fmt.Fprintln(w, "hello")
    16  	}))
    17  	defer ts.Close()
    18  	conn, err := net.Dial("tcp", ts.URL[7:])
    19  	if err != nil {
    20  		t.Fatalf("failed to create connection to %q: %v", ts.URL, err)
    21  	}
    22  	tconn := New(conn, 1*time.Second)
    23  
    24  	if _, err = bufio.NewReader(tconn).ReadString('\n'); err == nil {
    25  		t.Fatalf("expected timeout error, got none")
    26  	}
    27  	if _, err := fmt.Fprintf(tconn, "GET / HTTP/1.0\r\n\r\n"); err != nil {
    28  		t.Errorf("unexpected error: %v", err)
    29  	}
    30  	if _, err = bufio.NewReader(tconn).ReadString('\n'); err != nil {
    31  		t.Errorf("unexpected error: %v", err)
    32  	}
    33  }