-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathhttp_server_test.go
More file actions
65 lines (52 loc) · 1.79 KB
/
Copy pathhttp_server_test.go
File metadata and controls
65 lines (52 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package testutil_test
import (
"bufio"
"fmt"
"net"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/require"
"github.com/coder/coder/v2/testutil"
)
func TestNewUnstartedHTTPServer(t *testing.T) {
t.Parallel()
send := func(conn net.Conn) error {
_, err := fmt.Fprintf(conn, "GET / HTTP/1.1\r\nHost: x\r\n\r\n")
if err != nil {
return err
}
resp, err := http.ReadResponse(bufio.NewReader(conn), nil)
if err != nil {
return err
}
_ = resp.Body.Close()
return nil
}
t.Run("defaults", func(t *testing.T) {
t.Parallel()
srv := testutil.NewHTTPTestServer(t, http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
}))
conn, err := net.Dial("tcp", srv.Listener.Addr().String())
require.NoError(t, err, "dial")
defer conn.Close()
// Keepalives should be disabled: first request succeeds, second request on the same conn fails.
require.NoError(t, send(conn), "keepalives disabled: first request on reused connection must succeed")
require.Error(t, send(conn), "keepalives disabled: second request on reused connection must fail")
})
t.Run("override", func(t *testing.T) {
t.Parallel()
srv := testutil.NewHTTPTestServer(t, http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
}), func(srv *httptest.Server) {
srv.Config.SetKeepAlivesEnabled(true)
})
conn, err := net.Dial("tcp", srv.Listener.Addr().String())
require.NoError(t, err, "dial")
defer conn.Close()
// Keepalives should be enabled: multiple requests on the same conn succeed.
require.NoError(t, send(conn), "keepalives enabled: first request on reused connection must succeed")
require.NoError(t, send(conn), "keepalives enabled: second request on reused connection must succeed")
})
}