Completed
Push — master ( 29bda6...e00326 )
by David
9s
created

CurlClientTest   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 379
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
lcom 1
cbo 3
dl 0
loc 379
rs 10
c 1
b 0
f 0

15 Methods

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