Skip to content

Commit

Permalink
Merge branch 'socket-connector'
Browse files Browse the repository at this point in the history
  • Loading branch information
cboden committed Oct 12, 2017
2 parents e371f97 + 44d9225 commit 7a4c92c
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 14 deletions.
19 changes: 19 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
language: php

php:
- 5.4
- 5.5
- 5.6
- 7.0
- 7.1
- hhvm

dist: trusty

matrix:
allow_failures:
- php: hhvm

before_script:
- sh -c 'if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then echo "session.serialize_handler = php" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini; fi;'
- composer install --dev --prefer-source
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
, "evenement/evenement": "^3.0 || ^2.0"
, "ratchet/rfc6455": "^0.2.2"
}
, "require-dev": {
"phpunit/phpunit": "~4.8"
}
, "suggest": {
"reactivex/rxphp": "~2.0"
}
Expand Down
24 changes: 24 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
forceCoversAnnotation="true"
mapTestClassNameToCoveredClassName="true"
bootstrap="tests/bootstrap.php"
colors="true"
backupGlobals="false"
backupStaticAttributes="false"
syntaxCheck="false"
stopOnError="false"
>

<testsuites>
<testsuite name="unit">
<directory>./tests/unit/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./src/</directory>
</whitelist>
</filter>
</phpunit>
27 changes: 13 additions & 14 deletions src/Connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
use Ratchet\RFC6455\Handshake\ClientNegotiator;
use React\EventLoop\LoopInterface;
use React\Socket\ConnectionInterface;
use React\Socket\SecureConnector;
use React\Dns\Resolver\Resolver;
use React\Dns\Resolver\Factory as DnsFactory;
use React\Socket\ConnectorInterface;
use React\Promise\Deferred;
use React\Promise\RejectedPromise;
use GuzzleHttp\Psr7 as gPsr;
Expand All @@ -16,16 +14,14 @@ class Connector {
protected $_secureConnector;
protected $_negotiator;

public function __construct(LoopInterface $loop, Resolver $resolver = null, array $secureContext = []) {
if (null === $resolver) {
$factory = new DnsFactory();
$resolver = $factory->create('8.8.8.8', $loop);
public function __construct(LoopInterface $loop, ConnectorInterface $connector = null) {
if (null === $connector) {
$connector = new \React\Socket\Connector($loop);
}

$this->_loop = $loop;
$this->_connector = new \React\Socket\Connector($loop, ['dns' => $resolver]);
$this->_secureConnector = new SecureConnector($this->_connector, $loop, $secureContext);
$this->_negotiator = new ClientNegotiator;
$this->_loop = $loop;
$this->_connector = $connector;
$this->_negotiator = new ClientNegotiator;
}

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

$port = $uri->getPort() ?: 80;
$port = $uri->getPort() ?: ($secure ? 443 : 80);

$uriString = $uri->getHost() . ':' . $port;
$scheme = $secure ? 'tls' : 'tcp';

$uriString = $scheme . '://' . $uri->getHost() . ':' . $port;

return $connector->connect($uriString)->then(function(ConnectionInterface $conn) use ($request, $subProtocols) {
$futureWsConn = new Deferred;
Expand Down
3 changes: 3 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

require __DIR__ . '/../vendor/autoload.php';
39 changes: 39 additions & 0 deletions tests/unit/ConnectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

use PHPUnit\Framework\TestCase;
use Ratchet\Client\Connector;
use React\EventLoop\Factory;
use React\Promise\RejectedPromise;

class ConnectorTest extends TestCase
{
public function uriDataProvider() {
return [
['ws://127.0.0.1', 'tcp://127.0.0.1:80'],
['wss://127.0.0.1', 'tls://127.0.0.1:443'],
['ws://127.0.0.1:1234', 'tcp://127.0.0.1:1234'],
['wss://127.0.0.1:4321', 'tls://127.0.0.1:4321']
];
}

/**
* @dataProvider uriDataProvider
*/
public function testSecureConnectionUsesTlsScheme($uri, $expectedConnectorUri) {
$loop = Factory::create();

$connector = $this->getMock('React\Socket\ConnectorInterface');

$connector->expects($this->once())
->method('connect')
->with($this->callback(function ($uri) use ($expectedConnectorUri) {
return $uri === $expectedConnectorUri;
}))
// reject the promise so that we don't have to mock a connection here
->willReturn(new RejectedPromise(new Exception('')));

$pawlConnector = new Connector($loop, $connector);

$pawlConnector($uri);
}
}

0 comments on commit 7a4c92c

Please sign in to comment.