Skip to content

Commit 26f8927

Browse files
committed
Extract a test helper to correct front -> back msg passing
1 parent 7c3486f commit 26f8927

File tree

1 file changed

+26
-44
lines changed

1 file changed

+26
-44
lines changed

tcpproxy_test.go

Lines changed: 26 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,26 @@ func testProxy(t *testing.T, front net.Listener) *Proxy {
169169
}
170170
}
171171

172+
func testRouteToBackendWithExpected(t *testing.T, front net.Conn, back net.Listener, msg string, expected string) {
173+
io.WriteString(front, msg)
174+
fromProxy, err := back.Accept()
175+
if err != nil {
176+
t.Fatal(err)
177+
}
178+
179+
buf := make([]byte, len(msg))
180+
if _, err := io.ReadFull(fromProxy, buf); err != nil {
181+
t.Fatal(err)
182+
}
183+
if string(buf) != expected {
184+
t.Fatalf("got %q; want %q", buf, expected)
185+
}
186+
}
187+
188+
func testRouteToBackend(t *testing.T, front net.Conn, back net.Listener, msg string) {
189+
testRouteToBackendWithExpected(t, front, back, msg, msg)
190+
}
191+
172192
func TestProxyAlwaysMatch(t *testing.T) {
173193
front := newLocalListener(t)
174194
defer front.Close()
@@ -187,20 +207,7 @@ func TestProxyAlwaysMatch(t *testing.T) {
187207
}
188208
defer toFront.Close()
189209

190-
fromProxy, err := back.Accept()
191-
if err != nil {
192-
t.Fatal(err)
193-
}
194-
const msg = "message"
195-
io.WriteString(toFront, msg)
196-
197-
buf := make([]byte, len(msg))
198-
if _, err := io.ReadFull(fromProxy, buf); err != nil {
199-
t.Fatal(err)
200-
}
201-
if string(buf) != msg {
202-
t.Fatalf("got %q; want %q", buf, msg)
203-
}
210+
testRouteToBackend(t, toFront, back, "message")
204211
}
205212

206213
func TestProxyHTTP(t *testing.T) {
@@ -226,20 +233,7 @@ func TestProxyHTTP(t *testing.T) {
226233
defer toFront.Close()
227234

228235
const msg = "GET / HTTP/1.1\r\nHost: bar.com\r\n\r\n"
229-
io.WriteString(toFront, msg)
230-
231-
fromProxy, err := backBar.Accept()
232-
if err != nil {
233-
t.Fatal(err)
234-
}
235-
236-
buf := make([]byte, len(msg))
237-
if _, err := io.ReadFull(fromProxy, buf); err != nil {
238-
t.Fatal(err)
239-
}
240-
if string(buf) != msg {
241-
t.Fatalf("got %q; want %q", buf, msg)
242-
}
236+
testRouteToBackend(t, toFront, backBar, msg)
243237
}
244238

245239
func TestProxySNI(t *testing.T) {
@@ -264,6 +258,9 @@ func TestProxySNI(t *testing.T) {
264258
}
265259
defer toFront.Close()
266260

261+
msg := clientHelloRecord(t, "bar.com")
262+
testRouteToBackend(t, toFront, backBar, msg)
263+
}
267264
msg := clientHelloRecord(t, "bar.com")
268265
io.WriteString(toFront, msg)
269266

@@ -301,23 +298,8 @@ func TestProxyPROXYOut(t *testing.T) {
301298
t.Fatal(err)
302299
}
303300

304-
io.WriteString(toFront, "foo")
305-
toFront.Close()
306-
307-
fromProxy, err := back.Accept()
308-
if err != nil {
309-
t.Fatal(err)
310-
}
311-
312-
bs, err := ioutil.ReadAll(fromProxy)
313-
if err != nil {
314-
t.Fatal(err)
315-
}
316-
317301
want := fmt.Sprintf("PROXY TCP4 %s %d %s %d\r\nfoo", toFront.LocalAddr().(*net.TCPAddr).IP, toFront.LocalAddr().(*net.TCPAddr).Port, toFront.RemoteAddr().(*net.TCPAddr).IP, toFront.RemoteAddr().(*net.TCPAddr).Port)
318-
if string(bs) != want {
319-
t.Fatalf("got %q; want %q", bs, want)
320-
}
302+
testRouteToBackendWithExpected(t, toFront, back, "foo", want)
321303
}
322304

323305
type tlsServer struct {

0 commit comments

Comments
 (0)