diff --git a/phpunit.xml.dist b/phpunit.xml.dist index e093f23..78b3128 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -2,7 +2,7 @@ [A-Za-z]+)\s+(?P/.*)\s+(?P\w+)/(?P\d\.\d)\s*$#i', $line, $matches)) { - $method = strtoupper($matches['method']); - $protocol = strtoupper($matches['protocol']); - $path = $matches['path']; - $version = $matches['version']; - $scheme = 'http'; - } else if (strpos($line, ':')) { - list($key, $value) = explode(':', $line, 2); - $key = trim($key); - // Normalize standard HTTP headers - if (in_array(strtolower($key), self::$requestHeaders)) { - $key = str_replace(' ', '-', ucwords(str_replace('-', ' ', $key))); - } - // Headers are case insensitive - $headers->add($key, trim($value)); - } - } - - // Check for the Host header - if (isset($headers['Host'])) { - $host = $headers['Host']; - } - - if (strpos($host, ':')) { - list($host, $port) = array_map('trim', explode(':', $host)); - if ($port == 443) { - $scheme = 'https'; - } - } else { - $port = ''; - } - - // Check for basic authorization - $auth = isset($headers['Authorization']) ? $headers['Authorization'] : ''; - - if ($auth) { - list($type, $data) = explode(' ', $auth); - if (strtolower($type) == 'basic') { - $data = base64_decode($data); - list($user, $pass) = explode(':', $data); - } - } - - // Check if a query is present - $qpos = strpos($path, '?'); - if ($qpos) { - $query = substr($path, $qpos); - $path = substr($path, 0, $qpos); - } - - return array( - 'method' => $method, - 'protocol' => $protocol, - 'protocol_version' => $version, - 'parts' => array( - 'scheme' => $scheme, - 'host' => $host, - 'port' => $port, - 'user' => $user, - 'pass' => $pass, - 'path' => $path, - 'query' => $query - ), - 'headers' => $headers->getAll(), - 'body' => $body - ); - } - - /** - * {@inheritdoc} - */ - public function fromMessage($message) - { - $parsed = $this->parseMessage($message); - - if (!$parsed) { - return false; - } - - $request = $this->fromParts($parsed['method'], $parsed['parts'], - $parsed['headers'], $parsed['body'], $parsed['protocol'], - $parsed['protocol_version']); - - // EntityEnclosingRequest adds an "Expect: 100-Continue" header when - // using a raw request body for PUT or POST requests. This factory - // method should accurately reflect the message, so here we are - // removing the Expect header if one was not supplied in the message. - if (!isset($parsed['headers']['Expect'])) { - $request->removeHeader('Expect'); - } - - return $request; - } - - /** - * {@inheritdoc} - */ - public function fromParts($method, array $parts, $headers = null, $body = null, $protocol = 'HTTP', $protocolVersion = '1.1') - { - return $this->create($method, Url::buildUrl($parts, true), $headers, $body) - ->setProtocolVersion($protocolVersion); - } - - /** - * {@inheritdoc} - */ - public function create($method, $url, $headers = null, $body = null) - { - if ($method != 'POST' && $method != 'PUT' && $method != 'PATCH') { - $c = $this->requestClass; - $request = new $c($method, $url, $headers); - if ($body) { - $request->setBody(EntityBody::factory($body)); - } - } else { - $c = $this->entityEnclosingRequestClass; - $request = new $c($method, $url, $headers); - - if ($body) { - if ($method == 'POST' && (is_array($body) || $body instanceof Collection)) { - $request->addPostFields($body); - } else if (is_resource($body) || $body instanceof EntityBody) { - $request->setBody($body, (string) $request->getHeader('Content-Type')); - } else { - $request->setBody((string) $body, (string) $request->getHeader('Content-Type')); - } - } - - // Fix chunked transfers based on the passed headers - if (isset($headers['Transfer-Encoding']) && $headers['Transfer-Encoding'] == 'chunked') { - $request->removeHeader('Content-Length') - ->setHeader('Transfer-Encoding', 'chunked'); - } + public function create($method, $url, $headers = null, $body = null) { + $c = $this->entityEnclosingRequestClass; + $request = new $c($method, $url, $headers); + if ($body) { + $request->setBody(EntityBody::factory($body)); } return $request; diff --git a/tests/Ratchet/Tests/Component/WebSocket/Guzzle/Http/Message/RequestFactoryTest.php b/tests/Ratchet/Tests/Component/WebSocket/Guzzle/Http/Message/RequestFactoryTest.php new file mode 100644 index 0000000..1bf54e9 --- /dev/null +++ b/tests/Ratchet/Tests/Component/WebSocket/Guzzle/Http/Message/RequestFactoryTest.php @@ -0,0 +1,67 @@ +factory = RequestFactory::getInstance(); + } + + public function testMessageProvider() { + return array( + 'status' => 'GET / HTTP/1.1' + , 'headers' => array( + 'Upgrade' => 'WebSocket' + , 'Connection' => 'Upgrade' + , 'Host' => 'localhost:8000' + , 'Sec-WebSocket-Key1' => '> b3lU Z0 fh f 3+83394 6 (zG4' + , 'Sec-WebSocket-Key2' => ',3Z0X0677 dV-d [159 Z*4' + ) + , 'body' => "123456\r\n\r\n" + ); + } + + public function combineMessage($status, array $headers, $body = '') { + $message = $status . "\r\n"; + + foreach ($headers as $key => $val) { + $message .= "{$key}: {$val}\r\n"; + } + + $message .= "\r\n{$body}"; + + return $message; + } + + public function testExpectedDataFromGuzzleHeaders() { + $parts = $this->testMessageProvider(); + $message = $this->combineMessage($parts['status'], $parts['headers'], $parts['body']); + $object = $this->factory->fromMessage($message); + + foreach ($parts['headers'] as $key => $val) { + $this->assertEquals($val, $object->getHeader($key, true)); + } + } + + public function testExpectedDataFromNonGuzzleHeaders() { + $parts = $this->testMessageProvider(); + $message = $this->combineMessage($parts['status'], $parts['headers'], $parts['body']); + $object = $this->factory->fromMessage($message); + + $this->assertNull($object->getHeader('Nope', true)); + $this->assertNull($object->getHeader('Nope')); + } + + public function testExpectedDataFromNonGuzzleBody() { + $parts = $this->testMessageProvider(); + $message = $this->combineMessage($parts['status'], $parts['headers'], $parts['body']); + $object = $this->factory->fromMessage($message); + + $this->assertEquals($parts['body'], (string)$object->getBody()); + } +} \ No newline at end of file diff --git a/tests/Ratchet/Tests/Component/WebSocket/Version/RFC6455Test.php b/tests/Ratchet/Tests/Component/WebSocket/Version/RFC6455Test.php index 3b22481..b33b9e8 100644 --- a/tests/Ratchet/Tests/Component/WebSocket/Version/RFC6455Test.php +++ b/tests/Ratchet/Tests/Component/WebSocket/Version/RFC6455Test.php @@ -125,4 +125,12 @@ class RFC6455Test extends \PHPUnit_Framework_TestCase { $this->_version->handshake($request); } } + + public function testNewMessage() { + $this->assertInstanceOf('\\Ratchet\\Component\\WebSocket\\Version\\RFC6455\\Message', $this->_version->newMessage()); + } + + public function testNewFrame() { + $this->assertInstanceOf('\\Ratchet\\Component\\WebSocket\\Version\\RFC6455\\Frame', $this->_version->newFrame()); + } } \ No newline at end of file diff --git a/tests/bootstrap.php b/tests/bootstrap.php deleted file mode 100644 index 9c15fdc..0000000 --- a/tests/bootstrap.php +++ /dev/null @@ -1,5 +0,0 @@ -