1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173:
<?php
namespace Guzzle\Tests\Stream;
use Guzzle\Stream\Stream;
use Guzzle\Stream\PhpStreamRequestFactory;
use Guzzle\Http\Client;
class PhpStreamRequestFactoryTest extends \Guzzle\Tests\GuzzleTestCase
{
protected $client;
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);
}
}