Skip to content

Commit

Permalink
Merge pull request #87 from claudiosdc/Fix
Browse files Browse the repository at this point in the history
Converts "wss" scheme into "https" scheme
  • Loading branch information
mbonneau authored Jan 1, 2019
2 parents addec34 + 00a49ef commit 0aaacb8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
6 changes: 1 addition & 5 deletions src/Connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,7 @@ protected function generateRequest($url, array $subProtocols, array $headers) {
throw new \InvalidArgumentException(sprintf('Cannot connect to invalid URL (%s)', $url));
}

$uri = $uri->withScheme('HTTP');

if (!$uri->getPort()) {
$uri = $uri->withPort('wss' === $scheme ? 443 : 80);
}
$uri = $uri->withScheme('wss' === $scheme ? 'HTTPS' : 'HTTP');

$headers += ['User-Agent' => 'Ratchet-Pawl/0.3'];

Expand Down
41 changes: 41 additions & 0 deletions tests/unit/RequestUriTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* Created by claudio on 2018-12-31
*/

use PHPUnit\Framework\TestCase;
use React\EventLoop\Factory;
use Ratchet\Client\Connector;
use Psr\Http\Message\RequestInterface;

class RequestUriTest extends TestCase {
protected static function getPrivateClassMethod($className, $methodName) {
$class = new ReflectionClass($className);
$method = $class->getMethod($methodName);
$method->setAccessible(true);
return $method;
}

function uriDataProvider() {
return [
['ws://127.0.0.1/bla', 'http://127.0.0.1/bla'],
['wss://127.0.0.1/bla', 'https://127.0.0.1/bla'],
['ws://127.0.0.1:1234/bla', 'http://127.0.0.1:1234/bla'],
['wss://127.0.0.1:4321/bla', 'https://127.0.0.1:4321/bla']
];
}

/**
* @dataProvider uriDataProvider
*/
function testGeneratedRequestUri($uri, $expectedRequestUri) {
$loop = Factory::create();

$connector = new Connector($loop);

$generateRequest = self::getPrivateClassMethod('\Ratchet\Client\Connector', 'generateRequest');
$request = $generateRequest->invokeArgs($connector, [$uri, [], []]);

$this->assertEquals((string)$request->getUri(), $expectedRequestUri);
}
}

0 comments on commit 0aaacb8

Please sign in to comment.