forked from smarty/cproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
default_server_connector_test.go
102 lines (77 loc) · 2.58 KB
/
default_server_connector_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
package cproxy
import (
"testing"
"github.com/smartystreets/assertions/should"
"github.com/smartystreets/gunit"
)
func TestServerConnectorFixture(t *testing.T) {
gunit.Run(new(ServerConnectorFixture), t)
}
type ServerConnectorFixture struct {
*gunit.Fixture
clientSocket *TestSocket
serverSocket *TestSocket
dialer *TestDialer
initializer *TestInitializer
connector *defaultServerConnector
}
func (this *ServerConnectorFixture) Setup() {
this.clientSocket = NewTestSocket()
this.serverSocket = NewTestSocket()
this.dialer = NewTestDialer(this.serverSocket)
this.initializer = NewTestInitializer(true)
this.connector = newServerConnector(this.dialer, this.initializer)
}
//////////////////////////////////////////////////////////////
func (this *ServerConnectorFixture) TestFailedDialReturnsNoProxy() {
this.dialer.socket = nil
proxy := this.connect("address")
this.So(proxy, should.BeNil)
this.So(this.dialer.address, should.Equal, "address")
}
func (this *ServerConnectorFixture) TestFailedInitializationClosesServerSocketAndReturnsNoProxy() {
this.initializer.success = false
proxy := this.connect("a")
this.So(proxy, should.BeNil)
this.So(this.clientSocket.close, should.Equal, 0)
this.So(this.serverSocket.close, should.Equal, 1)
}
func (this *ServerConnectorFixture) TestSuccessfulConnectionYieldsInitializedProxy() {
proxy := this.connect("a")
this.So(proxy, should.NotBeNil)
this.So(proxy.(*defaultProxy).client, should.Equal, this.clientSocket)
this.So(proxy.(*defaultProxy).server, should.Equal, this.serverSocket)
this.So(this.initializer.client, should.Equal, this.clientSocket)
this.So(this.initializer.server, should.Equal, this.serverSocket)
this.So(this.clientSocket.close, should.Equal, 0)
this.So(this.serverSocket.close, should.Equal, 0)
}
func (this *ServerConnectorFixture) connect(address string) proxy {
return this.connector.Connect(this.clientSocket, address)
}
//////////////////////////////////////////////////////////////
type TestDialer struct {
address string
socket Socket
}
func NewTestDialer(socket Socket) *TestDialer {
return &TestDialer{socket: socket}
}
func (this *TestDialer) Dial(address string) Socket {
this.address = address
return this.socket
}
//////////////////////////////////////////////////////////////
type TestInitializer struct {
success bool
client Socket
server Socket
}
func NewTestInitializer(success bool) *TestInitializer {
return &TestInitializer{success: success}
}
func (this *TestInitializer) Initialize(client, server Socket) bool {
this.client = client
this.server = server
return this.success
}