pkg/timeoutconn/timeoutconn_test.go
Rik Nijessen ee25c29598 Rename package timeout to timeoutconn.
Signed-off-by: Rik Nijessen <riknijessen@gmail.com>
2015-02-25 20:52:37 +01:00

33 lines
786 B
Go

package timeoutconn
import (
"bufio"
"fmt"
"net"
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestRead(t *testing.T) {
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)
}
tconn := New(conn, 1*time.Second)
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)
}
}