diff --git a/src/Ratchet/Http/Router.php b/src/Ratchet/Http/Router.php index 8e6611e..817f6a8 100644 --- a/src/Ratchet/Http/Router.php +++ b/src/Ratchet/Http/Router.php @@ -3,6 +3,7 @@ namespace Ratchet\Http; use Ratchet\ConnectionInterface; use Guzzle\Http\Message\RequestInterface; use Guzzle\Http\Message\Response; +use Guzzle\Http\Url; use Symfony\Component\Routing\Matcher\UrlMatcherInterface; use Symfony\Component\Routing\Exception\MethodNotAllowedException; use Symfony\Component\Routing\Exception\ResourceNotFoundException; @@ -46,6 +47,16 @@ class Router implements HttpServerInterface { throw new \UnexpectedValueException('All routes must implement Ratchet\Http\HttpServerInterface'); } + $parameters = array(); + foreach($route as $key => $value) { + if ((is_string($key)) && ('_' !== substr($key, 0, 1))) { + $parameters[$key] = $value; + } + } + $url = Url::factory($request->getPath()); + $url->setQuery($parameters); + $request->setUrl($url); + $conn->controller = $route['_controller']; $conn->controller->onOpen($conn, $request); } @@ -89,4 +100,4 @@ class Router implements HttpServerInterface { $conn->send((string)$response); $conn->close(); } -} \ No newline at end of file +} diff --git a/tests/unit/Http/RouterTest.php b/tests/unit/Http/RouterTest.php index 8656dd3..412e774 100644 --- a/tests/unit/Http/RouterTest.php +++ b/tests/unit/Http/RouterTest.php @@ -1,7 +1,9 @@ _router->onError($this->_conn, $e); } + + public function testRouterGeneratesRouteParameters() + { + /** @var $controller WsServerInterface */ + $controller = $this->getMockBuilder('\Ratchet\WebSocket\WsServer')->disableOriginalConstructor()->getMock(); + /** @var $matcher UrlMatcherInterface */ + $this->_matcher->expects($this->any())->method('match')->will( + $this->returnValue(array('_controller' => $controller, 'foo' => 'bar', 'baz' => 'qux')) + ); + $conn = $this->getMock('Ratchet\Mock\Connection'); + + $request = $this->getMock('Guzzle\Http\Message\Request', array('getPath'), array('GET', 'ws://random.url'), '', false); + $request->expects($this->any())->method('getPath')->will($this->returnValue('ws://doesnt.matter/')); + + $request->setHeaderFactory($this->getMock('Guzzle\Http\Message\Header\HeaderFactoryInterface')); + $request->setUrl('ws://doesnt.matter/'); + + $router = new Router($this->_matcher); + + $router->onOpen($conn, $request); + + $this->assertEquals(array('foo' => 'bar', 'baz' => 'qux'), $request->getQuery()->getAll()); + } }