summaryrefslogtreecommitdiff
path: root/vendor/guzzle/guzzle/tests/Guzzle/Tests/Stream
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/guzzle/guzzle/tests/Guzzle/Tests/Stream')
-rw-r--r--vendor/guzzle/guzzle/tests/Guzzle/Tests/Stream/PhpStreamRequestFactoryTest.php172
-rw-r--r--vendor/guzzle/guzzle/tests/Guzzle/Tests/Stream/StreamTest.php189
2 files changed, 361 insertions, 0 deletions
diff --git a/vendor/guzzle/guzzle/tests/Guzzle/Tests/Stream/PhpStreamRequestFactoryTest.php b/vendor/guzzle/guzzle/tests/Guzzle/Tests/Stream/PhpStreamRequestFactoryTest.php
new file mode 100644
index 0000000..083aaa0
--- /dev/null
+++ b/vendor/guzzle/guzzle/tests/Guzzle/Tests/Stream/PhpStreamRequestFactoryTest.php
@@ -0,0 +1,172 @@
+<?php
+
+namespace Guzzle\Tests\Stream;
+
+use Guzzle\Stream\Stream;
+use Guzzle\Stream\PhpStreamRequestFactory;
+use Guzzle\Http\Client;
+
+/**
+ * @group server
+ * @covers \Guzzle\Stream\PhpStreamRequestFactory
+ */
+class PhpStreamRequestFactoryTest extends \Guzzle\Tests\GuzzleTestCase
+{
+ /** @var Client */
+ protected $client;
+
+ /** @var PhpStreamRequestFactory */
+ protected $factory;
+
+ protected function setUp()
+ {
+ $this->client = new Client($this->getServer()->getUrl());
+ $this->factory = new PhpStreamRequestFactory();
+ }
+
+ public function testOpensValidStreamByCreatingContext()
+ {
+ $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nhi");
+ $request = $this->client->get('/');
+ $stream = $this->factory->fromRequest($request);
+ $this->assertEquals('hi', (string) $stream);
+ $headers = $this->factory->getLastResponseHeaders();
+ $this->assertContains('HTTP/1.1 200 OK', $headers);
+ $this->assertContains('Content-Length: 2', $headers);
+ $this->assertSame($headers, $stream->getCustomData('response_headers'));
+ $this->assertEquals(2, $stream->getSize());
+ }
+
+ public function testOpensValidStreamByPassingContextAndMerging()
+ {
+ $request = $this->client->get('/');
+ $this->factory = $this->getMockBuilder('Guzzle\Stream\PhpStreamRequestFactory')
+ ->setMethods(array('createContext', 'createStream'))
+ ->getMock();
+ $this->factory->expects($this->never())
+ ->method('createContext');
+ $this->factory->expects($this->once())
+ ->method('createStream')
+ ->will($this->returnValue(new Stream(fopen('php://temp', 'r'))));
+
+ $context = array('http' => array('method' => 'HEAD', 'ignore_errors' => false));
+ $this->factory->fromRequest($request, stream_context_create($context));
+ $options = stream_context_get_options($this->readAttribute($this->factory, 'context'));
+ $this->assertEquals('HEAD', $options['http']['method']);
+ $this->assertFalse($options['http']['ignore_errors']);
+ $this->assertEquals('1.1', $options['http']['protocol_version']);
+ }
+
+ public function testAppliesProxySettings()
+ {
+ $request = $this->client->get('/');
+ $request->getCurlOptions()->set(CURLOPT_PROXY, 'tcp://foo.com');
+ $this->factory = $this->getMockBuilder('Guzzle\Stream\PhpStreamRequestFactory')
+ ->setMethods(array('createStream'))
+ ->getMock();
+ $this->factory->expects($this->once())
+ ->method('createStream')
+ ->will($this->returnValue(new Stream(fopen('php://temp', 'r'))));
+ $this->factory->fromRequest($request);
+ $options = stream_context_get_options($this->readAttribute($this->factory, 'context'));
+ $this->assertEquals('tcp://foo.com', $options['http']['proxy']);
+ }
+
+ public function testAddsPostFields()
+ {
+ $this->getServer()->flush();
+ $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nhi");
+ $request = $this->client->post('/', array('Foo' => 'Bar'), array('foo' => 'baz bar'));
+ $stream = $this->factory->fromRequest($request);
+ $this->assertEquals('hi', (string) $stream);
+
+ $headers = $this->factory->getLastResponseHeaders();
+ $this->assertContains('HTTP/1.1 200 OK', $headers);
+ $this->assertContains('Content-Length: 2', $headers);
+ $this->assertSame($headers, $stream->getCustomData('response_headers'));
+
+ $received = $this->getServer()->getReceivedRequests();
+ $this->assertEquals(1, count($received));
+ $this->assertContains('POST / HTTP/1.1', $received[0]);
+ $this->assertContains('host: ', $received[0]);
+ $this->assertContains('user-agent: Guzzle/', $received[0]);
+ $this->assertContains('foo: Bar', $received[0]);
+ $this->assertContains('content-length: 13', $received[0]);
+ $this->assertContains('foo=baz%20bar', $received[0]);
+ }
+
+ public function testAddsBody()
+ {
+ $this->getServer()->flush();
+ $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nhi");
+ $request = $this->client->put('/', array('Foo' => 'Bar'), 'Testing...123');
+ $stream = $this->factory->fromRequest($request);
+ $this->assertEquals('hi', (string) $stream);
+
+ $headers = $this->factory->getLastResponseHeaders();
+ $this->assertContains('HTTP/1.1 200 OK', $headers);
+ $this->assertContains('Content-Length: 2', $headers);
+ $this->assertSame($headers, $stream->getCustomData('response_headers'));
+
+ $received = $this->getServer()->getReceivedRequests();
+ $this->assertEquals(1, count($received));
+ $this->assertContains('PUT / HTTP/1.1', $received[0]);
+ $this->assertContains('host: ', $received[0]);
+ $this->assertContains('user-agent: Guzzle/', $received[0]);
+ $this->assertContains('foo: Bar', $received[0]);
+ $this->assertContains('content-length: 13', $received[0]);
+ $this->assertContains('Testing...123', $received[0]);
+ }
+
+ public function testCanDisableSslValidation()
+ {
+ $request = $this->client->get('/');
+ $request->getCurlOptions()->set(CURLOPT_SSL_VERIFYPEER, false);
+ $this->factory = $this->getMockBuilder('Guzzle\Stream\PhpStreamRequestFactory')
+ ->setMethods(array('createStream'))
+ ->getMock();
+ $this->factory->expects($this->once())
+ ->method('createStream')
+ ->will($this->returnValue(new Stream(fopen('php://temp', 'r'))));
+ $this->factory->fromRequest($request);
+ $options = stream_context_get_options($this->readAttribute($this->factory, 'context'));
+ $this->assertFalse($options['ssl']['verify_peer']);
+ }
+
+ public function testUsesSslValidationByDefault()
+ {
+ $request = $this->client->get('/');
+ $this->factory = $this->getMockBuilder('Guzzle\Stream\PhpStreamRequestFactory')
+ ->setMethods(array('createStream'))
+ ->getMock();
+ $this->factory->expects($this->once())
+ ->method('createStream')
+ ->will($this->returnValue(new Stream(fopen('php://temp', 'r'))));
+ $this->factory->fromRequest($request);
+ $options = stream_context_get_options($this->readAttribute($this->factory, 'context'));
+ $this->assertTrue($options['ssl']['verify_peer']);
+ $this->assertSame($request->getCurlOptions()->get(CURLOPT_CAINFO), $options['ssl']['cafile']);
+ }
+
+ public function testBasicAuthAddsUserAndPassToUrl()
+ {
+ $request = $this->client->get('/');
+ $request->setAuth('Foo', 'Bar');
+ $this->factory = $this->getMockBuilder('Guzzle\Stream\PhpStreamRequestFactory')
+ ->setMethods(array('createStream'))
+ ->getMock();
+ $this->factory->expects($this->once())
+ ->method('createStream')
+ ->will($this->returnValue(new Stream(fopen('php://temp', 'r'))));
+ $this->factory->fromRequest($request);
+ $this->assertContains('Foo:Bar@', (string) $this->readAttribute($this->factory, 'url'));
+ }
+
+ public function testCanCreateCustomStreamClass()
+ {
+ $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nhi");
+ $request = $this->client->get('/');
+ $stream = $this->factory->fromRequest($request, array(), array('stream_class' => 'Guzzle\Http\EntityBody'));
+ $this->assertInstanceOf('Guzzle\Http\EntityBody', $stream);
+ }
+}
diff --git a/vendor/guzzle/guzzle/tests/Guzzle/Tests/Stream/StreamTest.php b/vendor/guzzle/guzzle/tests/Guzzle/Tests/Stream/StreamTest.php
new file mode 100644
index 0000000..4973f25
--- /dev/null
+++ b/vendor/guzzle/guzzle/tests/Guzzle/Tests/Stream/StreamTest.php
@@ -0,0 +1,189 @@
+<?php
+
+namespace Guzzle\Tests\Stream;
+
+use Guzzle\Stream\Stream;
+
+/**
+ * @group server
+ * @covers Guzzle\Stream\Stream
+ */
+class StreamTest extends \Guzzle\Tests\GuzzleTestCase
+{
+ /**
+ * @expectedException \InvalidArgumentException
+ */
+ public function testConstructorThrowsExceptionOnInvalidArgument()
+ {
+ $stream = new Stream(true);
+ }
+
+ public function testConstructor()
+ {
+ $handle = fopen('php://temp', 'r+');
+ fwrite($handle, 'data');
+ $stream = new Stream($handle);
+ $this->assertEquals($handle, $stream->getStream());
+ $this->assertTrue($stream->isReadable());
+ $this->assertTrue($stream->isWritable());
+ $this->assertTrue($stream->isLocal());
+ $this->assertTrue($stream->isSeekable());
+ $this->assertEquals('PHP', $stream->getWrapper());
+ $this->assertEquals('TEMP', $stream->getStreamType());
+ $this->assertEquals(4, $stream->getSize());
+ $this->assertEquals('php://temp', $stream->getUri());
+ $this->assertEquals(array(), $stream->getWrapperData());
+ $this->assertFalse($stream->isConsumed());
+ unset($stream);
+ }
+
+ public function testCanModifyStream()
+ {
+ $handle1 = fopen('php://temp', 'r+');
+ $handle2 = fopen('php://temp', 'r+');
+ $stream = new Stream($handle1);
+ $this->assertSame($handle1, $stream->getStream());
+ $stream->setStream($handle2, 10);
+ $this->assertEquals(10, $stream->getSize());
+ $this->assertSame($handle2, $stream->getStream());
+ }
+
+ public function testStreamClosesHandleOnDestruct()
+ {
+ $handle = fopen('php://temp', 'r');
+ $stream = new Stream($handle);
+ unset($stream);
+ $this->assertFalse(is_resource($handle));
+ }
+
+ public function testConvertsToString()
+ {
+ $handle = fopen('php://temp', 'w+');
+ fwrite($handle, 'data');
+ $stream = new Stream($handle);
+ $this->assertEquals('data', (string) $stream);
+ unset($stream);
+
+ $handle = fopen(__DIR__ . '/../TestData/FileBody.txt', 'r');
+ $stream = new Stream($handle);
+ $this->assertEquals('', (string) $stream);
+ unset($stream);
+ }
+
+ public function testConvertsToStringAndRestoresCursorPos()
+ {
+ $handle = fopen('php://temp', 'w+');
+ $stream = new Stream($handle);
+ $stream->write('foobazbar');
+ $stream->seek(3);
+ $this->assertEquals('foobazbar', (string) $stream);
+ $this->assertEquals(3, $stream->ftell());
+ }
+
+ public function testIsConsumed()
+ {
+ $handle = fopen('php://temp', 'w+');
+ fwrite($handle, 'data');
+ $stream = new Stream($handle);
+ $this->assertFalse($stream->isConsumed());
+ $stream->read(4);
+ $this->assertTrue($stream->isConsumed());
+ }
+
+ public function testAllowsSettingManualSize()
+ {
+ $handle = fopen('php://temp', 'w+');
+ fwrite($handle, 'data');
+ $stream = new Stream($handle);
+ $stream->setSize(10);
+ $this->assertEquals(10, $stream->getSize());
+ unset($stream);
+ }
+
+ public function testWrapsStream()
+ {
+ $handle = fopen('php://temp', 'w+');
+ fwrite($handle, 'data');
+ $stream = new Stream($handle);
+ $this->assertTrue($stream->isSeekable());
+ $this->assertTrue($stream->isReadable());
+ $this->assertTrue($stream->seek(0));
+ $this->assertEquals('da', $stream->read(2));
+ $this->assertEquals('ta', $stream->read(2));
+ $this->assertTrue($stream->seek(0));
+ $this->assertEquals('data', $stream->read(4));
+ $stream->write('_appended');
+ $stream->seek(0);
+ $this->assertEquals('data_appended', $stream->read(13));
+ }
+
+ public function testGetSize()
+ {
+ $size = filesize(__DIR__ . '/../../../bootstrap.php');
+ $handle = fopen(__DIR__ . '/../../../bootstrap.php', 'r');
+ $stream = new Stream($handle);
+ $this->assertEquals($handle, $stream->getStream());
+ $this->assertEquals($size, $stream->getSize());
+ $this->assertEquals($size, $stream->getSize());
+ unset($stream);
+
+ // Make sure that false is returned when the size cannot be determined
+ $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
+ $handle = fopen('http://127.0.0.1:' . $this->getServer()->getPort(), 'r');
+ $stream = new Stream($handle);
+ $this->assertEquals(false, $stream->getSize());
+ unset($stream);
+ }
+
+ public function testEnsuresSizeIsConsistent()
+ {
+ $h = fopen('php://temp', 'r+');
+ fwrite($h, 'foo');
+ $stream = new Stream($h);
+ $this->assertEquals(3, $stream->getSize());
+ $stream->write('test');
+ $this->assertEquals(7, $stream->getSize());
+ fclose($h);
+ }
+
+ public function testAbstractsMetaData()
+ {
+ $handle = fopen(__DIR__ . '/../../../bootstrap.php', 'r');
+ $stream = new Stream($handle);
+ $this->assertEquals('plainfile', $stream->getMetaData('wrapper_type'));
+ $this->assertEquals(null, $stream->getMetaData('wrapper_data'));
+ $this->assertInternalType('array', $stream->getMetaData());
+ }
+
+ public function testDoesNotAttemptToWriteToReadonlyStream()
+ {
+ $handle = fopen(__DIR__ . '/../../../bootstrap.php', 'r');
+ $stream = new Stream($handle);
+ $this->assertEquals(0, $stream->write('foo'));
+ }
+
+ public function testProvidesStreamPosition()
+ {
+ $handle = fopen(__DIR__ . '/../../../bootstrap.php', 'r');
+ $stream = new Stream($handle);
+ $stream->read(2);
+ $this->assertSame(ftell($handle), $stream->ftell());
+ $this->assertEquals(2, $stream->ftell());
+ }
+
+ public function testRewindIsSeekZero()
+ {
+ $stream = new Stream(fopen('php://temp', 'w+'));
+ $stream->write('foobazbar');
+ $this->assertTrue($stream->rewind());
+ $this->assertEquals('foobazbar', $stream->read(9));
+ }
+
+ public function testCanDetachStream()
+ {
+ $r = fopen('php://temp', 'w+');
+ $stream = new Stream($r);
+ $stream->detachStream();
+ $this->assertNull($stream->getStream());
+ }
+}