/
fixture.go
109 lines (87 loc) · 2.26 KB
/
fixture.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
package scenariostest
import (
"bytes"
"context"
"io"
"net"
"net/http"
"os"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/require"
)
// Set this environment variable to skip slow tests
const HTTPX_FAST = "HTTPX_FAST"
func isFastEnvironment(t testing.TB) bool {
if testing.Short() {
return true
}
if _, exists := os.LookupEnv(HTTPX_FAST); exists {
return true
}
return false
}
func skipForFastEnvironment(t testing.TB) {
t.Helper()
if isFastEnvironment(t) {
t.Skip("skipped slow test")
}
}
type TestContext struct {
testing.TB
}
// New creates a TestContext instance.
func New(t testing.TB) *TestContext {
return &TestContext{TB: t}
}
// ListenTCP listens on the local TCP network with random port and returns the address.
// The connection will be closed when the test is finished.
func (tc *TestContext) ListenTCP() net.Listener {
tc.Helper()
lis, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(tc, err, "ListenTCP failed")
tc.Cleanup(func() {
_ = lis.Close()
})
addr := lis.Addr().String()
tc.Logf("ListenTCP: %q", addr)
return lis
}
func (tc *TestContext) CreateFastCheckContext() (context.Context, context.CancelFunc) {
return context.WithTimeout(context.Background(), 5*time.Second)
}
func (tc *TestContext) ExpectToServeResponse(
ctx context.Context,
addr string,
checkResp func(tc *TestContext, resp *http.Response, body []byte),
) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://"+addr, nil)
require.NoError(tc, err, "NewRequestWithContext failed")
resp, err := http.DefaultClient.Do(req)
require.NoError(tc, err, "send request")
var body []byte
if resp.Body != nil {
var readErr error
body, readErr = io.ReadAll(resp.Body)
require.NoError(tc, readErr, "read response body")
require.NoError(tc, resp.Body.Close(), "close response body")
resp.Body = io.NopCloser(bytes.NewBuffer(body))
}
checkResp(tc, resp, body)
}
type ConnStateCounter struct {
CountStateNew atomic.Int64
CountActiveState atomic.Int64
CountClosedState atomic.Int64
}
func (c *ConnStateCounter) Count(conn net.Conn, state http.ConnState) {
switch state {
case http.StateNew:
c.CountStateNew.Add(1)
case http.StateActive:
c.CountActiveState.Add(1)
case http.StateClosed:
c.CountClosedState.Add(1)
}
}