forked from erigontech/erigon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrpcstack_test.go
More file actions
603 lines (533 loc) · 20.5 KB
/
Copy pathrpcstack_test.go
File metadata and controls
603 lines (533 loc) · 20.5 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
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
// Copyright 2020 The go-ethereum Authors
// (original work)
// Copyright 2024 The Erigon Authors
// (modifications)
// This file is part of Erigon.
//
// Erigon is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Erigon is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Erigon. If not, see <http://www.gnu.org/licenses/>.
package node
import (
"bufio"
"bytes"
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"io"
"math/big"
"net"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
"github.com/coder/websocket"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/erigontech/erigon/common"
"github.com/erigontech/erigon/common/log/v3"
"github.com/erigontech/erigon/common/testlog"
"github.com/erigontech/erigon/rpc"
"github.com/erigontech/erigon/rpc/rpccfg"
)
// TestCorsHandler makes sure CORS are properly handled on the http server.
func TestCorsHandler(t *testing.T) {
url := startHTTPServer(t, nil, []string{"test", "test.com"}, nil)
resp := rpcRequest(t, url, "origin", "test.com")
defer resp.Body.Close()
assert.Equal(t, "test.com", resp.Header.Get("Access-Control-Allow-Origin"))
resp2 := rpcRequest(t, url, "origin", "bad")
defer resp2.Body.Close()
assert.Empty(t, resp2.Header.Get("Access-Control-Allow-Origin"))
}
// TestVhosts makes sure vhosts is properly handled on the http server.
func TestVhosts(t *testing.T) {
url := startHTTPServer(t, nil, nil, []string{"test"})
resp := rpcRequest(t, url, "host", "test")
defer resp.Body.Close()
assert.Equal(t, http.StatusOK, resp.StatusCode)
respUpper := rpcRequest(t, url, "host", "TeSt:1234")
defer respUpper.Body.Close()
assert.Equal(t, http.StatusOK, respUpper.StatusCode)
resp2 := rpcRequest(t, url, "host", "bad")
defer resp2.Body.Close()
assert.Equal(t, http.StatusForbidden, resp2.StatusCode)
}
// TestVhostsAny makes sure vhosts any is properly handled on the http server.
func TestVhostsAny(t *testing.T) {
url := startHTTPServer(t, nil, nil, []string{"any"})
resp := rpcRequest(t, url, "host", "test")
defer resp.Body.Close()
assert.Equal(t, http.StatusOK, resp.StatusCode)
resp2 := rpcRequest(t, url, "host", "bad")
defer resp2.Body.Close()
assert.Equal(t, http.StatusOK, resp.StatusCode)
}
type originTest struct {
spec string
expOk []string
expFail []string
}
// TestWebsocketOrigins makes sure the websocket origins are properly handled on the websocket server.
func TestWebsocketOrigins(t *testing.T) {
tests := []originTest{
{
spec: "*", // allow all
expOk: []string{
"", "http://test", "https://test", "http://test:8540", "https://test:8540",
"http://test.com", "https://foo.test", "http://testa", "http://atestb:8540", "https://atestb:8540",
},
},
{
spec: "test",
expOk: []string{"http://test", "https://test", "http://test:8540", "https://test:8540"},
expFail: []string{"http://test.com", "https://foo.test", "http://testa", "http://atestb:8540", "https://atestb:8540"},
},
// scheme tests
{
spec: "https://test",
expOk: []string{"https://test", "https://test:9999"},
expFail: []string{
"test", // no scheme, required by spec
"http://test", // wrong scheme
"http://test.foo", "https://a.test.x", // subdomain variatoins
"http://testx:8540", "https://xtest:8540",
},
},
// ip tests
{
spec: "https://12.34.56.78",
expOk: []string{"https://12.34.56.78", "https://12.34.56.78:8540"},
expFail: []string{
"http://12.34.56.78", // wrong scheme
"http://12.34.56.78:443", // wrong scheme
"http://1.12.34.56.78", // wrong 'domain name'
"http://12.34.56.78.a", // wrong 'domain name'
"https://87.65.43.21", "http://87.65.43.21:8540", "https://87.65.43.21:8540",
},
},
// port tests
{
spec: "test:8540",
expOk: []string{"http://test:8540", "https://test:8540"},
expFail: []string{
"http://test", "https://test", // spec says port required
"http://test:8541", "https://test:8541", // wrong port
"http://bad", "https://bad", "http://bad:8540", "https://bad:8540",
},
},
// scheme and port
{
spec: "https://test:8540",
expOk: []string{"https://test:8540"},
expFail: []string{
"https://test", // missing port
"http://test", // missing port, + wrong scheme
"http://test:8540", // wrong scheme
"http://test:8541", "https://test:8541", // wrong port
"http://bad", "https://bad", "http://bad:8540", "https://bad:8540",
},
},
// several allowed origins
{
spec: "localhost,http://127.0.0.1",
expOk: []string{
"localhost", "http://localhost", "https://localhost:8443",
"http://127.0.0.1", "http://127.0.0.1:8080",
},
expFail: []string{
"https://127.0.0.1", // wrong scheme
"http://bad", "https://bad", "http://bad:8540", "https://bad:8540",
},
},
}
for _, tc := range tests {
t.Run(tc.spec, func(t *testing.T) {
url := startWSServer(t, common.CliString2Array(tc.spec))
for _, origin := range tc.expOk {
if err := wsRequest(t, url, origin); err != nil {
t.Errorf("spec '%v', origin '%v': expected ok, got %v", tc.spec, origin, err)
}
}
for _, origin := range tc.expFail {
if err := wsRequest(t, url, origin); err == nil {
t.Errorf("spec '%v', origin '%v': expected not to allow, got ok", tc.spec, origin)
}
}
})
}
}
// newTestRPCServer creates a bare rpc.Server like the production wiring in
// cmd/rpcdaemon/cli does.
func newTestRPCServer(t *testing.T) *rpc.Server {
t.Helper()
srv := rpc.NewServer(50, false /* traceRequests */, false /* debugSingleRequest */, true, testlog.Logger(t, log.LvlError), 0)
t.Cleanup(srv.Stop)
return srv
}
// startHTTPServer starts a test HTTP server wrapped in the production handler stack.
func startHTTPServer(t *testing.T, allowList rpc.AllowList, cors, vhosts []string) string {
t.Helper()
srv := newTestRPCServer(t)
srv.SetAllowList(allowList)
ts := httptest.NewServer(NewHTTPHandlerStack(srv, cors, vhosts, false, 1000, true))
t.Cleanup(ts.Close)
return ts.URL
}
// startWSServer starts a test WebSocket server with the production websocket handler.
func startWSServer(t *testing.T, origins []string) string {
t.Helper()
srv := newTestRPCServer(t)
ts := httptest.NewServer(srv.WebsocketHandler(origins, nil, false, testlog.Logger(t, log.LvlError)))
t.Cleanup(ts.Close)
return "ws://" + ts.Listener.Addr().String()
}
// wsRequest attempts to open a WebSocket connection to the given URL.
func wsRequest(t *testing.T, url, browserOrigin string) error {
t.Helper()
t.Logf("checking WebSocket on %s (origin %q)", url, browserOrigin)
headers := make(http.Header)
if browserOrigin != "" {
headers.Set("Origin", browserOrigin)
}
//nolint
conn, resp, err := websocket.Dial(context.Background(), url, &websocket.DialOptions{HTTPHeader: headers})
if err != nil {
if resp != nil {
resp.Body.Close()
}
return err
}
return conn.Close(websocket.StatusNormalClosure, "")
}
func TestAllowList(t *testing.T) {
allowList := rpc.AllowList(map[string]struct{}{"net_version": {}}) // don't allow RPC modules
url := startHTTPServer(t, allowList, nil, nil)
assert.False(t, testCustomRequest(t, url, "rpc_modules"))
}
func testCustomRequest(t *testing.T, url, method string) bool {
body := bytes.NewReader(fmt.Appendf(nil, `{"jsonrpc":"2.0","id":1,"method":"%s"}`, method))
req, _ := http.NewRequestWithContext(t.Context(), "POST", url, body)
req.Header.Set("content-type", "application/json")
client := http.DefaultClient
resp, err := client.Do(req)
if err != nil {
return false
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
require.NoError(t, err)
return !strings.Contains(string(respBody), "error")
}
// rpcRequest performs a JSON-RPC request to the given URL.
func rpcRequest(t *testing.T, url string, extraHeaders ...string) *http.Response {
t.Helper()
// Create the request.
body := bytes.NewReader([]byte(`{"jsonrpc":"2.0","id":1,"method":"rpc_modules","params":[]}`))
req, err := http.NewRequestWithContext(t.Context(), "POST", url, body)
if err != nil {
t.Fatal("could not create http request:", err)
}
req.Header.Set("content-type", "application/json")
// Apply extra headers.
if len(extraHeaders)%2 != 0 {
panic("odd extraHeaders length")
}
for i := 0; i < len(extraHeaders); i += 2 {
key, value := extraHeaders[i], extraHeaders[i+1]
if strings.EqualFold(key, "host") {
req.Host = value
} else {
req.Header.Set(key, value)
}
}
// Perform the request.
t.Logf("checking RPC/HTTP on %s %v", url, extraHeaders)
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
return resp
}
// A batch is answered through the codec, not the stream, so the gzip wrapper must not get a zero Content-Length.
func TestGzipBatchKeepsBody(t *testing.T) {
srv := rpc.NewServer(50, false /* traceRequests */, false /* debugSingleRequest */, false /* disableStreaming */, testlog.Logger(t, log.LvlError), 0)
t.Cleanup(srv.Stop)
ts := httptest.NewServer(NewHTTPHandlerStack(srv, nil, nil, true, 1000, true))
t.Cleanup(ts.Close)
req, err := http.NewRequestWithContext(t.Context(), http.MethodPost, ts.URL, strings.NewReader(`[{"jsonrpc":"2.0","id":1,"method":"rpc_modules"}]`))
require.NoError(t, err)
req.Header.Set("Content-Type", "application/json")
resp, err := ts.Client().Do(req)
require.NoError(t, err)
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.Contains(t, string(body), `"id":1`)
}
// Cleartext HTTP/2 is not served: it is measurably slower than HTTP/1.1 here, geth and reth
// reach it only by prior knowledge and Nethermind refuses it too.
func TestHTTP2CleartextNotServed(t *testing.T) {
srv := newTestRPCServer(t)
handler := NewHTTPHandlerStack(srv, nil, nil, false, 1000, true)
httpSrv, addr, err := StartHTTPEndpoint("tcp://127.0.0.1:0", &HttpEndpointConfig{Timeouts: rpccfg.DefaultHTTPTimeouts}, handler)
require.NoError(t, err)
t.Cleanup(func() { _ = httpSrv.Shutdown(context.Background()) })
transport := &http.Transport{}
transport.Protocols = new(http.Protocols)
transport.Protocols.SetUnencryptedHTTP2(true)
client := &http.Client{Transport: transport}
body := strings.NewReader(`{"jsonrpc":"2.0","id":1,"method":"rpc_modules","params":[]}`)
req, err := http.NewRequestWithContext(t.Context(), "POST", "http://"+addr.String(), body)
require.NoError(t, err)
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req) //nolint:bodyclose
if err == nil {
defer resp.Body.Close()
}
require.Error(t, err, "a client that commits to cleartext HTTP/2 has no HTTP/1.1 to fall back to")
}
// A client offering the RFC 7540 Section 3.2 upgrade — what "curl --http2" sends over cleartext —
// is answered on HTTP/1.1 instead of being switched, so it keeps working.
func TestHTTP1UpgradeToH2CIsIgnored(t *testing.T) {
srv := newTestRPCServer(t)
handler := NewHTTPHandlerStack(srv, nil, nil, false, 1000, true)
httpSrv, addr, err := StartHTTPEndpoint("tcp://127.0.0.1:0", &HttpEndpointConfig{Timeouts: rpccfg.DefaultHTTPTimeouts}, handler)
require.NoError(t, err)
t.Cleanup(func() { _ = httpSrv.Shutdown(context.Background()) })
var dialer net.Dialer
conn, err := dialer.DialContext(t.Context(), "tcp", addr.String())
require.NoError(t, err)
defer conn.Close()
require.NoError(t, conn.SetDeadline(time.Now().Add(10*time.Second)))
_, err = conn.Write([]byte("GET / HTTP/1.1\r\n" +
"Host: " + addr.String() + "\r\n" +
"Connection: Upgrade, HTTP2-Settings\r\n" +
"Upgrade: h2c\r\n" +
"HTTP2-Settings: AAMAAABkAAQAAP__\r\n\r\n"))
require.NoError(t, err)
statusLine, err := bufio.NewReader(conn).ReadString('\n')
require.NoError(t, err)
assert.Equal(t, "HTTP/1.1 200 OK\r\n", statusLine)
}
// TestHTTPSEndpoint checks that the TLS endpoint negotiates HTTP/2 via ALPN and
// still serves clients which only speak HTTP/1.1.
func TestHTTPSEndpoint(t *testing.T) {
certFile, keyFile, roots := newSelfSignedCert(t)
srv := newTestRPCServer(t)
handler := NewHTTPHandlerStack(srv, nil, nil, false, 1000, true)
httpSrv, addr, err := StartHTTPEndpoint("tcp://127.0.0.1:0", &HttpEndpointConfig{
Timeouts: rpccfg.DefaultHTTPTimeouts,
HTTPS: true,
CertFile: certFile,
KeyFile: keyFile,
}, handler)
require.NoError(t, err)
t.Cleanup(func() { _ = httpSrv.Shutdown(context.Background()) })
for _, tc := range []struct {
name string
http2 bool
wantProto string
}{
{name: "h2 via ALPN", http2: true, wantProto: "HTTP/2.0"},
{name: "http/1.1 only", http2: false, wantProto: "HTTP/1.1"},
} {
t.Run(tc.name, func(t *testing.T) {
transport := &http.Transport{
TLSClientConfig: &tls.Config{
MinVersion: tls.VersionTLS12,
RootCAs: roots,
},
}
transport.Protocols = new(http.Protocols)
transport.Protocols.SetHTTP1(!tc.http2)
transport.Protocols.SetHTTP2(tc.http2)
client := &http.Client{Transport: transport, Timeout: 10 * time.Second}
defer client.CloseIdleConnections()
body := strings.NewReader(`{"jsonrpc":"2.0","id":1,"method":"rpc_modules","params":[]}`)
req, err := http.NewRequestWithContext(t.Context(), "POST", "https://"+addr.String(), body)
require.NoError(t, err)
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
require.NoError(t, err)
defer resp.Body.Close()
assert.Equal(t, tc.wantProto, resp.Proto)
assert.Equal(t, http.StatusOK, resp.StatusCode)
result, err := io.ReadAll(resp.Body)
require.NoError(t, err)
assert.Contains(t, string(result), "jsonrpc", "expected JSON-RPC response")
})
}
}
func newSelfSignedCert(t *testing.T) (certFile, keyFile string, roots *x509.CertPool) {
t.Helper()
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
require.NoError(t, err)
template := x509.Certificate{
SerialNumber: big.NewInt(1),
Subject: pkix.Name{CommonName: "erigon-test"},
NotBefore: time.Now().Add(-time.Hour),
NotAfter: time.Now().Add(time.Hour),
KeyUsage: x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
IPAddresses: []net.IP{net.IPv4(127, 0, 0, 1)},
}
der, err := x509.CreateCertificate(rand.Reader, &template, &template, &key.PublicKey, key)
require.NoError(t, err)
keyDER, err := x509.MarshalECPrivateKey(key)
require.NoError(t, err)
cert, err := x509.ParseCertificate(der)
require.NoError(t, err)
roots = x509.NewCertPool()
roots.AddCert(cert)
dir := t.TempDir()
certFile = filepath.Join(dir, "cert.pem")
keyFile = filepath.Join(dir, "key.pem")
require.NoError(t, os.WriteFile(certFile, pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der}), 0o600))
require.NoError(t, os.WriteFile(keyFile, pem.EncodeToMemory(&pem.Block{Type: "EC PRIVATE KEY", Bytes: keyDER}), 0o600))
return certFile, keyFile, roots
}
// TestRPCAdmissionHandler verifies that rpcAdmissionHandler correctly limits
// concurrent requests and returns HTTP 503 when the limit is exceeded.
func TestRPCAdmissionHandler(t *testing.T) {
okHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
t.Run("disabled when limit is zero", func(t *testing.T) {
h := newRPCAdmissionHandler(0, okHandler)
rec := httptest.NewRecorder()
h.ServeHTTP(rec, httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/", nil))
assert.Equal(t, http.StatusOK, rec.Code)
})
t.Run("allows requests under the limit", func(t *testing.T) {
h := newRPCAdmissionHandler(5, okHandler)
rec := httptest.NewRecorder()
h.ServeHTTP(rec, httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/", nil))
assert.Equal(t, http.StatusOK, rec.Code)
})
t.Run("returns 503 when limit is exceeded", func(t *testing.T) {
// Use a gate channel to hold inflight requests open long enough to
// trigger the limit.
gate := make(chan struct{})
blockingHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
<-gate
w.WriteHeader(http.StatusOK)
})
const limit = 2
h := newRPCAdmissionHandler(limit, blockingHandler)
var wg sync.WaitGroup
for range limit {
wg.Go(func() {
h.ServeHTTP(httptest.NewRecorder(), httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/", nil))
})
}
// Give goroutines time to enter the handler and increment the counter.
// We busy-wait on the inflight counter rather than sleeping.
admission := h.(*rpcAdmissionHandler)
for admission.inflight.Load() < limit {
// spin
}
// Now the limit is reached — next request must be rejected.
rec := httptest.NewRecorder()
h.ServeHTTP(rec, httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/", nil))
assert.Equal(t, http.StatusServiceUnavailable, rec.Code)
// Release the held requests.
close(gate)
wg.Wait()
})
}
// TestWsConnectionLimit verifies that the WebSocket connection limiter rejects
// excess WebSocket connections with HTTP 503 and allows new ones once a slot
// is freed.
func TestWsConnectionLimit(t *testing.T) {
const limit = 1
srv := newTestRPCServer(t)
limiter := NewWSConnectionLimiter(limit, srv.WebsocketHandler([]string{"*"}, nil, false, testlog.Logger(t, log.LvlError)))
wsLimiter := limiter.(*wsConnectionLimiter)
ts := httptest.NewServer(limiter)
t.Cleanup(ts.Close)
url := "ws://" + ts.Listener.Addr().String()
// First connection should succeed.
conn1, resp1, err := websocket.Dial(context.Background(), url, nil)
if err != nil && resp1 != nil {
resp1.Body.Close()
}
require.NoError(t, err, "first connection should succeed")
// Wait until the server increments its counter.
require.Eventually(t, func() bool { return wsLimiter.count.Load() == 1 },
5*time.Second, 5*time.Millisecond)
// While first connection is open the second must be rejected.
_, resp, err2 := websocket.Dial(context.Background(), url, nil)
require.Error(t, err2, "second connection should be rejected")
if resp != nil {
assert.Equal(t, http.StatusServiceUnavailable, resp.StatusCode)
resp.Body.Close()
}
// Close first connection and wait for the counter to drop.
require.NoError(t, conn1.Close(websocket.StatusNormalClosure, ""))
require.Eventually(t, func() bool { return wsLimiter.count.Load() == 0 },
5*time.Second, 5*time.Millisecond)
// A new connection should now succeed.
conn3, resp3, err := websocket.Dial(context.Background(), url, nil)
if err != nil && resp3 != nil {
resp3.Body.Close()
}
require.NoError(t, err, "connection after limit released should succeed")
require.NoError(t, conn3.Close(websocket.StatusNormalClosure, ""))
}
// TestNewWSConnectionLimiter tests the standalone NewWSConnectionLimiter handler.
func TestNewWSConnectionLimiter(t *testing.T) {
// limit=0: handler passes through without restriction.
passthrough := NewWSConnectionLimiter(0, http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
}))
rr0 := httptest.NewRecorder()
passthrough.ServeHTTP(rr0, httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/", nil))
assert.Equal(t, http.StatusOK, rr0.Code)
// Build a limiter with limit=1.
// Use a channel to keep "connections" open until the test releases them.
hold := make(chan struct{})
slow := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
<-hold
w.WriteHeader(http.StatusOK)
})
limiter := NewWSConnectionLimiter(1, slow)
// First request: should be accepted (blocks on hold).
go func() {
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/", nil)
limiter.ServeHTTP(httptest.NewRecorder(), req)
}()
// Give the goroutine time to increment the counter.
require.Eventually(t, func() bool {
return limiter.(*wsConnectionLimiter).count.Load() == 1
}, 2*time.Second, time.Millisecond)
// Second request: should be rejected with 503.
rr := httptest.NewRecorder()
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/", nil)
limiter.ServeHTTP(rr, req)
assert.Equal(t, http.StatusServiceUnavailable, rr.Code)
// Release the first "connection".
close(hold)
require.Eventually(t, func() bool {
return limiter.(*wsConnectionLimiter).count.Load() == 0
}, 2*time.Second, time.Millisecond)
}