Skip to content

Commit 93d0eff

Browse files
committed
tests: implement example tests for Listener and Conn
1 parent 10a7983 commit 93d0eff

File tree

2 files changed

+228
-0
lines changed

2 files changed

+228
-0
lines changed

example_conn_test.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package proxyproto_test
2+
3+
import (
4+
"net"
5+
"time"
6+
7+
"github.com/pires/go-proxyproto"
8+
)
9+
10+
func ExampleNewConn_default() {
11+
serverConn, clientConn := net.Pipe()
12+
defer serverConn.Close()
13+
defer clientConn.Close()
14+
15+
go func() {
16+
_, _ = clientConn.Write([]byte("x"))
17+
_ = clientConn.Close()
18+
}()
19+
20+
conn := proxyproto.NewConn(serverConn)
21+
buf := make([]byte, 1)
22+
_, _ = conn.Read(buf)
23+
// Output:
24+
}
25+
26+
func ExampleNewConn_withBufferSize() {
27+
serverConn, clientConn := net.Pipe()
28+
defer serverConn.Close()
29+
defer clientConn.Close()
30+
31+
go func() {
32+
_, _ = clientConn.Write([]byte("y"))
33+
_ = clientConn.Close()
34+
}()
35+
36+
conn := proxyproto.NewConn(serverConn, proxyproto.WithBufferSize(4096))
37+
buf := make([]byte, 1)
38+
_, _ = conn.Read(buf)
39+
// Output:
40+
}
41+
42+
func ExampleNewConn_withReadHeaderTimeout() {
43+
serverConn, clientConn := net.Pipe()
44+
defer serverConn.Close()
45+
defer clientConn.Close()
46+
47+
go func() {
48+
_, _ = clientConn.Write([]byte("z"))
49+
_ = clientConn.Close()
50+
}()
51+
52+
conn := proxyproto.NewConn(serverConn, proxyproto.SetReadHeaderTimeout(time.Second))
53+
buf := make([]byte, 1)
54+
_, _ = conn.Read(buf)
55+
// Output:
56+
}
57+
58+
func ExampleNewConn_withPolicy() {
59+
serverConn, clientConn := net.Pipe()
60+
defer serverConn.Close()
61+
defer clientConn.Close()
62+
63+
go func() {
64+
_, _ = clientConn.Write([]byte(proxyV1Line))
65+
_, _ = clientConn.Write([]byte("p"))
66+
_ = clientConn.Close()
67+
}()
68+
69+
conn := proxyproto.NewConn(serverConn, proxyproto.WithPolicy(proxyproto.REQUIRE))
70+
buf := make([]byte, 1)
71+
_, _ = conn.Read(buf)
72+
// Output:
73+
}
74+
75+
func ExampleNewConn_combined() {
76+
serverConn, clientConn := net.Pipe()
77+
defer serverConn.Close()
78+
defer clientConn.Close()
79+
80+
go func() {
81+
_, _ = clientConn.Write([]byte("c"))
82+
_ = clientConn.Close()
83+
}()
84+
85+
conn := proxyproto.NewConn(serverConn,
86+
proxyproto.WithBufferSize(2048),
87+
proxyproto.SetReadHeaderTimeout(2*time.Second),
88+
)
89+
buf := make([]byte, 1)
90+
_, _ = conn.Read(buf)
91+
// Output:
92+
}

