1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Spiral: PSR7 aware cURL client (https://github.com/juliangut/spiral) |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/juliangut/spiral for the canonical source repository |
6
|
|
|
* |
7
|
|
|
* @license https://raw.githubusercontent.com/juliangut/spiral/master/LICENSE |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Jgut\Spiral\Tests; |
11
|
|
|
|
12
|
|
|
use Jgut\Spiral\Client; |
13
|
|
|
use Jgut\Spiral\Exception\TransportException; |
14
|
|
|
use Zend\Diactoros\Request; |
15
|
|
|
use Zend\Diactoros\Response; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Client handler tests. |
19
|
|
|
*/ |
20
|
|
|
class ClientTest extends \PHPUnit_Framework_TestCase |
21
|
|
|
{ |
22
|
|
|
public function testGettersSetters() |
23
|
|
|
{ |
24
|
|
|
$transport = $this->getMock('\Jgut\Spiral\Transport'); |
25
|
|
|
|
26
|
|
|
$client = new Client(); |
27
|
|
|
|
28
|
|
|
$client->setTransport($transport); |
29
|
|
|
|
30
|
|
|
static::assertEquals($transport, $client->getTransport()); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testBadRequest() |
34
|
|
|
{ |
35
|
|
|
$request = new Request('http://fake_made_up_web.com', 'GET'); |
36
|
|
|
$response = new Response; |
37
|
|
|
|
38
|
|
|
$client = new Client(); |
39
|
|
|
try { |
40
|
|
|
$client->request($request, $response); |
41
|
|
|
} catch (TransportException $exception) { |
42
|
|
|
static::assertEquals(CURLE_COULDNT_RESOLVE_HOST, $exception->getCode()); |
43
|
|
|
static::assertEquals('', $exception->getCategory()); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testRequest() |
48
|
|
|
{ |
49
|
|
|
$responseContent = <<<RESP |
50
|
|
|
HTTP/1.1 302 FOUND |
51
|
|
|
Server: nginx |
52
|
|
|
Date: Tue, 09 Feb 2016 11:54:35 GMT |
53
|
|
|
Content-Type: text/html; charset=utf-8 |
54
|
|
|
Content-Length: 0 |
55
|
|
|
Connection: keep-alive |
56
|
|
|
Location: get |
57
|
|
|
Access-Control-Allow-Origin: * |
58
|
|
|
Access-Control-Allow-Credentials: true |
59
|
|
|
|
60
|
|
|
HTTP/1.1 200 OK |
61
|
|
|
Server: nginx |
62
|
|
|
Date: Tue, 09 Feb 2016 11:54:35 GMT |
63
|
|
|
Content-Type: text/html; charset=utf-8 |
64
|
|
|
Content-Length: 30 |
65
|
|
|
Connection: keep-alive |
66
|
|
|
Access-Control-Allow-Origin: * |
67
|
|
|
Access-Control-Allow-Credentials: true |
68
|
|
|
|
69
|
|
|
<!doctype html> |
70
|
|
|
<html> |
71
|
|
|
</html> |
72
|
|
|
RESP; |
73
|
|
|
$transferInfo = [ |
74
|
|
|
'header_size' => 452, |
75
|
|
|
'http_code' => 200, |
76
|
|
|
'content_type' => 'text/html; charset=utf-8', |
77
|
|
|
]; |
78
|
|
|
|
79
|
|
|
$transport = $this->getMockBuilder('\Jgut\Spiral\Transport\Curl') |
80
|
|
|
->disableOriginalConstructor() |
81
|
|
|
->getMock(); |
82
|
|
|
$transport |
83
|
|
|
->expects(static::once()) |
84
|
|
|
->method('request') |
85
|
|
|
->will(static::returnValue($responseContent)); |
86
|
|
|
$transport |
87
|
|
|
->expects(static::once()) |
88
|
|
|
->method('responseInfo') |
89
|
|
|
->will(static::returnValue($transferInfo)); |
90
|
|
|
$transport |
91
|
|
|
->expects(static::once()) |
92
|
|
|
->method('close'); |
93
|
|
|
|
94
|
|
|
$request = new Request('', 'GET'); |
95
|
|
|
$response = new Response; |
96
|
|
|
|
97
|
|
|
$client = new Client($transport); |
98
|
|
|
$response = $client->request($request, $response); |
99
|
|
|
|
100
|
|
|
static::assertEquals(200, $response->getStatusCode()); |
101
|
|
|
static::assertEquals('nginx', $response->getHeaderLine('Server')); |
102
|
|
|
static::assertFalse($response->hasHeader('Location')); |
103
|
|
|
static::assertEquals(1, preg_match('/^<!doctype html>/i', $response->getBody())); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|