forked from smarty/cproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefault_proxy_test.go
103 lines (85 loc) · 2.2 KB
/
default_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
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
package cproxy
import (
"bytes"
"net"
"testing"
"github.com/smartystreets/assertions/should"
"github.com/smartystreets/gunit"
)
func TestProxyFixture(t *testing.T) {
gunit.Run(new(ProxyFixture), t)
}
type ProxyFixture struct {
*gunit.Fixture
client *TestSocket
server *TestSocket
proxy *defaultProxy
}
func (this *ProxyFixture) Setup() {
this.client = NewTestSocket()
this.server = NewTestSocket()
this.proxy = newProxy(this.client, this.server)
}
func (this *ProxyFixture) TestProxyCopiesAndCloses() {
this.client.readBuffer.WriteString("from client")
this.server.readBuffer.WriteString("from server")
this.proxy.Proxy()
this.So(this.client.writeBuffer.String(), should.Equal, "from server")
this.So(this.server.writeBuffer.String(), should.Equal, "from client")
this.So(this.client.closeRead, should.Equal, 1)
this.So(this.server.closeWrite, should.Equal, 1)
this.So(this.client.closeWrite, should.Equal, 1)
this.So(this.server.closeRead, should.Equal, 1)
this.So(this.server.close, should.Equal, 1)
this.So(this.client.close, should.Equal, 1)
}
//////////////////////////////////////////////////////////////
type TestSocket struct {
readBuffer *bytes.Buffer
writeBuffer *bytes.Buffer
reads int
writes int
closeRead int
closeWrite int
close int
address string
port int
}
func NewTestSocket() *TestSocket {
return &TestSocket{
readBuffer: bytes.NewBufferString(""),
writeBuffer: bytes.NewBufferString(""),
}
}
func (this *TestSocket) Close() error {
this.close++
return nil
}
func (this *TestSocket) CloseRead() error {
this.closeRead++
return nil
}
func (this *TestSocket) CloseWrite() error {
this.closeWrite++
return nil
}
func (this *TestSocket) Read(value []byte) (int, error) {
this.reads++
if this.close > 0 || this.closeRead > 0 {
panic("Can't read from closed socket")
}
return this.readBuffer.Read(value)
}
func (this *TestSocket) Write(value []byte) (int, error) {
this.writes++
if this.close > 0 || this.closeWrite > 0 {
panic("Can't write to closed socket")
}
return this.writeBuffer.Write(value)
}
func (this *TestSocket) RemoteAddr() net.Addr {
return &net.TCPAddr{
IP: net.ParseIP(this.address),
Port: this.port,
}
}