Commit 2136d8e4 authored by TheBigB's avatar TheBigB
Browse files

Increasing test coverage

parent 62851167
Pipeline #20752967 failed with stage
in 1 minute and 39 seconds
[![build status](https://gitlab.com/1of0/php-curly/badges/develop/build.svg)](https://gitlab.com/1of0/php-curly/commits/develop)
[![coverage report](https://gitlab.com/1of0/php-curly/badges/develop/coverage.svg)](https://gitlab.com/1of0/php-curly/commits/develop)
# Curly
Curly is an object oriented wrapper around PHP's cURL extension.
......
......@@ -4,4 +4,12 @@
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
<exclude>
<directory>src/Exceptions</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
......@@ -95,6 +95,11 @@ class Curly implements HttpClientInterface
curl_exec($channel);
if ($responseStream !== null && $responseStream->isSeekable())
{
$responseStream->rewind();
}
$status = curl_getinfo($channel, CURLINFO_HTTP_CODE);
return new Response($responseStream ?: 'php://temp', $status, $this->parseHeaderStream($headerStream));
}
......@@ -115,7 +120,7 @@ class Curly implements HttpClientInterface
if ($options->outputHeaderStream === null)
{
$stream = fopen('php://temp', 'rwb');
$stream = fopen('php://temp', 'r+b');
$options->outputHeaderStream = $stream;
return new Stream($stream);
}
......@@ -141,7 +146,7 @@ class Curly implements HttpClientInterface
if ($options->outputStream === null)
{
$stream = fopen('php://temp', 'rwb');
$stream = fopen('php://temp', 'r+b');
$options->outputStream = $stream;
return new Stream($stream);
}
......
......@@ -2,8 +2,6 @@
namespace OneOfZero\Curly;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UriInterface;
use Zend\Diactoros\ServerRequest;
use Zend\Diactoros\Uri;
......@@ -33,7 +31,7 @@ class ExtendedServerRequest extends ServerRequest
*/
public function withStringBody($content)
{
$stream = new SharedStream('php://temp', 'rwb');
$stream = new SharedStream('php://temp', 'r+b');
BinarySafe::write($stream, $content);
$stream->rewind();
......@@ -112,134 +110,4 @@ class ExtendedServerRequest extends ServerRequest
->withStringBody($buffer)
;
}
#region // What follows is irrelevant proxying to fix the return-type hints
/**
* {@inheritdoc}
* @return static
*/
public function withProtocolVersion($version)
{
return parent::withProtocolVersion($version);
}
/**
* {@inheritdoc}
* @return static
*/
public function withHeader($header, $value)
{
return parent::withHeader($header, $value);
}
/**
* {@inheritdoc}
* @return static
*/
public function withAddedHeader($header, $value)
{
return parent::withAddedHeader($header, $value);
}
/**
* {@inheritdoc}
* @return static
*/
public function withoutHeader($header)
{
return parent::withoutHeader($header);
}
/**
* {@inheritdoc}
* @return static
*/
public function withBody(StreamInterface $body)
{
return parent::withBody($body);
}
/**
* {@inheritdoc}
* @return static
*/
public function withRequestTarget($requestTarget)
{
return parent::withRequestTarget($requestTarget);
}
/**
* {@inheritdoc}
* @return static
*/
public function withUri(UriInterface $uri, $preserveHost = false)
{
return parent::withUri($uri, $preserveHost);
}
/**
* {@inheritdoc}
* @return static
*/
public function withUploadedFiles(array $uploadedFiles)
{
return parent::withUploadedFiles($uploadedFiles);
}
/**
* {@inheritdoc}
* @return static
*/
public function withCookieParams(array $cookies)
{
return parent::withCookieParams($cookies);
}
/**
* {@inheritdoc}
* @return static
*/
public function withQueryParams(array $query)
{
return parent::withQueryParams($query);
}
/**
* {@inheritdoc}
* @return static
*/
public function withParsedBody($data)
{
return parent::withParsedBody($data);
}
/**
* {@inheritdoc}
* @return static
*/
public function withAttribute($attribute, $value)
{
return parent::withAttribute($attribute, $value);
}
/**
* {@inheritdoc}
* @return static
*/
public function withoutAttribute($attribute)
{
return parent::withoutAttribute($attribute);
}
/**
* {@inheritdoc}
* @return static
*/
public function withMethod($method)
{
return parent::withMethod($method);
}
#endregion
}
......@@ -7,6 +7,7 @@ use OneOfZero\Curly\BinarySafe;
use OneOfZero\Curly\CancellationCallbackInterface;
use OneOfZero\Curly\CurlyOptions;
use OneOfZero\Streams\SharedStreamInterface;
use RuntimeException;
/**
* Class StreamHandler
......@@ -161,8 +162,13 @@ class StreamHandler extends CancellableHandler
// Trim off line break
list($header) = explode("\r\n", $headerData);
header($header, false);
if (headers_sent())
{
throw new RuntimeException('Cannot forward headers because headers were already sent');
}
header($header, false);
return parent::onHeader($channel, $headerData);
}
......
......@@ -11,7 +11,6 @@ namespace OneOfZero\Curly\Tests;
use OneOfZero\Curly\ExtendedServerRequest;
use PHPUnit_Framework_TestCase;
use Zend\Diactoros\Request;
use Zend\Diactoros\Uri;
abstract class AbstractTestCase extends PHPUnit_Framework_TestCase
......
......@@ -11,7 +11,6 @@ namespace OneOfZero\Curly\Tests;
use OneOfZero\Curly\Curly;
use OneOfZero\Curly\ExtendedServerRequest;
use Zend\Diactoros\Request;
class GenericTest extends AbstractTestCase
{
......
......@@ -93,7 +93,7 @@ class HandlerTest extends AbstractTestCase
*/
private function createRandomByteStream()
{
$stream = new SharedStream('php://temp', 'rwb');
$stream = new SharedStream('php://temp', 'r+b');
for ($i = 0; $i < self::BYTES_TO_READ; $i++)
{
......
<?php
/**
* Copyright (c) 2015 Bernardo van der Wal
* MIT License
*
* Refer to the LICENSE file for the full copyright notice.
*/
namespace OneOfZero\Curly\Tests;
use OneOfZero\Curly\Curly;
use OneOfZero\Curly\Handlers\StreamHandler;
use RuntimeException;
class HeaderForwardTest extends AbstractTestCase
{
/**
* Checks whether output streams work properly when configured through a StreamHandler.
*/
public function testHeaderForwarding()
{
$curly = new Curly();
$handler = new StreamHandler();
$handler->enableOutputHeaderForwarding();
$curly->setCustomHandler($handler);
$request = $this->buildRequest('GET', 'get');
$this->expectException(RuntimeException::class);
$response = $curly->request($request);
$this->assertEquals(200, $response->getStatusCode());
// TODO: Abstract header() call in StreamHandler class and test the result
}
}
<?php
/**
* Copyright (c) 2015 Bernardo van der Wal
* MIT License
*
* Refer to the LICENSE file for the full copyright notice.
*/
namespace OneOfZero\Curly\Tests;
use OneOfZero\Curly\Curly;
use OneOfZero\Curly\Handlers\StreamHandler;
use OneOfZero\Curly\SharedStream;
class InputStreamTest extends AbstractTestCase
{
const BYTES_TO_READ = 1024;
/**
* Checks whether output streams work properly when configured through a StreamHandler.
*/
public function testStreamHandler()
{
$curly = new Curly();
$inputStream = $this->createRandomByteStream();
$curly->setCustomHandler(new StreamHandler($inputStream));
$request = $this->buildRequest('POST', 'post')
->withAddedHeader('Content-Type', 'text/plain')
->withAddedHeader('Content-Length', strval(self::BYTES_TO_READ))
;
$response = $curly->request($request);
$this->assertEquals(200, $response->getStatusCode());
$responseObject = json_decode($response->getBody()->getContents());
$this->assertEquals(strval($inputStream), $responseObject->data);
}
/**
* Creates a stream in memory of BYTES_TO_READ random bytes.
*
* @return SharedStream
*/
private function createRandomByteStream()
{
$stream = new SharedStream('php://temp', 'r+b');
for ($i = 0; $i < self::BYTES_TO_READ; $i++)
{
$stream->write(chr(($i % 26) + 97));
}
$stream->rewind();
return $stream;
}
}
......@@ -40,7 +40,7 @@ class OutputStreamTest extends AbstractTestCase
{
$curly = new Curly();
$stream = new SharedStream('php://temp', 'rwb');
$stream = new SharedStream('php://temp', 'r+b');
$curly->setCustomHandler(new StreamHandler(null, $stream));
$request = $this->buildRequest('GET', 'bytes', self::BYTES_TO_READ);
......@@ -57,7 +57,7 @@ class OutputStreamTest extends AbstractTestCase
{
$curly = new Curly();
$stream = new SharedStream('php://temp', 'rwb');
$stream = new SharedStream('php://temp', 'r+b');
$curly->getOptions()->outputStream = $stream->getResource();
$request = $this->buildRequest('GET', 'bytes', self::BYTES_TO_READ);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment