2015-02-25 14:52:37 -05:00
|
|
|
package timeoutconn
|
2014-06-07 18:11:57 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2015-02-25 10:59:29 -05:00
|
|
|
func TestRead(t *testing.T) {
|
2014-06-07 18:11:57 -04:00
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fmt.Fprintln(w, "hello")
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
|
|
|
conn, err := net.Dial("tcp", ts.URL[7:])
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create connection to %q: %v", ts.URL, err)
|
|
|
|
}
|
2015-02-25 10:59:29 -05:00
|
|
|
tconn := New(conn, 1*time.Second)
|
2014-06-07 18:11:57 -04:00
|
|
|
|
|
|
|
if _, err = bufio.NewReader(tconn).ReadString('\n'); err == nil {
|
|
|
|
t.Fatalf("expected timeout error, got none")
|
|
|
|
}
|
|
|
|
if _, err := fmt.Fprintf(tconn, "GET / HTTP/1.0\r\n\r\n"); err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
if _, err = bufio.NewReader(tconn).ReadString('\n'); err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
}
|