Open Monograph Press  3.3.0
PhpStreamRequestFactoryTest.php
1 <?php
2 
4 
8 
14 {
16  protected $client;
17 
19  protected $factory;
20 
21  protected function setUp()
22  {
23  $this->client = new Client($this->getServer()->getUrl());
24  $this->factory = new PhpStreamRequestFactory();
25  }
26 
28  {
29  $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nhi");
30  $request = $this->client->get('/');
31  $stream = $this->factory->fromRequest($request);
32  $this->assertEquals('hi', (string) $stream);
33  $headers = $this->factory->getLastResponseHeaders();
34  $this->assertContains('HTTP/1.1 200 OK', $headers);
35  $this->assertContains('Content-Length: 2', $headers);
36  $this->assertSame($headers, $stream->getCustomData('response_headers'));
37  $this->assertEquals(2, $stream->getSize());
38  }
39 
41  {
42  $request = $this->client->get('/');
43  $this->factory = $this->getMockBuilder('Guzzle\Stream\PhpStreamRequestFactory')
44  ->setMethods(array('createContext', 'createStream'))
45  ->getMock();
46  $this->factory->expects($this->never())
47  ->method('createContext');
48  $this->factory->expects($this->once())
49  ->method('createStream')
50  ->will($this->returnValue(new Stream(fopen('php://temp', 'r'))));
51 
52  $context = array('http' => array('method' => 'HEAD', 'ignore_errors' => false));
53  $this->factory->fromRequest($request, stream_context_create($context));
54  $options = stream_context_get_options($this->readAttribute($this->factory, 'context'));
55  $this->assertEquals('HEAD', $options['http']['method']);
56  $this->assertFalse($options['http']['ignore_errors']);
57  $this->assertEquals('1.1', $options['http']['protocol_version']);
58  }
59 
60  public function testAppliesProxySettings()
61  {
62  $request = $this->client->get('/');
63  $request->getCurlOptions()->set(CURLOPT_PROXY, 'tcp://foo.com');
64  $this->factory = $this->getMockBuilder('Guzzle\Stream\PhpStreamRequestFactory')
65  ->setMethods(array('createStream'))
66  ->getMock();
67  $this->factory->expects($this->once())
68  ->method('createStream')
69  ->will($this->returnValue(new Stream(fopen('php://temp', 'r'))));
70  $this->factory->fromRequest($request);
71  $options = stream_context_get_options($this->readAttribute($this->factory, 'context'));
72  $this->assertEquals('tcp://foo.com', $options['http']['proxy']);
73  }
74 
75  public function testAddsPostFields()
76  {
77  $this->getServer()->flush();
78  $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nhi");
79  $request = $this->client->post('/', array('Foo' => 'Bar'), array('foo' => 'baz bar'));
80  $stream = $this->factory->fromRequest($request);
81  $this->assertEquals('hi', (string) $stream);
82 
83  $headers = $this->factory->getLastResponseHeaders();
84  $this->assertContains('HTTP/1.1 200 OK', $headers);
85  $this->assertContains('Content-Length: 2', $headers);
86  $this->assertSame($headers, $stream->getCustomData('response_headers'));
87 
88  $received = $this->getServer()->getReceivedRequests();
89  $this->assertEquals(1, count($received));
90  $this->assertContains('POST / HTTP/1.1', $received[0]);
91  $this->assertContains('host: ', $received[0]);
92  $this->assertContains('user-agent: Guzzle/', $received[0]);
93  $this->assertContains('foo: Bar', $received[0]);
94  $this->assertContains('content-length: 13', $received[0]);
95  $this->assertContains('foo=baz%20bar', $received[0]);
96  }
97 
98  public function testAddsBody()
99  {
100  $this->getServer()->flush();
101  $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nhi");
102  $request = $this->client->put('/', array('Foo' => 'Bar'), 'Testing...123');
103  $stream = $this->factory->fromRequest($request);
104  $this->assertEquals('hi', (string) $stream);
105 
106  $headers = $this->factory->getLastResponseHeaders();
107  $this->assertContains('HTTP/1.1 200 OK', $headers);
108  $this->assertContains('Content-Length: 2', $headers);
109  $this->assertSame($headers, $stream->getCustomData('response_headers'));
110 
111  $received = $this->getServer()->getReceivedRequests();
112  $this->assertEquals(1, count($received));
113  $this->assertContains('PUT / HTTP/1.1', $received[0]);
114  $this->assertContains('host: ', $received[0]);
115  $this->assertContains('user-agent: Guzzle/', $received[0]);
116  $this->assertContains('foo: Bar', $received[0]);
117  $this->assertContains('content-length: 13', $received[0]);
118  $this->assertContains('Testing...123', $received[0]);
119  }
120 
121  public function testCanDisableSslValidation()
122  {
123  $request = $this->client->get('/');
124  $request->getCurlOptions()->set(CURLOPT_SSL_VERIFYPEER, false);
125  $this->factory = $this->getMockBuilder('Guzzle\Stream\PhpStreamRequestFactory')
126  ->setMethods(array('createStream'))
127  ->getMock();
128  $this->factory->expects($this->once())
129  ->method('createStream')
130  ->will($this->returnValue(new Stream(fopen('php://temp', 'r'))));
131  $this->factory->fromRequest($request);
132  $options = stream_context_get_options($this->readAttribute($this->factory, 'context'));
133  $this->assertFalse($options['ssl']['verify_peer']);
134  }
135 
136  public function testUsesSslValidationByDefault()
137  {
138  $request = $this->client->get('/');
139  $this->factory = $this->getMockBuilder('Guzzle\Stream\PhpStreamRequestFactory')
140  ->setMethods(array('createStream'))
141  ->getMock();
142  $this->factory->expects($this->once())
143  ->method('createStream')
144  ->will($this->returnValue(new Stream(fopen('php://temp', 'r'))));
145  $this->factory->fromRequest($request);
146  $options = stream_context_get_options($this->readAttribute($this->factory, 'context'));
147  $this->assertTrue($options['ssl']['verify_peer']);
148  $this->assertSame($request->getCurlOptions()->get(CURLOPT_CAINFO), $options['ssl']['cafile']);
149  }
150 
151  public function testBasicAuthAddsUserAndPassToUrl()
152  {
153  $request = $this->client->get('/');
154  $request->setAuth('Foo', 'Bar');
155  $this->factory = $this->getMockBuilder('Guzzle\Stream\PhpStreamRequestFactory')
156  ->setMethods(array('createStream'))
157  ->getMock();
158  $this->factory->expects($this->once())
159  ->method('createStream')
160  ->will($this->returnValue(new Stream(fopen('php://temp', 'r'))));
161  $this->factory->fromRequest($request);
162  $this->assertContains('Foo:Bar@', (string) $this->readAttribute($this->factory, 'url'));
163  }
164 
165  public function testCanCreateCustomStreamClass()
166  {
167  $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nhi");
168  $request = $this->client->get('/');
169  $stream = $this->factory->fromRequest($request, array(), array('stream_class' => 'Guzzle\Http\EntityBody'));
170  $this->assertInstanceOf('Guzzle\Http\EntityBody', $stream);
171  }
172 }
Guzzle\Tests\Stream\PhpStreamRequestFactoryTest\testAppliesProxySettings
testAppliesProxySettings()
Definition: PhpStreamRequestFactoryTest.php:66
Guzzle\Tests\Stream\PhpStreamRequestFactoryTest\testUsesSslValidationByDefault
testUsesSslValidationByDefault()
Definition: PhpStreamRequestFactoryTest.php:142
Guzzle\Stream\Stream
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Stream/Stream.php:10
Guzzle\Tests\GuzzleTestCase
Definition: GuzzleTestCase.php:22
Guzzle\Stream\PhpStreamRequestFactory
Definition: PhpStreamRequestFactory.php:16
Guzzle\Tests\Stream\PhpStreamRequestFactoryTest\testOpensValidStreamByCreatingContext
testOpensValidStreamByCreatingContext()
Definition: PhpStreamRequestFactoryTest.php:33
Guzzle\Tests\Stream\PhpStreamRequestFactoryTest\testAddsBody
testAddsBody()
Definition: PhpStreamRequestFactoryTest.php:104
Guzzle\Tests\Stream\PhpStreamRequestFactoryTest\testCanDisableSslValidation
testCanDisableSslValidation()
Definition: PhpStreamRequestFactoryTest.php:127
Guzzle\Tests\Stream
Definition: PhpStreamRequestFactoryTest.php:3
Guzzle\Tests\Stream\PhpStreamRequestFactoryTest\testBasicAuthAddsUserAndPassToUrl
testBasicAuthAddsUserAndPassToUrl()
Definition: PhpStreamRequestFactoryTest.php:157
Guzzle\Tests\Stream\PhpStreamRequestFactoryTest
Definition: PhpStreamRequestFactoryTest.php:13
Guzzle\Tests\Stream\PhpStreamRequestFactoryTest\testOpensValidStreamByPassingContextAndMerging
testOpensValidStreamByPassingContextAndMerging()
Definition: PhpStreamRequestFactoryTest.php:46
Guzzle\Tests\Stream\PhpStreamRequestFactoryTest\testAddsPostFields
testAddsPostFields()
Definition: PhpStreamRequestFactoryTest.php:81
Guzzle\Http\Client
Definition: paymethod/paypal/lib/vendor/guzzle/guzzle/src/Guzzle/Http/Client.php:24
Guzzle\Tests\Stream\PhpStreamRequestFactoryTest\$factory
$factory
Definition: PhpStreamRequestFactoryTest.php:25
Guzzle\Tests\GuzzleTestCase\getServer
static getServer()
Definition: GuzzleTestCase.php:36
Guzzle\Tests\Stream\PhpStreamRequestFactoryTest\testCanCreateCustomStreamClass
testCanCreateCustomStreamClass()
Definition: PhpStreamRequestFactoryTest.php:171
Guzzle\Tests\Stream\PhpStreamRequestFactoryTest\setUp
setUp()
Definition: PhpStreamRequestFactoryTest.php:27
Guzzle\Tests\Stream\PhpStreamRequestFactoryTest\$client
$client
Definition: PhpStreamRequestFactoryTest.php:19