Skip to content

Commit 7a4c92c

Browse files
committed
Merge branch 'socket-connector'
2 parents e371f97 + 44d9225 commit 7a4c92c

File tree

6 files changed

+101
-14
lines changed

6 files changed

+101
-14
lines changed

.travis.yml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
language: php
2+
3+
php:
4+
- 5.4
5+
- 5.5
6+
- 5.6
7+
- 7.0
8+
- 7.1
9+
- hhvm
10+
11+
dist: trusty
12+
13+
matrix:
14+
allow_failures:
15+
- php: hhvm
16+
17+
before_script:
18+
- sh -c 'if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then echo "session.serialize_handler = php" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini; fi;'
19+
- composer install --dev --prefer-source

composer.json

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
, "evenement/evenement": "^3.0 || ^2.0"
1616
, "ratchet/rfc6455": "^0.2.2"
1717
}
18+
, "require-dev": {
19+
"phpunit/phpunit": "~4.8"
20+
}
1821
, "suggest": {
1922
"reactivex/rxphp": "~2.0"
2023
}

phpunit.xml.dist

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
forceCoversAnnotation="true"
4+
mapTestClassNameToCoveredClassName="true"
5+
bootstrap="tests/bootstrap.php"
6+
colors="true"
7+
backupGlobals="false"
8+
backupStaticAttributes="false"
9+
syntaxCheck="false"
10+
stopOnError="false"
11+
>
12+
13+
<testsuites>
14+
<testsuite name="unit">
15+
<directory>./tests/unit/</directory>
16+
</testsuite>
17+
</testsuites>
18+
19+
<filter>
20+
<whitelist>
21+
<directory>./src/</directory>
22+
</whitelist>
23+
</filter>
24+
</phpunit>

src/Connector.php

+13-14
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
use Ratchet\RFC6455\Handshake\ClientNegotiator;
44
use React\EventLoop\LoopInterface;
55
use React\Socket\ConnectionInterface;
6-
use React\Socket\SecureConnector;
7-
use React\Dns\Resolver\Resolver;
8-
use React\Dns\Resolver\Factory as DnsFactory;
6+
use React\Socket\ConnectorInterface;
97
use React\Promise\Deferred;
108
use React\Promise\RejectedPromise;
119
use GuzzleHttp\Psr7 as gPsr;
@@ -16,16 +14,14 @@ class Connector {
1614
protected $_secureConnector;
1715
protected $_negotiator;
1816

19-
public function __construct(LoopInterface $loop, Resolver $resolver = null, array $secureContext = []) {
20-
if (null === $resolver) {
21-
$factory = new DnsFactory();
22-
$resolver = $factory->create('8.8.8.8', $loop);
17+
public function __construct(LoopInterface $loop, ConnectorInterface $connector = null) {
18+
if (null === $connector) {
19+
$connector = new \React\Socket\Connector($loop);
2320
}
2421

25-
$this->_loop = $loop;
26-
$this->_connector = new \React\Socket\Connector($loop, ['dns' => $resolver]);
27-
$this->_secureConnector = new SecureConnector($this->_connector, $loop, $secureContext);
28-
$this->_negotiator = new ClientNegotiator;
22+
$this->_loop = $loop;
23+
$this->_connector = $connector;
24+
$this->_negotiator = new ClientNegotiator;
2925
}
3026

3127
/**
@@ -41,11 +37,14 @@ public function __invoke($url, array $subProtocols = [], array $headers = []) {
4137
} catch (\Exception $e) {
4238
return new RejectedPromise($e);
4339
}
44-
$connector = 'wss' === substr($url, 0, 3) ? $this->_secureConnector : $this->_connector;
40+
$secure = 'wss' === substr($url, 0, 3);
41+
$connector = $this->_connector;
4542

46-
$port = $uri->getPort() ?: 80;
43+
$port = $uri->getPort() ?: ($secure ? 443 : 80);
4744

48-
$uriString = $uri->getHost() . ':' . $port;
45+
$scheme = $secure ? 'tls' : 'tcp';
46+
47+
$uriString = $scheme . '://' . $uri->getHost() . ':' . $port;
4948

5049
return $connector->connect($uriString)->then(function(ConnectionInterface $conn) use ($request, $subProtocols) {
5150
$futureWsConn = new Deferred;

tests/bootstrap.php

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
require __DIR__ . '/../vendor/autoload.php';

tests/unit/ConnectorTest.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
use Ratchet\Client\Connector;
5+
use React\EventLoop\Factory;
6+
use React\Promise\RejectedPromise;
7+
8+
class ConnectorTest extends TestCase
9+
{
10+
public function uriDataProvider() {
11+
return [
12+
['ws://127.0.0.1', 'tcp://127.0.0.1:80'],
13+
['wss://127.0.0.1', 'tls://127.0.0.1:443'],
14+
['ws://127.0.0.1:1234', 'tcp://127.0.0.1:1234'],
15+
['wss://127.0.0.1:4321', 'tls://127.0.0.1:4321']
16+
];
17+
}
18+
19+
/**
20+
* @dataProvider uriDataProvider
21+
*/
22+
public function testSecureConnectionUsesTlsScheme($uri, $expectedConnectorUri) {
23+
$loop = Factory::create();
24+
25+
$connector = $this->getMock('React\Socket\ConnectorInterface');
26+
27+
$connector->expects($this->once())
28+
->method('connect')
29+
->with($this->callback(function ($uri) use ($expectedConnectorUri) {
30+
return $uri === $expectedConnectorUri;
31+
}))
32+
// reject the promise so that we don't have to mock a connection here
33+
->willReturn(new RejectedPromise(new Exception('')));
34+
35+
$pawlConnector = new Connector($loop, $connector);
36+
37+
$pawlConnector($uri);
38+
}
39+
}

0 commit comments

Comments
 (0)