forked from ooni/probe-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp3_test.go
140 lines (131 loc) · 3.75 KB
/
http3_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
package netxlite
import (
"crypto/tls"
"errors"
"net/http"
"testing"
"github.com/lucas-clemente/quic-go/http3"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/model/mocks"
nlmocks "github.com/ooni/probe-cli/v3/internal/netxlite/mocks"
)
func TestHTTP3Transport(t *testing.T) {
t.Run("CloseIdleConnections", func(t *testing.T) {
var (
calledHTTP3 bool
calledDialer bool
)
txp := &http3Transport{
child: &nlmocks.HTTP3RoundTripper{
MockClose: func() error {
calledHTTP3 = true
return nil
},
},
dialer: &mocks.QUICDialer{
MockCloseIdleConnections: func() {
calledDialer = true
},
},
}
txp.CloseIdleConnections()
if !calledHTTP3 || !calledDialer {
t.Fatal("not called")
}
})
t.Run("Network", func(t *testing.T) {
txp := &http3Transport{}
if txp.Network() != "udp" {
t.Fatal("unexpected .Network return value")
}
})
t.Run("RoundTrip", func(t *testing.T) {
expected := errors.New("mocked error")
txp := &http3Transport{
child: &nlmocks.HTTP3RoundTripper{
MockRoundTrip: func(req *http.Request) (*http.Response, error) {
return nil, expected
},
},
}
resp, err := txp.RoundTrip(&http.Request{})
if !errors.Is(err, expected) {
t.Fatal("unexpected err", err)
}
if resp != nil {
t.Fatal("unexpected resp")
}
})
}
// verifyTypeChainForHTTP3 helps to verify type chains for HTTP3.
//
// Arguments:
//
// - t is the MANDATORY testing ref;
//
// - txp is the MANDATORY HTTP transport to verify;
//
// - underlyingLogger is the MANDATORY logger we expect to find;
//
// - qd is the OPTIONAL QUIC dialer: if not nil, we expect to
// see this value as the QUIC dialer, otherwise we will check the
// type chain of the real dialer;
//
// - config is the MANDATORY TLS config: we'll always check
// whether the TLSClientConfig is equal to this value: passing
// nil here means we expect to see nil in the object;
//
// - reso is the OPTIONAL resolver: if present and the qd is
// nil, we'll unwrap the QUIC dialer and check whether we have
// this resolver as the underlying resolver.
func verifyTypeChainForHTTP3(t *testing.T, txp model.HTTPTransport,
underlyingLogger model.DebugLogger, qd model.QUICDialer,
config *tls.Config, reso model.Resolver) {
logger := txp.(*httpTransportLogger)
if logger.Logger != underlyingLogger {
t.Fatal("invalid logger")
}
ew := logger.HTTPTransport.(*httpTransportErrWrapper)
h3txp := ew.HTTPTransport.(*http3Transport)
if qd != nil && h3txp.dialer != qd {
t.Fatal("invalid dialer")
}
if qd == nil {
qdlog := h3txp.dialer.(*quicDialerLogger)
qdr := qdlog.Dialer.(*quicDialerResolver)
if reso != nil && qdr.Resolver != reso {
t.Fatal("invalid resolver")
}
}
h3 := h3txp.child.(*http3.RoundTripper)
if h3.Dial == nil {
t.Fatal("invalid Dial")
}
if !h3.DisableCompression {
t.Fatal("invalid DisableCompression")
}
if h3.TLSClientConfig != config {
t.Fatal("invalid TLSClientConfig")
}
}
func TestNewHTTP3Transport(t *testing.T) {
t.Run("creates the correct type chain", func(t *testing.T) {
qd := &mocks.QUICDialer{}
config := &tls.Config{}
txp := NewHTTP3Transport(model.DiscardLogger, qd, config)
verifyTypeChainForHTTP3(t, txp, model.DiscardLogger, qd, config, nil)
})
}
func TestNewHTTP3TransportStdlib(t *testing.T) {
t.Run("creates the correct type chain", func(t *testing.T) {
txp := NewHTTP3TransportStdlib(model.DiscardLogger)
verifyTypeChainForHTTP3(t, txp, model.DiscardLogger, nil, nil, nil)
})
}
func TestNewHTTP3TransportWithResolver(t *testing.T) {
t.Run("creates the correct type chain", func(t *testing.T) {
reso := &mocks.Resolver{}
txp := NewHTTP3TransportWithResolver(model.DiscardLogger, reso)
verifyTypeChainForHTTP3(t, txp, model.DiscardLogger, nil, nil, reso)
})
}