Client tests using ContextInterface

This commit is contained in:
matt 2015-05-24 19:50:51 -04:00
parent 1c6a486e8a
commit 6676b05d02
3 changed files with 126 additions and 76 deletions

View File

@ -0,0 +1,67 @@
<?php
class AbConnectionContext implements Ratchet\RFC6455\Messaging\Streaming\ContextInterface {
private $_frame;
private $_message;
protected $maskPayload;
/**
* @var \React\Stream\Stream
*/
protected $_conn;
public function __construct(\React\Stream\Stream $connectionContext, $maskPayload = false) {
$this->_conn = $connectionContext;
$this->maskPayload = $maskPayload;
}
public function setFrame(\Ratchet\RFC6455\Messaging\Protocol\FrameInterface $frame = null) {
$this->_frame = $frame;
}
public function getFrame() {
return $this->_frame;
}
public function setMessage(\Ratchet\RFC6455\Messaging\Protocol\MessageInterface $message = null) {
$this->_message = $message;
}
public function getMessage() {
return $this->_message;
}
public function onMessage(\Ratchet\RFC6455\Messaging\Protocol\MessageInterface $msg) {
$frame = new \Ratchet\RFC6455\Messaging\Protocol\Frame($msg->getPayload(), true, $msg[0]->getOpcode());
if ($this->maskPayload) {
$frame->maskPayload();
}
$this->_conn->write($frame->getContents());
}
public function onPing(\Ratchet\RFC6455\Messaging\Protocol\FrameInterface $frame) {
$pong = new \Ratchet\RFC6455\Messaging\Protocol\Frame($frame->getPayload(), true, \Ratchet\RFC6455\Messaging\Protocol\Frame::OP_PONG);
if ($this->maskPayload) {
$pong->maskPayload();
}
$this->_conn->write($pong->getContents());
}
public function onPong(\Ratchet\RFC6455\Messaging\Protocol\FrameInterface $msg) {
// TODO: Implement onPong() method.
}
public function onClose($code = 1000) {
$frame = new \Ratchet\RFC6455\Messaging\Protocol\Frame(
pack('n', $code),
true,
\Ratchet\RFC6455\Messaging\Protocol\Frame::OP_CLOSE
);
if ($this->maskPayload) {
$frame->maskPayload();
}
$this->_conn->end($frame->getContents());
}
}

View File

