Skip to content

Commit

Permalink
Add headers to the client
Browse files Browse the repository at this point in the history
  • Loading branch information
mbonneau committed Mar 5, 2022
1 parent dba6a0a commit 33325a9
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ class Client extends Observable
private $loop;
private $connector;
private $keepAlive;
private $headers;

public function __construct(string $url, bool $useMessageObject = false, array $subProtocols = [], LoopInterface $loop = null, ConnectorInterface $connector = null, int $keepAlive = 60000)
public function __construct(string $url, bool $useMessageObject = false, array $subProtocols = [], LoopInterface $loop = null, ConnectorInterface $connector = null, int $keepAlive = 60000, array $headers = [])
{
$parsedUrl = parse_url($url);
if (!isset($parsedUrl['scheme']) || !in_array($parsedUrl['scheme'], ['wss', 'ws'])) {
Expand All @@ -48,6 +49,7 @@ public function __construct(string $url, bool $useMessageObject = false, array $
$this->loop = $loop ?: \EventLoop\getLoop();
$this->connector = $connector;
$this->keepAlive = $keepAlive;
$this->headers = $headers;
}

public function _subscribe(ObserverInterface $clientObserver): DisposableInterface
Expand All @@ -72,6 +74,10 @@ public function _subscribe(ObserverInterface $clientObserver): DisposableInterfa
$flatHeaders[$k] = $v[0];
}

foreach ($this->headers as $k => $v) {
$flatHeaders[$k] = $v;
}

$request = $client->request('GET', $this->url, $flatHeaders, '1.1');

$request->on('error', function ($error) use ($clientObserver) {
Expand Down
51 changes: 51 additions & 0 deletions test/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
namespace Rx\Websocket\Test;

use React\EventLoop\Factory;
use React\Socket\ConnectionInterface;
use React\Socket\ConnectorInterface;
use function React\Promise\resolve;
use function RingCentral\Psr7\parse_request;

class ClientTest extends \PHPUnit\Framework\TestCase
{
Expand All @@ -26,4 +30,51 @@ function ($err) use (&$errored) {

$this->assertTrue($errored);
}

public function testPassthroughOfHeaders() {
$writtenData = '';

$connection = $this->getMockBuilder(ConnectionInterface::class)
->getMock();
$connection
->method('write')
->with($this->callback(function($data) use (&$writtenData) { $writtenData .= $data; }))
->willReturn(true);


$connector = $this->getMockBuilder(ConnectorInterface::class)
->getMock();

$connector
->expects($this->once())
->method('connect')
->willReturn(resolve($connection));

$loop = Factory::create();

// expecting connection error
$client = new \Rx\Websocket\Client(
'ws://127.0.0.1:12340/',
false,
[],
$loop,
$connector,
60000,
[
'X-Test-Header' => 'test header value'
]
);

$client->subscribe(
null,
function ($err) use (&$errored) {
$errored = true;
}
);

// This should be the Request
$requestRaw = $writtenData;
$request = parse_request($requestRaw);
$this->assertEquals(['test header value'], $request->getHeader('X-Test-Header'));
}
}

0 comments on commit 33325a9

Please sign in to comment.