-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetHeaderFunc_test.go
79 lines (68 loc) · 1.84 KB
/
setHeaderFunc_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
package transport_test
import (
"crypto/rand"
"fmt"
"io"
"net/http"
"net/http/httptest"
"sync"
"testing"
"time"
"github.com/go-chi/transport"
"golang.org/x/sync/errgroup"
)
func TestSetHeaderFunc(t *testing.T) {
var uniqueAuth sync.Map
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
auth := r.Header.Get("Authorization")
// Error out if we see the same Authorization header twice.
if _, ok := uniqueAuth.LoadOrStore(auth, true); ok {
w.WriteHeader(401)
fmt.Fprintf(w, "%v", fmt.Errorf("received the same Authorization header twice: %q", auth))
return
}
w.WriteHeader(200)
}))
defer srv.Close()
authClient := http.Client{
Transport: transport.Chain(
http.DefaultTransport,
transport.SetHeaderFunc("Authorization", issueRandomAuthToken),
transport.LogRequests(transport.LogOptions{}),
),
Timeout: 15 * time.Second,
}
req, err := http.NewRequest("GET", srv.URL, nil)
if err != nil {
t.Fatalf("creating request: %v", err)
}
// Send request concurrently.
// Each request should send random Authorization header value.
//
// NOTE: On macOS, 128 might be the max number of parallel connections
// allowed by OS. To increase the limits, you might need to run:
// sudo ulimit -n 6049
// sudo sysctl -w kern.ipc.somaxconn=1024
var g errgroup.Group
for i := 0; i < 128; i++ {
g.Go(func() error {
resp, err := authClient.Do(req)
if err != nil {
return fmt.Errorf("sending auth'd request: %w", err)
}
if resp.StatusCode != 200 {
b, _ := io.ReadAll(resp.Body)
return fmt.Errorf("HTTP %v:\n%s", resp.StatusCode, string(b))
}
return nil
})
}
if err := g.Wait(); err != nil {
t.Fatal(err)
}
}
func issueRandomAuthToken(req *http.Request) string {
b := make([]byte, 32)
rand.Read(b)
return fmt.Sprintf("BEARER %x", b)
}