@ -4,9 +4,32 @@ use Ratchet\RFC6455\Messaging\Protocol\Frame;
use Ratchet\RFC6455\Messaging\Protocol\Message; use Ratchet\RFC6455\Messaging\Protocol\Message;
require __DIR__ . '/../bootstrap.php'; require __DIR__ . '/../bootstrap.php';
require __DIR__ . '/AbConnectionContext.php';
define('AGENT', 'RatchetRFC/0.0.0'); define('AGENT', 'RatchetRFC/0.0.0');
class EmConnectionContext extends AbConnectionContext implements \Evenement\EventEmitterInterface, Ratchet\RFC6455\Messaging\Streaming\ContextInterface {
use \Evenement\EventEmitterTrait;
public function onMessage(\Ratchet\RFC6455\Messaging\Protocol\MessageInterface $msg) {
$this->emit('message', [$msg]);
}
public function sendMessage(Frame $frame) {
if ($this->maskPayload) {
$frame->maskPayload();
}
$this->_conn->write($frame->getContents());
}
public function close($closeCode = Frame::CLOSE_NORMAL) {
$closeFrame = new Frame(pack('n', $closeCode), true, Frame::OP_CLOSE);
$closeFrame->maskPayload();
$this->_conn->end($closeFrame->getContents());
}
}
$loop = React\EventLoop\Factory::create(); $loop = React\EventLoop\Factory::create();
$dnsResolverFactory = new React\Dns\Resolver\Factory(); $dnsResolverFactory = new React\Dns\Resolver\Factory();
@ -26,27 +49,12 @@ function getTestCases() {
$rawResponse = ""; $rawResponse = "";
$response = null; $response = null;
$ms = new \Ratchet\RFC6455\Messaging\Streaming\MessageStreamer(true); $ms = new \Ratchet\RFC6455\Messaging\Streaming\MessageStreamer(new \Ratchet\RFC6455\Encoding\Validator(), true);
$ms->on('message', function (Message $msg) use ($stream, $deferred) { /** @var EmConnectionContext $context */
$deferred->resolve($msg->getPayload()); $context = null;
$closeFrame = new Frame(pack('n', Frame::CLOSE_NORMAL), true, Frame::OP_CLOSE); $stream->on('data', function ($data) use ($stream, &$rawResponse, &$response, $ms, $cn, $deferred, &$context) {
$closeFrame->maskPayload();
$stream->end($closeFrame->getContents());
});
$ms->on('close', function ($code) use ($stream) {
if ($code === null) {
$stream->end();
return;
}
$frame = new Frame(pack('n', $code), true, Frame::OP_CLOSE);
$frame->maskPayload();
$stream->end($frame->getContents());
});
$stream->on('data', function ($data) use ($stream, &$rawResponse, &$response, $ms, $cn, $deferred) {
if ($response === null) { if ($response === null) {
$rawResponse .= $data; $rawResponse .= $data;
$pos = strpos($rawResponse, "\r\n\r\n"); $pos = strpos($rawResponse, "\r\n\r\n");
@ -58,13 +66,20 @@ function getTestCases() {
if (!$cn->validateResponse($response)) { if (!$cn->validateResponse($response)) {
$stream->end(); $stream->end();
$deferred->reject(); $deferred->reject();
} else {
$context = new EmConnectionContext($stream, true);
$context->on('message', function (Message $msg) use ($stream, $deferred, $context) {
$deferred->resolve($msg->getPayload());
$context->close();
});
} }
} }
} }
// feed the message streamer // feed the message streamer
if ($response) { if ($response && $context) {
$ms->onData($data); $ms->onData($data, $context);
} }
}); });
@ -89,34 +104,12 @@ function runTest($case)
$rawResponse = ""; $rawResponse = "";
$response = null; $response = null;
$ms = new \Ratchet\RFC6455\Messaging\Streaming\MessageStreamer(true); $ms = new \Ratchet\RFC6455\Messaging\Streaming\MessageStreamer(new \Ratchet\RFC6455\Encoding\Validator(), true);
$ms->on('message', function (Message $msg) use ($stream, $deferred, $case) { /** @var AbConnectionContext $context */
echo "Got message on case " . $case . "\n"; $context = null;
$opcode = $msg->isBinary() ? Frame::OP_BINARY : Frame::OP_TEXT;
$frame = new Frame($msg->getPayload(), true, $opcode);
$frame->maskPayload();
$stream->write($frame->getContents()); $stream->on('data', function ($data) use ($stream, &$rawResponse, &$response, $ms, $cn, $deferred, &$context) {
});
$ms->on('ping', function (Frame $frame) use ($stream) {
$response = new Frame($frame->getPayload(), true, Frame::OP_PONG);
$response->maskPayload();
$stream->write($response->getContents());
});
$ms->on('close', function ($code) use ($stream, $deferred) {
if ($code === null) {
$stream->end();
return;
}
$frame = new Frame(pack('n', $code), true, Frame::OP_CLOSE);
$frame->maskPayload();
$stream->end($frame->getContents());
});
$stream->on('data', function ($data) use ($stream, &$rawResponse, &$response, $ms, $cn, $deferred) {
if ($response === null) { if ($response === null) {
$rawResponse .= $data; $rawResponse .= $data;
$pos = strpos($rawResponse, "\r\n\r\n"); $pos = strpos($rawResponse, "\r\n\r\n");
@ -128,13 +121,15 @@ function runTest($case)
if (!$cn->validateResponse($response)) { if (!$cn->validateResponse($response)) {
$stream->end(); $stream->end();
$deferred->reject(); $deferred->reject();
} else {
$context = new AbConnectionContext($stream, true);
} }
} }
} }
// feed the message streamer // feed the message streamer
if ($response) { if ($response && $context) {
$ms->onData($data); $ms->onData($data, $context);
} }
}); });
@ -148,8 +143,6 @@ function runTest($case)
return $deferred->promise(); return $deferred->promise();
} }
function createReport() { function createReport() {
global $factory; global $factory;
@ -162,27 +155,12 @@ function createReport() {
$rawResponse = ""; $rawResponse = "";
$response = null; $response = null;
$ms = new \Ratchet\RFC6455\Messaging\Streaming\MessageStreamer(true); $ms = new \Ratchet\RFC6455\Messaging\Streaming\MessageStreamer(new \Ratchet\RFC6455\Encoding\Validator(), true);
$ms->on('message', function (Message $msg) use ($stream, $deferred) { /** @var EmConnectionContext $context */
$deferred->resolve($msg->getPayload()); $context = null;
$closeFrame = new Frame(pack('n', Frame::CLOSE_NORMAL), true, Frame::OP_CLOSE); $stream->on('data', function ($data) use ($stream, &$rawResponse, &$response, $ms, $cn, $deferred, &$context) {
$closeFrame->maskPayload();
$stream->end($closeFrame->getContents());
});
$ms->on('close', function ($code) use ($stream) {
if ($code === null) {
$stream->end();
return;
}
$frame = new Frame(pack('n', $code), true, Frame::OP_CLOSE);
$frame->maskPayload();
$stream->end($frame->getContents());
});
$stream->on('data', function ($data) use ($stream, &$rawResponse, &$response, $ms, $cn, $deferred) {
if ($response === null) { if ($response === null) {
$rawResponse .= $data; $rawResponse .= $data;
$pos = strpos($rawResponse, "\r\n\r\n"); $pos = strpos($rawResponse, "\r\n\r\n");
@ -194,13 +172,20 @@ function createReport() {
if (!$cn->validateResponse($response)) { if (!$cn->validateResponse($response)) {
$stream->end(); $stream->end();
$deferred->reject(); $deferred->reject();
} else {
$context = new EmConnectionContext($stream, true);
$context->on('message', function (Message $msg) use ($stream, $deferred, $context) {
$deferred->resolve($msg->getPayload());
$context->close();
});
} }
} }
} }
// feed the message streamer // feed the message streamer
if ($response) { if ($response && $context) {
$ms->onData($data); $ms->onData($data, $context);
} }
}); });

View File

@ -4,10 +4,8 @@
"failByDrop": false "failByDrop": false
} }
, "outdir": "./reports/clients" , "outdir": "./reports/clients"
, "casesa": ["*"] , "cases": ["*"]
, "casesj": ["9.3.2", "9.3.3", "9.3.4", "9.4.2", "9.4.3", "9.4.4", "9.7.6", "9.8.6"] , "cases_failing": ["9.3.2", "9.3.3", "9.3.4", "9.4.2", "9.4.3", "9.4.4"]
, "cases": ["9.3.1", "9.3.2"]
, "casesx": ["1.*","2.*"]
, "exclude-cases": ["12.*", "13.*"] , "exclude-cases": ["12.*", "13.*"]
, "exclude-agent-cases": {} , "exclude-agent-cases": {}
} }