testRetrieveResponseThrowsExceptionOnInvalidUrl()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

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