Repository navigation
Expand file tree
/
Copy pathhttpserver_test.go
More file actions
79 lines (64 loc) · 1.85 KB
/
Copy pathhttpserver_test.go
File metadata and controls
79 lines (64 loc) · 1.85 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
package waf_test
import (
"bytes"
"context"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gitlab.com/tozd/waf"
)
func TestNewHTTPServer(t *testing.T) {
t.Parallel()
server := waf.NewHTTPServer(zerolog.Nop(), nil)
assert.Empty(t, server.Addr)
assert.Nil(t, server.TLSConfig)
assert.Positive(t, server.ReadHeaderTimeout)
assert.Positive(t, server.IdleTimeout)
}
func TestNewHTTPServerErrorLog(t *testing.T) {
t.Parallel()
out := &bytes.Buffer{}
server := waf.NewHTTPServer(zerolog.New(out), nil)
server.ErrorLog.Print("failed")
assert.JSONEq(t, `{"message":"failed"}`, out.String())
}
func TestNewHTTPServerConnectionID(t *testing.T) {
t.Parallel()
ts := httptest.NewUnstartedServer(nil)
ts.Config = waf.NewHTTPServer(zerolog.Nop(), http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
_, _ = io.WriteString(w, waf.MustConnectionID(req.Context()).String())
}))
ts.Start()
t.Cleanup(ts.Close)
get := func(client *http.Client) string {
t.Helper()
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, ts.URL, nil)
require.NoError(t, err)
res, err := client.Do(req)
require.NoError(t, err)
defer res.Body.Close() //nolint:errcheck
body, err := io.ReadAll(res.Body)
require.NoError(t, err)
return string(body)
}
// Requests made over the same connection have the same identifier.
client := ts.Client()
first := get(client)
assert.NotEmpty(t, first)
assert.Equal(t, first, get(client))
// And a new connection has a new one.
client.CloseIdleConnections()
assert.NotEqual(t, first, get(client))
}
func TestConnectionIDMissing(t *testing.T) {
t.Parallel()
_, ok := waf.ConnectionID(context.Background())
assert.False(t, ok)
assert.Panics(t, func() {
waf.MustConnectionID(context.Background())
})
}