6
6
use React \SocketClient \TcpConnector ;
7
7
use React \Socket \Server ;
8
8
use Clue \React \Block ;
9
+ use React \Socket \ConnectionException ;
10
+ use React \Socket \ConnectionInterface ;
9
11
10
12
class FunctionalServerTest extends TestCase
11
13
{
@@ -26,6 +28,76 @@ public function testEmitsConnectionForNewConnection()
26
28
Block \sleep (0.1 , $ loop );
27
29
}
28
30
31
+ public function testEmitsConnectionWithRemoteIp ()
32
+ {
33
+ $ loop = Factory::create ();
34
+
35
+ $ server = new Server ($ loop );
36
+ $ peer = null ;
37
+ $ server ->on ('connection ' , function (ConnectionInterface $ conn ) use (&$ peer ) {
38
+ $ peer = $ conn ->getRemoteAddress ();
39
+ });
40
+ $ server ->listen (0 );
41
+ $ port = $ server ->getPort ();
42
+
43
+ $ connector = new TcpConnector ($ loop );
44
+ $ promise = $ connector ->create ('127.0.0.1 ' , $ port );
45
+
46
+ $ promise ->then ($ this ->expectCallableOnce ());
47
+
48
+ Block \sleep (0.1 , $ loop );
49
+
50
+ $ this ->assertEquals ('127.0.0.1 ' , $ peer );
51
+ }
52
+
53
+ public function testEmitsConnectionWithRemoteIpAfterConnectionIsClosedByPeer ()
54
+ {
55
+ $ loop = Factory::create ();
56
+
57
+ $ server = new Server ($ loop );
58
+ $ peer = null ;
59
+ $ server ->on ('connection ' , function (ConnectionInterface $ conn ) use (&$ peer ) {
60
+ $ conn ->on ('close ' , function () use ($ conn , &$ peer ) {
61
+ $ peer = $ conn ->getRemoteAddress ();
62
+ });
63
+ });
64
+ $ server ->listen (0 );
65
+ $ port = $ server ->getPort ();
66
+
67
+ $ connector = new TcpConnector ($ loop );
68
+ $ promise = $ connector ->create ('127.0.0.1 ' , $ port );
69
+
70
+ $ client = Block \await ($ promise , $ loop , 0.1 );
71
+ $ client ->end ();
72
+
73
+ Block \sleep (0.1 , $ loop );
74
+
75
+ $ this ->assertEquals ('127.0.0.1 ' , $ peer );
76
+ }
77
+
78
+ public function testEmitsConnectionWithRemoteNullAddressAfterConnectionIsClosedLocally ()
79
+ {
80
+ $ loop = Factory::create ();
81
+
82
+ $ server = new Server ($ loop );
83
+ $ peer = null ;
84
+ $ server ->on ('connection ' , function (ConnectionInterface $ conn ) use (&$ peer ) {
85
+ $ conn ->close ();
86
+ $ peer = $ conn ->getRemoteAddress ();
87
+ });
88
+ $ server ->listen (0 );
89
+ $ port = $ server ->getPort ();
90
+
91
+ $ connector = new TcpConnector ($ loop );
92
+ $ promise = $ connector ->create ('127.0.0.1 ' , $ port );
93
+
94
+ $ promise ->then ($ this ->expectCallableOnce ());
95
+
96
+ Block \sleep (0.1 , $ loop );
97
+
98
+ $ this ->assertNull ($ peer );
99
+ }
100
+
29
101
public function testEmitsConnectionEvenIfConnectionIsCancelled ()
30
102
{
31
103
$ loop = Factory::create ();
@@ -43,4 +115,51 @@ public function testEmitsConnectionEvenIfConnectionIsCancelled()
43
115
44
116
Block \sleep (0.1 , $ loop );
45
117
}
118
+
119
+ public function testEmitsConnectionForNewIpv6Connection ()
120
+ {
121
+ $ loop = Factory::create ();
122
+
123
+ $ server = new Server ($ loop );
124
+ $ server ->on ('connection ' , $ this ->expectCallableOnce ());
125
+ try {
126
+ $ server ->listen (0 , '::1 ' );
127
+ } catch (ConnectionException $ e ) {
128
+ $ this ->markTestSkipped ('Unable to start IPv6 server socket (not available on your platform?) ' );
129
+ }
130
+ $ port = $ server ->getPort ();
131
+
132
+ $ connector = new TcpConnector ($ loop );
133
+ $ promise = $ connector ->create ('::1 ' , $ port );
134
+
135
+ $ promise ->then ($ this ->expectCallableOnce ());
136
+
137
+ Block \sleep (0.1 , $ loop );
138
+ }
139
+
140
+ public function testEmitsConnectionWithRemoteIpv6 ()
141
+ {
142
+ $ loop = Factory::create ();
143
+
144
+ $ server = new Server ($ loop );
145
+ $ peer = null ;
146
+ $ server ->on ('connection ' , function (ConnectionInterface $ conn ) use (&$ peer ) {
147
+ $ peer = $ conn ->getRemoteAddress ();
148
+ });
149
+ try {
150
+ $ server ->listen (0 , '::1 ' );
151
+ } catch (ConnectionException $ e ) {
152
+ $ this ->markTestSkipped ('Unable to start IPv6 server socket (not available on your platform?) ' );
153
+ }
154
+ $ port = $ server ->getPort ();
155
+
156
+ $ connector = new TcpConnector ($ loop );
157
+ $ promise = $ connector ->create ('::1 ' , $ port );
158
+
159
+ $ promise ->then ($ this ->expectCallableOnce ());
160
+
161
+ Block \sleep (0.1 , $ loop );
162
+
163
+ $ this ->assertEquals ('::1 ' , $ peer );
164
+ }
46
165
}
0 commit comments