Skip to content

Commit

Permalink
Merging pull request Textalk#28 into devHeaderEncoding before master-…
Browse files Browse the repository at this point in the history
…merge.
  • Loading branch information
Fredrik Liljegren committed Oct 9, 2015
1 parent f425f20 commit d3eb1d3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
24 changes: 13 additions & 11 deletions lib/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,27 +65,29 @@ protected function connect() {
$host_uri = ($scheme === 'wss' ? 'ssl' : 'tcp') . '://' . $host;

// Set the stream context options if they're already set in the config
if(isset($this->options['context'])) {
if (isset($this->options['context'])) {
// Suppress the error since we'll catch it below
if(@get_resource_type($this->options['context']) === 'stream-context') {
if (@get_resource_type($this->options['context']) === 'stream-context') {
$context = $this->options['context'];
} else {
}
else {
throw new \InvalidArgumentException(
"Stream context in \$options['context'] isn't a valid context"
"Stream context in \$options['context'] isn't a valid context"
);
}
} else {
}
else {
$context = stream_context_create();
}

// Open the socket. @ is there to supress warning that we will catch in check below instead.
$this->socket = @stream_socket_client(
$host_uri . ':' . $port,
$errno,
$errstr,
$this->options['timeout'],
STREAM_CLIENT_CONNECT,
$context
$host_uri . ':' . $port,
$errno,
$errstr,
$this->options['timeout'],
STREAM_CLIENT_CONNECT,
$context
);

if ($this->socket === false) {
Expand Down
17 changes: 12 additions & 5 deletions tests/unit/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,10 @@ public function testSetFragmentSize($port) {
$this->assertSame(123, $size);
}

public function testSetStreamContextOptions() {
/**
* @dataProvider serverPortProvider
*/
public function testSetStreamContextOptions($port) {
$context = stream_context_create();
stream_context_set_option($context, 'ssl', 'verify_peer', true);
stream_context_set_option($context, 'ssl', 'verify_host', true);
Expand All @@ -372,23 +375,27 @@ public function testSetStreamContextOptions() {
'context' => $context
);

$ws = new Client('ws://localhost:' . self::$port, $options);
$ws = new Client('ws://localhost:' . self::$ports[$port], $options);
// Do a send to touch the context using code in connect. We can't really assert that the
// stream has the correct context, but we make sure it doesn't crash.
$ws->send('foo');
$this->assertTrue(get_resource_type($ws->options['context']) === 'stream-context');
}

/**
* @dataProvider serverPortProvider
*
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Stream context in $options['context'] isn't a valid context
*/
public function testSetInvalidStreamContextOptions() {
public function testSetInvalidStreamContextOptions($port) {
$context = false;

$options = array(
'context' => $context
'context' => $context
);

$ws = new Client('ws://localhost:' . self::$port, $options);
$ws = new Client('ws://localhost:' . self::$ports[$port], $options);
$ws->send('foo');
}
}

0 comments on commit d3eb1d3

Please sign in to comment.