-
Notifications
You must be signed in to change notification settings - Fork 22
/
conn_test.go
361 lines (307 loc) · 8.78 KB
/
conn_test.go
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package h2conn
import (
"bufio"
"bytes"
"context"
"crypto/tls"
"encoding/binary"
"encoding/gob"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/http/httptest"
"sync"
"testing"
"time"
"github.com/posener/h2conn/h2test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/net/http2"
)
const (
numRequests = 100
shortDuration = 300 * time.Millisecond
)
var insecureClient = Client{
Client: &http.Client{
Transport: &http2.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}},
},
}
// TestConcurrent runs a simple test of concurrent reads and writes on an HTTP2 full duplex connection
func TestConcurrent(t *testing.T) {
t.Parallel()
var serverConn *Conn
server := h2test.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var err error
serverConn, err = Accept(w, r)
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
defer serverConn.Close()
// simple read loop that echos the upper case of what was read.
buf := bufio.NewReader(serverConn)
for {
msg, _, err := buf.ReadLine()
if err != nil {
log.Printf("Server failed read: %s", err)
break
}
_, err = serverConn.Write(append(bytes.ToUpper(msg), '\n'))
if err != nil {
log.Printf("Server failed write: %s", err)
break
}
}
}))
defer server.Close()
clientConn, resp, err := insecureClient.Connect(context.Background(), server.URL)
require.Nil(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
buf := bufio.NewReader(clientConn)
var wg sync.WaitGroup
wg.Add(numRequests)
go func() {
for i := 0; i < numRequests; i++ {
_, err := clientConn.Write([]byte("hello\n"))
require.Nil(t, err)
}
}()
go func() {
for i := 0; i < numRequests; i++ {
msg, _, err := buf.ReadLine()
require.Nil(t, err)
assert.Equal(t, "HELLO", string(msg))
wg.Done()
}
}()
wg.Wait()
clientConn.Close()
}
// TestClientClose tests that server gets io.EOF after client closed the connection
func TestClientClose(t *testing.T) {
t.Parallel()
server, serverAccepted, serverHandlerWait := startServer()
defer server.Close()
defer close(serverHandlerWait)
clientConn, resp, err := insecureClient.Connect(context.Background(), server.URL)
require.Nil(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
serverConn := <-serverAccepted
// close client connection
clientConn.Close()
// test that read from server returns an io.EOF error
var buf = make([]byte, 100)
n, err := serverConn.Read(buf)
assert.Equal(t, io.EOF, err)
assert.Equal(t, 0, n)
}
// TestServer tests that client gets io.EOF after server closed the connection
func TestServerClose(t *testing.T) {
t.Parallel()
server, serverAccepted, serverHandlerWait := startServer()
defer server.Close()
clientConn, resp, err := insecureClient.Connect(context.Background(), server.URL)
require.Nil(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
serverConn := <-serverAccepted
// close server connection
serverConn.Close()
close(serverHandlerWait)
// test that read from server returns an io.EOF error
var buf = make([]byte, 100)
n, err := clientConn.Read(buf)
assert.Equal(t, io.EOF, err)
assert.Equal(t, 0, n)
}
func TestSpecialCases(t *testing.T) {
t.Parallel()
tests := []struct {
name string
server func(*testing.T) *httptest.Server
client func() *Client
wantErr bool
}{
{
name: "insecure transport",
server: nopHandler,
client: func() *Client {
return &Client{Client: &http.Client{Transport: &http2.Transport{}}}
},
wantErr: true,
},
{
name: "invalid request",
server: nopHandler,
client: func() *Client {
cl := insecureClient
cl.Method = "\n"
return &cl
},
wantErr: true,
},
{
name: "headers",
client: func() *Client {
cl := insecureClient
cl.Header = http.Header{"Foo": []string{"bar"}}
return &cl
},
server: func(*testing.T) *httptest.Server {
return h2test.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := Accept(w, r)
require.NoError(t, err)
assert.Equal(t, "bar", r.Header.Get("Foo"))
}))
},
},
{
name: "server use http1",
server: func(*testing.T) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := Accept(w, r)
assert.Error(t, err)
}))
},
wantErr: true,
},
{
name: "server and client use http1",
client: func() *Client {
return &Client{Client: &http.Client{
// Timeout must be set, otherwise the client hangs because of an open client writer.
Timeout: shortDuration,
}}
},
server: func(*testing.T) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := Accept(w, r)
assert.Error(t, err)
}))
},
wantErr: true,
},
{
name: "client use http1 transport",
client: func() *Client {
return &Client{Client: &http.Client{
Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}},
// Timeout must be set, otherwise the client hangs because of an open client writer.
Timeout: shortDuration,
}}
},
server: func(*testing.T) *httptest.Server {
return h2test.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := Accept(w, r)
assert.Error(t, err)
}))
},
wantErr: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
server := tt.server(t)
defer server.Close()
cl := &insecureClient
if tt.client != nil {
cl = tt.client()
}
conn, resp, err := cl.Connect(context.Background(), server.URL)
if tt.wantErr {
assert.Error(t, err)
return
}
require.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
conn.Close()
})
}
}
// TestFormat tests sending JSON and GOB formats over h2conn
func TestFormat(t *testing.T) {
t.Parallel()
server, serverAccepted, serverHandlerWait := startServer()
defer server.Close()
defer close(serverHandlerWait)
clientConn, resp, err := insecureClient.Connect(context.Background(), server.URL)
require.Nil(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
serverConn := <-serverAccepted
serverJSONIn, serverJSONOut := json.NewDecoder(serverConn), json.NewEncoder(serverConn)
clientJSONIn, clientJSONOut := json.NewDecoder(clientConn), json.NewEncoder(clientConn)
serverGOBIn, serverGOBOut := gob.NewDecoder(serverConn), gob.NewEncoder(serverConn)
clientGOBIn, clientGOBOut := gob.NewDecoder(clientConn), gob.NewEncoder(clientConn)
serverConstFormatter := &constLenFormatter{len: 100, rw: serverConn}
clientConstFormatter := &constLenFormatter{len: 100, rw: clientConn}
for i, tt := range []struct {
encoder interface{ Encode(interface{}) error }
decoder interface{ Decode(interface{}) error }
}{
{encoder: serverJSONOut, decoder: clientJSONIn},
{encoder: clientJSONOut, decoder: serverJSONIn},
{encoder: serverGOBOut, decoder: clientGOBIn},
{encoder: clientGOBOut, decoder: serverGOBIn},
{encoder: serverConstFormatter, decoder: clientConstFormatter},
{encoder: clientConstFormatter, decoder: serverConstFormatter},
} {
require.NoError(t, tt.encoder.Encode(i))
var answer int
require.NoError(t, tt.decoder.Decode(&answer))
assert.Equal(t, i, answer)
}
}
func startServer() (server *httptest.Server, serverAccepted <-chan *Conn, serverHandlerWait chan<- struct{}) {
var (
accepted = make(chan *Conn)
handlerWait = make(chan struct{})
)
server = h2test.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
serverConn, err := Accept(w, r)
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
accepted <- serverConn
<-handlerWait
}))
return server, accepted, handlerWait
}
func nopHandler(t *testing.T) *httptest.Server {
return h2test.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := Accept(w, r)
require.NoError(t, err)
}))
}
// constLenFormatter is a simple int encoder/decoder that uses reads and writes with constant size.
type constLenFormatter struct {
len int
rw io.ReadWriter
}
func (f *constLenFormatter) Encode(v interface{}) error {
i, ok := v.(int)
if !ok {
return fmt.Errorf("works only for int")
}
buf := make([]byte, f.len)
binary.LittleEndian.PutUint64(buf, uint64(i))
_, err := f.rw.Write(buf)
return err
}
func (f *constLenFormatter) Decode(v interface{}) error {
i, ok := v.(*int)
if !ok {
return fmt.Errorf("works only for string pointers")
}
buf := make([]byte, f.len)
n, err := f.rw.Read(buf)
if err != nil {
return err
}
*i = int(binary.LittleEndian.Uint64(buf[:n]))
return nil
}