example_listener_test.go

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package proxyproto_test
2+
3+
import (
4+
"net"
5+
"time"
6+
7+
"github.com/pires/go-proxyproto"
8+
)
9+
10+
// proxyV1Line is a minimal PROXY protocol v1 header for examples.
11+
const proxyV1Line = "PROXY TCP4 192.168.1.1 192.168.1.2 12345 443\r\n"
12+
13+
func ExampleListener_default() {
14+
l, _ := net.Listen("tcp", "127.0.0.1:0")
15+
pl := &proxyproto.Listener{Listener: l}
16+
defer pl.Close()
17+
18+
go func() {
19+
c, _ := net.Dial("tcp", pl.Addr().String())
20+
if c != nil {
21+
_, _ = c.Write([]byte("x"))
22+
_ = c.Close()
23+
}
24+
}()
25+
26+
conn, _ := pl.Accept()
27+
if conn != nil {
28+
buf := make([]byte, 1)
29+
_, _ = conn.Read(buf)
30+
_ = conn.Close()
31+
}
32+
// Output:
33+
}
34+
35+
func ExampleListener_readHeaderTimeout() {
36+
l, _ := net.Listen("tcp", "127.0.0.1:0")
37+
pl := &proxyproto.Listener{
38+
Listener: l,
39+
ReadHeaderTimeout: 2 * time.Second,
40+
}
41+
defer pl.Close()
42+
43+
go func() {
44+
c, _ := net.Dial("tcp", pl.Addr().String())
45+
if c != nil {
46+
_, _ = c.Write([]byte("a"))
47+
_ = c.Close()
48+
}
49+
}()
50+
51+
conn, _ := pl.Accept()
52+
if conn != nil {
53+
_ = conn.SetReadDeadline(time.Now().Add(time.Second))
54+
buf := make([]byte, 1)
55+
_, _ = conn.Read(buf)
56+
_ = conn.Close()
57+
}
58+
// Output:
59+
}
60+
61+
func ExampleListener_readBufferSize() {
62+
l, _ := net.Listen("tcp", "127.0.0.1:0")
63+
pl := &proxyproto.Listener{
64+
Listener: l,
65+
ReadBufferSize: 4096,
66+
}
67+
defer pl.Close()
68+
69+
go func() {
70+
c, _ := net.Dial("tcp", pl.Addr().String())
71+
if c != nil {
72+
_, _ = c.Write([]byte("b"))
73+
_ = c.Close()
74+
}
75+
}()
76+
77+
conn, _ := pl.Accept()
78+
if conn != nil {
79+
buf := make([]byte, 1)
80+
_, _ = conn.Read(buf)
81+
_ = conn.Close()
82+
}
83+
// Output:
84+
}
85+
86+
func ExampleListener_policyRequire() {
87+
l, _ := net.Listen("tcp", "127.0.0.1:0")
88+
pl := &proxyproto.Listener{
89+
Listener: l,
90+
Policy: func(net.Addr) (proxyproto.Policy, error) { return proxyproto.REQUIRE, nil },
91+
}
92+
defer pl.Close()
93+
94+
go func() {
95+
c, _ := net.Dial("tcp", pl.Addr().String())
96+
if c != nil {
97+
_, _ = c.Write([]byte(proxyV1Line))
98+
_, _ = c.Write([]byte("p"))
99+
_ = c.Close()
100+
}
101+
}()
102+
103+
conn, _ := pl.Accept()
104+
if conn != nil {
105+
buf := make([]byte, 1)
106+
_, _ = conn.Read(buf)
107+
_ = conn.Close()
108+
}
109+
// Output:
110+
}
111+
112+
func ExampleListener_validateHeader() {
113+
l, _ := net.Listen("tcp", "127.0.0.1:0")
114+
pl := &proxyproto.Listener{
115+
Listener: l,
116+
ValidateHeader: func(*proxyproto.Header) error { return nil },
117+
}
118+
defer pl.Close()
119+
120+
go func() {
121+
c, _ := net.Dial("tcp", pl.Addr().String())
122+
if c != nil {
123+
_, _ = c.Write([]byte(proxyV1Line))
124+
_, _ = c.Write([]byte("v"))
125+
_ = c.Close()
126+
}
127+
}()
128+
129+
conn, _ := pl.Accept()
130+
if conn != nil {
131+
buf := make([]byte, 1)
132+
_, _ = conn.Read(buf)
133+
_ = conn.Close()
134+
}
135+
// Output:
136+
}

0 commit comments

Comments
 (0)