-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy_test.go
53 lines (48 loc) · 939 Bytes
/
proxy_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
package main
import (
"fmt"
"net"
"testing"
)
func TestForward(t *testing.T) {
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
backend := ln.Addr().String()
t.Log(backend)
acceptCh := make(chan net.Conn, 1)
errCh := make(chan error, 1)
backendDone := make(chan struct{})
go func() {
defer close(backendDone)
conn, err := ln.Accept()
if err != nil {
errCh <- err
}
acceptCh <- conn
}()
frontend, conn := net.Pipe()
frontendDone := make(chan struct{})
go func() {
defer close(frontendDone)
errCh <- forward(conn, backend)
}()
select {
case backendConn := <-acceptCh:
fmt.Fprint(frontend, "hello world")
b := make([]byte, 64)
n, err := backendConn.Read(b)
if err != nil {
t.Fatal(err)
}
if string(b[:n]) != "hello world" {
t.Error("forwarded bytes mismatch")
}
case err := <-errCh:
t.Fatal(err)
}
frontend.Close()
<-frontendDone
<-backendDone
}