1<?php
2
3namespace OAuthTest\Unit\Common\Http\Client;
4
5use OAuth\Common\Http\Client\CurlClient;
6
7class CurlClientTest extends \PHPUnit_Framework_TestCase
8{
9    /**
10     *
11     */
12    public function testConstructCorrectInstance()
13    {
14        $client = new CurlClient();
15
16        $this->assertInstanceOf('\\OAuth\\Common\\Http\\Client\\AbstractClient', $client);
17    }
18
19    /**
20     * @covers OAuth\Common\Http\Client\CurlClient::setForceSSL3
21     */
22    public function testSetForceSSL3()
23    {
24        $client = new CurlClient();
25
26        $this->assertInstanceOf('\\OAuth\\Common\\Http\\Client\\CurlClient', $client->setForceSSL3(true));
27    }
28
29    /**
30     * @covers OAuth\Common\Http\Client\CurlClient::retrieveResponse
31     */
32    public function testRetrieveResponseThrowsExceptionOnGetRequestWithBody()
33    {
34        $this->setExpectedException('\\InvalidArgumentException');
35
36        $client = new CurlClient();
37
38        $client->retrieveResponse(
39            $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface'),
40            'foo',
41            array(),
42            'GET'
43        );
44    }
45
46    /**
47     * @covers OAuth\Common\Http\Client\CurlClient::retrieveResponse
48     */
49    public function testRetrieveResponseThrowsExceptionOnGetRequestWithBodyMethodConvertedToUpper()
50    {
51        $this->setExpectedException('\\InvalidArgumentException');
52
53        $client = new CurlClient();
54
55        $client->retrieveResponse(
56            $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface'),
57            'foo',
58            array(),
59            'get'
60        );
61    }
62
63    /**
64     * @covers OAuth\Common\Http\Client\StreamClient::retrieveResponse
65     * @covers OAuth\Common\Http\Client\StreamClient::generateStreamContext
66     */
67    public function testRetrieveResponseDefaultUserAgent()
68    {
69        $endPoint = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface');
70        $endPoint->expects($this->any())
71            ->method('getHost')
72            ->will($this->returnValue('httpbin.org'));
73        $endPoint->expects($this->any())
74            ->method('getAbsoluteUri')
75            ->will($this->returnValue('http://httpbin.org/get'));
76
77        $client = new CurlClient();
78
79        $response = $client->retrieveResponse(
80            $endPoint,
81            '',
82            array(),
83            'get'
84        );
85
86        $response = json_decode($response, true);
87
88        $this->assertSame('PHPoAuthLib', $response['headers']['User-Agent']);
89    }
90
91    /**
92     * @covers OAuth\Common\Http\Client\StreamClient::retrieveResponse
93     * @covers OAuth\Common\Http\Client\StreamClient::generateStreamContext
94     */
95    public function testRetrieveResponseCustomUserAgent()
96    {
97        $endPoint = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface');
98        $endPoint->expects($this->any())
99            ->method('getHost')
100            ->will($this->returnValue('httpbin.org'));
101        $endPoint->expects($this->any())
102            ->method('getAbsoluteUri')
103            ->will($this->returnValue('http://httpbin.org/get'));
104
105        $client = new CurlClient('My Super Awesome Http Client');
106
107        $response = $client->retrieveResponse(
108            $endPoint,
109            '',
110            array(),
111            'get'
112        );
113
114        $response = json_decode($response, true);
115
116        $this->assertSame('My Super Awesome Http Client', $response['headers']['User-Agent']);
117    }
118
119    /**
120     * @covers OAuth\Common\Http\Client\CurlClient::retrieveResponse
121     */
122    public function testRetrieveResponseWithCustomContentType()
123    {
124        $endPoint = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface');
125        $endPoint->expects($this->any())
126            ->method('getHost')
127            ->will($this->returnValue('httpbin.org'));
128        $endPoint->expects($this->any())
129            ->method('getAbsoluteUri')
130            ->will($this->returnValue('http://httpbin.org/get'));
131
132        $client = new CurlClient();
133
134        $response = $client->retrieveResponse(
135            $endPoint,
136            '',
137            array('Content-Type' => 'foo/bar'),
138            'get'
139        );
140
141        $response = json_decode($response, true);
142
143        $this->assertSame('foo/bar', $response['headers']['Content-Type']);
144    }
145
146    /**
147     * @covers OAuth\Common\Http\Client\CurlClient::retrieveResponse
148     */
149    public function testRetrieveResponseWithFormUrlEncodedContentType()
150    {
151        $endPoint = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface');
152        $endPoint->expects($this->any())
153            ->method('getHost')
154            ->will($this->returnValue('httpbin.org'));
155        $endPoint->expects($this->any())
156            ->method('getAbsoluteUri')
157            ->will($this->returnValue('http://httpbin.org/post'));
158
159        $client = new CurlClient();
160
161        $response = $client->retrieveResponse(
162            $endPoint,
163            array('foo' => 'bar', 'baz' => 'fab'),
164            array(),
165            'POST'
166        );
167
168        $response = json_decode($response, true);
169
170        $this->assertSame('application/x-www-form-urlencoded', $response['headers']['Content-Type']);
171        $this->assertEquals(array('foo' => 'bar', 'baz' => 'fab'), $response['form']);
172    }
173
174    /**
175     * @covers OAuth\Common\Http\Client\CurlClient::retrieveResponse
176     */
177    public function testRetrieveResponseHost()
178    {
179        $endPoint = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface');
180        $endPoint->expects($this->any())
181            ->method('getHost')
182            ->will($this->returnValue('httpbin.org'));
183        $endPoint->expects($this->any())
184            ->method('getAbsoluteUri')
185            ->will($this->returnValue('http://httpbin.org/post'));
186
187        $client = new CurlClient();
188
189        $response = $client->retrieveResponse(
190            $endPoint,
191            array('foo' => 'bar', 'baz' => 'fab'),
192            array(),
193            'POST'
194        );
195
196        $response = json_decode($response, true);
197
198        $this->assertSame('httpbin.org', $response['headers']['Host']);
199    }
200
201    /**
202     * @covers OAuth\Common\Http\Client\CurlClient::retrieveResponse
203     */
204    public function testRetrieveResponsePostRequestWithRequestBodyAsString()
205    {
206        $endPoint = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface');
207        $endPoint->expects($this->any())
208            ->method('getHost')
209            ->will($this->returnValue('httpbin.org'));
210        $endPoint->expects($this->any())
211            ->method('getAbsoluteUri')
212            ->will($this->returnValue('http://httpbin.org/post'));
213
214        $client = new CurlClient();
215
216        $response = $client->retrieveResponse(
217            $endPoint,
218            'foo',
219            array(),
220            'POST'
221        );
222
223        $response = json_decode($response, true);
224
225        $this->assertSame('foo', $response['data']);
226    }
227
228    /**
229     * @covers OAuth\Common\Http\Client\CurlClient::retrieveResponse
230     */
231    public function testRetrieveResponsePutRequestWithRequestBodyAsString()
232    {
233        $endPoint = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface');
234        $endPoint->expects($this->any())
235            ->method('getHost')
236            ->will($this->returnValue('httpbin.org'));
237        $endPoint->expects($this->any())
238            ->method('getAbsoluteUri')
239            ->will($this->returnValue('http://httpbin.org/put'));
240
241        $client = new CurlClient();
242
243        $response = $client->retrieveResponse(
244            $endPoint,
245            'foo',
246            array(),
247            'PUT'
248        );
249
250        $response = json_decode($response, true);
251
252        $this->assertSame('foo', $response['data']);
253    }
254
255    /**
256     * @covers OAuth\Common\Http\Client\CurlClient::retrieveResponse
257     */
258    public function testRetrieveResponsePutRequestWithRequestBodyAsStringNoRedirects()
259    {
260        $endPoint = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface');
261        $endPoint->expects($this->any())
262            ->method('getHost')
263            ->will($this->returnValue('httpbin.org'));
264        $endPoint->expects($this->any())
265            ->method('getAbsoluteUri')
266            ->will($this->returnValue('http://httpbin.org/put'));
267
268        $client = new CurlClient();
269
270        $client->setMaxRedirects(0);
271
272        $response = $client->retrieveResponse(
273            $endPoint,
274            'foo',
275            array(),
276            'PUT'
277        );
278
279        $response = json_decode($response, true);
280
281        $this->assertSame('foo', $response['data']);
282    }
283
284    /**
285     * @covers OAuth\Common\Http\Client\CurlClient::retrieveResponse
286     */
287    public function testRetrieveResponseWithForcedSsl3()
288    {
289        $endPoint = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface');
290        $endPoint->expects($this->any())
291            ->method('getHost')
292            ->will($this->returnValue('httpbin.org'));
293        $endPoint->expects($this->any())
294            ->method('getAbsoluteUri')
295            ->will($this->returnValue('https://httpbin.org/get'));
296
297        $client = new CurlClient();
298
299        $client->setForceSSL3(true);
300
301        $response = $client->retrieveResponse(
302            $endPoint,
303            '',
304            array('Content-Type' => 'foo/bar'),
305            'get'
306        );
307
308        $response = json_decode($response, true);
309
310        $this->assertSame('foo/bar', $response['headers']['Content-Type']);
311    }
312
313    /**
314     * @covers OAuth\Common\Http\Client\CurlClient::retrieveResponse
315     */
316    public function testRetrieveResponseThrowsExceptionOnInvalidUrl()
317    {
318        $this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
319
320        $endPoint = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface');
321        $endPoint->expects($this->any())
322            ->method('getHost')
323            ->will($this->returnValue('jkehfkefcmekjhcnkerjh'));
324        $endPoint->expects($this->any())
325            ->method('getAbsoluteUri')
326            ->will($this->returnValue('jkehfkefcmekjhcnkerjh'));
327
328        $client = new CurlClient();
329
330        $client->setForceSSL3(true);
331
332        $response = $client->retrieveResponse(
333            $endPoint,
334            '',
335            array('Content-Type' => 'foo/bar'),
336            'get'
337        );
338
339        $response = json_decode($response, true);
340
341        $this->assertSame('foo/bar', $response['headers']['Content-Type']);
342    }
343
344    public function testAdditionalParameters()
345    {
346        $endPoint = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface');
347        $endPoint->expects($this->any())
348            ->method('getHost')
349            ->will($this->returnValue('httpbin.org'));
350        $endPoint->expects($this->any())
351            ->method('getAbsoluteUri')
352            ->will($this->returnValue('http://httpbin.org/gzip'));
353
354        $client = new CurlClient();
355        $client->setCurlParameters(array(
356            CURLOPT_ENCODING => 'gzip',
357        ));
358
359        $response = $client->retrieveResponse(
360            $endPoint,
361            '',
362            array(),
363            'get'
364        );
365
366        $response = json_decode($response, true);
367
368        $this->assertNotNull($response);
369        $this->assertSame('gzip', $response['headers']['Accept-Encoding']);
370        $this->assertTrue($response['gzipped']);
371    }
372}
373