-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_test.go
More file actions
175 lines (144 loc) · 4.14 KB
/
server_test.go
File metadata and controls
175 lines (144 loc) · 4.14 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
package httpproxy
import (
"bytes"
"context"
"errors"
"io"
"log/slog"
"net"
"net/http"
"net/http/httptest"
"strconv"
"testing"
)
type failMakeRoundTripper struct{}
func (failMakeRoundTripper) MakeRoundTripper(cd ContextDialer) (rt http.RoundTripper) {
return fakeRoundTripper{errors.New("failMakeRoundTripper")}
}
func TestMakeRoundTripper(t *testing.T) {
destsrv := makeHTTPDestSrv(t)
defer destsrv.Close()
proxysrv := httptest.NewServer(&Server{
Logger: slog.Default(),
RoundTripperMaker: failMakeRoundTripper{},
DialerSelector: nil,
})
defer proxysrv.Close()
resp, err := makeClient(t, proxysrv.URL).Get(destsrv.URL)
maybeFatal(t, err)
if resp.StatusCode != http.StatusInternalServerError {
t.Error(resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
maybeFatal(t, err)
maybeFatal(t, resp.Body.Close())
if resp.ContentLength != int64(len(body)) {
t.Error("ContentLength", resp.ContentLength, "len(body)", len(body))
}
if string(body) != "failMakeRoundTripper" {
t.Error(string(body))
}
}
type failSelectDialer struct{}
func (failSelectDialer) SelectDialer(username, network, address string) (cd ContextDialer, err error) {
return nil, errors.New("failSelectDialer")
}
func TestSelectDialer(t *testing.T) {
destsrv := makeHTTPDestSrv(t)
defer destsrv.Close()
proxysrv := httptest.NewServer(&Server{
Logger: slog.Default(),
DialerSelector: failSelectDialer{},
})
defer proxysrv.Close()
resp, err := makeClient(t, proxysrv.URL).Get(destsrv.URL)
maybeFatal(t, err)
if resp.StatusCode != http.StatusInternalServerError {
t.Error(resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
maybeFatal(t, err)
maybeFatal(t, resp.Body.Close())
if resp.ContentLength != int64(len(body)) {
t.Error("ContentLength", resp.ContentLength, "len(body)", len(body))
}
if string(body) != "failSelectDialer" {
t.Error(string(body))
}
}
type testContextDialer string
func (testContextDialer) SelectDialer(username, network, address string) (cd ContextDialer, err error) {
return testContextDialer(address), nil
}
func (testContextDialer) DialContext(ctx context.Context, network, address string) (conn net.Conn, err error) {
return DefaultContextDialer.DialContext(ctx, network, address)
}
func TestRoundTripperCache(t *testing.T) {
srv := &Server{
Logger: slog.Default(),
DialerSelector: testContextDialer(""),
}
m := map[int]http.RoundTripper{}
for i := range MaxCachedRoundTrippers {
m[i] = srv.ensureTripper(testContextDialer(strconv.Itoa(i)))
}
if len(srv.trippers) != MaxCachedRoundTrippers {
t.Fatal(len(srv.trippers))
}
for i := range MaxCachedRoundTrippers {
if m[i] != srv.ensureTripper(testContextDialer(strconv.Itoa(i))) {
t.Fatal(i)
}
if i > 0 {
if m[i-1] == m[i] {
t.Fatal(i)
}
}
}
cd := testContextDialer(strconv.Itoa(MaxCachedRoundTrippers))
if _, ok := srv.trippers[cd]; ok {
t.Error("tripper MaxCachedRoundTrippers was in cache")
}
_ = srv.ensureTripper(cd) // cache overflows and cleans old entries
if _, ok := srv.trippers[cd]; !ok {
t.Error("tripper MaxCachedRoundTrippers should have been in cache")
}
cd = testContextDialer(strconv.Itoa(MaxCachedRoundTrippers - 1))
if _, ok := srv.trippers[cd]; !ok {
t.Error("tripper MaxCachedRoundTrippers-1 should have been in cache")
}
}
func TestMuxer(t *testing.T) {
var homebody = []byte("home page")
destsrv := makeHTTPDestSrv(t)
defer destsrv.Close()
mux := http.NewServeMux()
mux.HandleFunc("/{$}", func(w http.ResponseWriter, r *http.Request) {
w.Write(homebody)
})
srv := &Server{}
proxysrv := httptest.NewServer(srv)
defer proxysrv.Close()
client := makeClient(t, proxysrv.URL)
resp, err := client.Get(proxysrv.URL)
maybeFatal(t, err)
resp.Body.Close()
if resp.StatusCode != http.StatusNotFound {
t.Error(resp.StatusCode)
}
resp, err = client.Get(destsrv.URL)
maybeFatal(t, err)
body, _ := io.ReadAll(resp.Body)
resp.Body.Close()
if !bytes.Equal(body, testBody) {
t.Errorf("%q", body)
}
srv.Handler = mux
resp, err = client.Get(proxysrv.URL)
maybeFatal(t, err)
body, _ = io.ReadAll(resp.Body)
resp.Body.Close()
if !bytes.Equal(body, homebody) {
t.Errorf("%q", body)
}
}