Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added tests

  • Loading branch information...
commit baaeb04e83e32f90ab80e8d83d9280e11460d366 1 parent 5b39aee
@gharlan gharlan authored
View
16 redaxo/src/core/lib/util/socket.php
@@ -274,7 +274,7 @@ protected function openConnection()
* @throws rex_socket_exception
* @return rex_socket_response Response
*/
- protected function writeRequest($method, $path, $headers = array(), $data = '')
+ protected function writeRequest($method, $path, array $headers = array(), $data = '')
{
$this->response = null;
@@ -300,7 +300,7 @@ protected function writeRequest($method, $path, $headers = array(), $data = '')
}
$meta = stream_get_meta_data($this->stream);
- if($meta['timed_out'])
+ if(isset($meta['timed_out']) && $meta['timed_out'])
{
throw new rex_socket_exception('Timeout!');
}
@@ -317,21 +317,25 @@ protected function writeRequest($method, $path, $headers = array(), $data = '')
static protected function parseUrl($url)
{
$parts = parse_url($url);
- if(!isset($parts['host']))
+ if ($parts !== false && !isset($parts['host']) && strpos($url, 'http') !== 0)
+ {
+ $parts = parse_url('http://' . $url);
+ }
+ if ($parts === false || !isset($parts['host']))
{
throw new rex_socket_exception('It isn\'t possible to parse the URL "'. $url .'"!');
}
$port = 80;
$ssl = false;
- if(isset($parts['scheme']))
+ if (isset($parts['scheme']))
{
$supportedProtocols = array('http', 'https');
- if(!in_array($parts['scheme'], $supportedProtocols))
+ if (!in_array($parts['scheme'], $supportedProtocols))
{
throw new rex_socket_exception('Unsupported protocol "'. $parts['scheme'] .'". Supported protocols are '. implode(', ', $supportedProtocols). '.');
}
- if($parts['scheme'] == 'https')
+ if ($parts['scheme'] == 'https')
{
$ssl = true;
$port = 443;
View
5 redaxo/src/core/lib/util/socket_response.php
@@ -13,7 +13,7 @@ class rex_socket_response
private $chunkLength = 0;
private $statusCode;
private $statusMessage;
- private $header;
+ private $header = '';
private $headers = array();
private $body;
@@ -36,7 +36,8 @@ public function __construct($stream)
{
$this->header .= fgets($this->stream);
}
- if(preg_match('@^HTTP/1\.\d ([0-9]{3}) (\V*)@', $this->getHeader(), $matches))
+ $this->header = rtrim($this->header);
+ if (preg_match('@^HTTP/1\.\d ([0-9]+) (\V+)@', $this->header, $matches))
{
$this->statusCode = intval($matches[1]);
$this->statusMessage = $matches[2];
View
29 redaxo/src/core/tests/util/socket_proxy_test.php
@@ -0,0 +1,29 @@
+<?php
+
+class rex_socket_proxy_test extends PHPUnit_Framework_TestCase
+{
+ private $proxy;
+
+ protected function setUp()
+ {
+ $this->proxy = rex::getProperty('socket_proxy');
+ rex::setProperty('socket_proxy', null);
+ }
+
+ protected function tearDown()
+ {
+ rex::setProperty('socket_proxy', $this->proxy);
+ }
+
+ public function testFactory()
+ {
+ $socket = rex_socket_proxy::factory('www.example.com');
+ $this->assertEquals('rex_socket_proxy', get_class($socket));
+ }
+
+ public function testFactoryUrl()
+ {
+ $socket = rex_socket_proxy::factoryUrl('www.example.com');
+ $this->assertEquals('rex_socket_proxy', get_class($socket));
+ }
+}
View
79 redaxo/src/core/tests/util/socket_response_test.php
@@ -0,0 +1,79 @@
+<?php
+
+class rex_socket_response_test extends PHPUnit_Framework_TestCase
+{
+ private function getResponse($content)
+ {
+ $stream = fopen('php://temp', 'r+');
+ fwrite($stream, $content);
+ fseek($stream, 0);
+
+ return new rex_socket_response($stream);
+ }
+
+ public function getStatusProvider()
+ {
+ return array(
+ array('', null, null, 'isInvalid'),
+ array('abc', null, null, 'isInvalid'),
+ array('200 OK', null, null, 'isInvalid'),
+ array('HTTP/1.1 99 Message', 99, 'Message', 'isInvalid'),
+ array('HTTP/1.1 600 Message', 600, 'Message', 'isInvalid'),
+ array('HTTP/1.1 100 Continue', 100, 'Continue', 'isInformational'),
+ array('HTTP/1.1 200 OK', 200, 'OK', 'isSuccessful'),
+ array('HTTP/1.1 301 Moved Permanently', 301, 'Moved Permanently', 'isRedirection'),
+ array('HTTP/1.1 404 Not Found', 404, 'Not Found', 'isClientError'),
+ array('HTTP/1.1 501 Not Implemented', 501, 'Not Implemented', 'isServerError')
+ );
+ }
+
+ /**
+ * @dataProvider getStatusProvider
+ */
+ public function testGetStatus($header, $statusCode, $statusMessage, $positiveMethod)
+ {
+ $response = $this->getResponse($header . "\r\n");
+
+ $this->assertSame($statusCode, $response->getStatusCode(), 'getStatusCode()');
+ $this->assertSame($statusMessage, $response->getStatusMessage(), 'getStatusMessage()');
+ $this->assertSame($statusCode == 200, $response->isOk(), 'isOk()');
+
+ $methods = array('isInformational', 'isSuccessful', 'isRedirection', 'isClientError', 'isServerError', 'isInvalid');
+ foreach($methods as $method)
+ {
+ $this->assertSame($positiveMethod == $method, $response->$method(), $method . '()');
+ }
+ }
+
+ public function testGetHeader()
+ {
+ $header = "HTTP/1.1 200 OK\r\nKey1: Value1\r\nkey2: Value2";
+ $response = $this->getResponse($header . "\r\n\r\nbody\r\nbody");
+
+ $this->assertSame($header, $response->getHeader(), 'getHeader() without params returns full header');
+ $this->assertSame('Value1', $response->getHeader('Key1'), 'getHeader($key) returns the value of the key');
+ $this->assertSame('Value2', $response->getHeader('Key2', 'default'), 'getHeader($key, $default) returns the value of the key');
+ $this->assertSame(null, $response->getHeader('Key3'), 'getHeader($key) returns null for non-existing keys');
+ $this->assertSame('default', $response->getHeader('Key3', 'default'), 'getHeader($key, $default) returns $default for non-existing keys');
+ }
+
+ public function testGetBody()
+ {
+ $body = "body1\r\nbody2";
+ $response = $this->getResponse("HTTP/1.1 200 OK\r\nKey: Value\r\n\r\n" . $body);
+
+ $this->assertSame($body, $response->getBody());
+ }
+
+ public function testWriteBodyTo()
+ {
+ $body = "body1\r\nbody2";
+ $response = $this->getResponse("HTTP/1.1 200 OK\r\nKey: Value\r\n\r\n" . $body);
@staabm Owner
staabm added a note

müssen header nicht mit \n getrennt sein? \r\n gibts doch dort nicht oder?

@gharlan Owner
gharlan added a note

Nee, andersrum, es muss \r\n sein.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
+ $temp = fopen('php://temp', 'r+');
+ $response->writeBodyTo($temp);
+ fseek($temp, 0);
+ $this->assertSame($body, fread($temp, 1024));
+ fclose($temp);
+ }
+}
View
129 redaxo/src/core/tests/util/socket_test.php
@@ -0,0 +1,129 @@
+<?php
+
+class rex_socket_test extends PHPUnit_Framework_TestCase
+{
+ private $proxy;
+
+ protected function setUp()
+ {
+ $this->proxy = rex::getProperty('socket_proxy');
+ rex::setProperty('socket_proxy', null);
+ }
+
+ protected function tearDown()
+ {
+ rex::setProperty('socket_proxy', $this->proxy);
+ }
+
+ public function testFactory()
+ {
+ $socket = rex_socket::factory('www.example.com');
+ $this->assertEquals('rex_socket', get_class($socket));
+
+ return $socket;
+ }
+
+ public function testFactoryProxy()
+ {
+ rex::setProperty('socket_proxy', 'proxy.example.com:8888');
+ $socket = rex_socket::factory('www.example.com');
+ $this->assertEquals('rex_socket_proxy', get_class($socket));
+ }
+
+ public function testFactoryUrl()
+ {
+ $socket = rex_socket::factoryUrl('www.example.com');
+ $this->assertEquals('rex_socket', get_class($socket));
+ }
+
+ public function testFactoryUrlProxy()
+ {
+ rex::setProperty('socket_proxy', 'proxy.example.com:8888');
+ $socket = rex_socket::factoryUrl('www.example.com');
+ $this->assertEquals('rex_socket_proxy', get_class($socket));
+ }
+
+ /**
+ * @depends testFactory
+ */
+ public function testWriteRequest($socket)
+ {
+ $class = new ReflectionClass('rex_socket');
+ $property = $class->getProperty('stream');
+ $property->setAccessible(true);
+ $method = $class->getMethod('writeRequest');
+ $method->setAccessible(true);
+
+ $stream = fopen('php://temp', 'r+');
+ $property->setValue($socket, $stream);
+ $response = $method->invoke($socket, 'GET', '/a/path', array('Host' => 'www.example.com', 'Connection' => 'Close'), "body1\r\nbody2");
+
+ $this->assertInstanceOf('rex_socket_response', $response);
+
+ $eol = "\r\n";
+ $expected = 'GET /a/path HTTP/1.1' . $eol
+ . 'Host: www.example.com' . $eol
+ . 'Connection: Close' . $eol
+ . 'Content-Length: 12' . $eol
+ . $eol
+ . 'body1' . $eol
+ . 'body2';
+ fseek($stream, 0);
+ $this->assertEquals($expected, fread($stream, 1024));
+ fclose($stream);
+ }
+
+ public function parseUrlProvider()
+ {
+ return array(
+ array('example.com', 'example.com', 80, false, '/'),
+ array('example.com:81', 'example.com', 81, false, '/'),
+ array('example.com/a/path/?key=value', 'example.com', 80, false, '/a/path/?key=value'),
+ array('example.com:81/a/path/?key=value', 'example.com', 81, false, '/a/path/?key=value'),
+ array('http://example.com', 'example.com', 80, false, '/'),
+ array('https://example.com', 'example.com', 443, true, '/'),
+ array('http://example.com:81', 'example.com', 81, false, '/'),
+ array('https://example.com:444', 'example.com', 444, true, '/'),
+ array('http://example.com/a/path/?key=value', 'example.com', 80, false, '/a/path/?key=value'),
+ array('http://example.com:81/a/path/?key=value', 'example.com', 81, false, '/a/path/?key=value'),
+ );
+ }
+
+ /**
+ * @dataProvider parseUrlProvider
+ */
+ public function testParseUrl($url, $expectedHost, $expectedPort, $expectedSsl, $expectedPath)
+ {
+ $method = new ReflectionMethod('rex_socket', 'parseUrl');
+ $method->setAccessible(true);
+ $result = $method->invoke(null, $url);
+ $expected = array(
+ 'host' => $expectedHost,
+ 'port' => $expectedPort,
+ 'ssl' => $expectedSsl,
+ 'path' => $expectedPath
+ );
+ $this->assertEquals($expected, $result);
+ }
+
+ public function parseUrlExceptionProvider()
+ {
+ return array(
+ array(''),
+ array('http://'),
+ array('abc://example.com')
+ );
+ }
+
+ /**
+ * @dataProvider parseUrlExceptionProvider
+ */
+ public function testParseUrlException($url)
+ {
+ $this->setExpectedException('rex_socket_exception');
+
+ $method = new ReflectionMethod('rex_socket', 'parseUrl');
+ $method->setAccessible(true);
+ $method->invoke(null, $url);
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.