Skip to content

Commit c5b0495

Browse files
committed
Report proxy connection issues
1 parent 6a0ea6c commit c5b0495

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

src/ProxyConnector.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ public function connect($uri)
189189
$stream->write("CONNECT " . $host . ":" . $port . " HTTP/1.1\r\nHost: " . $host . ":" . $port . "\r\n" . $auth . "\r\n");
190190

191191
return $deferred->promise();
192+
}, function (Exception $e) use ($proxyUri) {
193+
throw new RuntimeException('Unable to connect to proxy (ECONNREFUSED)', defined('SOCKET_ECONNREFUSED') ? SOCKET_ECONNREFUSED : 111, $e);
192194
});
193195
}
194196
}

tests/FunctionalTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ public function setUp()
2828
$this->dnsConnector = new DnsConnector($this->tcpConnector, $resolver);
2929
}
3030

31+
public function testNonListeningSocketRejectsConnection()
32+
{
33+
$proxy = new ProxyConnector('127.0.0.1:9999', $this->dnsConnector);
34+
35+
$promise = $proxy->connect('google.com:80');
36+
37+
$this->setExpectedException('RuntimeException', 'Unable to connect to proxy', SOCKET_ECONNREFUSED);
38+
Block\await($promise, $this->loop, 3.0);
39+
}
40+
3141
public function testPlainGoogleDoesNotAcceptConnectMethod()
3242
{
3343
$proxy = new ProxyConnector('google.com', $this->dnsConnector);

tests/ProxyConnectorTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,18 @@ public function testRejectsUriWithNonTcpScheme()
162162
$promise->then(null, $this->expectCallableOnce());
163163
}
164164

165+
public function testRejectsIfConnectorRejects()
166+
{
167+
$promise = \React\Promise\reject(new \RuntimeException());
168+
$this->connector->expects($this->once())->method('connect')->willReturn($promise);
169+
170+
$proxy = new ProxyConnector('proxy.example.com', $this->connector);
171+
172+
$promise = $proxy->connect('google.com:80');
173+
174+
$promise->then(null, $this->expectCallableOnce());
175+
}
176+
165177
public function testRejectsAndClosesIfStreamWritesNonHttp()
166178
{
167179
$stream = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->setMethods(array('close', 'write'))->getMock();

0 commit comments

Comments
 (0)