File size: 7,207 Bytes
e4f4821 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
<?php
namespace JsonRPC;
require_once __DIR__.'/../../../vendor/autoload.php';
defined('CURLOPT_URL') or define('CURLOPT_URL', 10002);
defined('CURLOPT_RETURNTRANSFER') or define('CURLOPT_RETURNTRANSFER', 19913);
defined('CURLOPT_CONNECTTIMEOUT') or define('CURLOPT_CONNECTTIMEOUT', 78);
defined('CURLOPT_MAXREDIRS') or define('CURLOPT_MAXREDIRS', 68);
defined('CURLOPT_SSL_VERIFYPEER') or define('CURLOPT_SSL_VERIFYPEER', 64);
defined('CURLOPT_POST') or define('CURLOPT_POST', 47);
defined('CURLOPT_POSTFIELDS') or define('CURLOPT_POSTFIELDS', 10015);
defined('CURLOPT_HTTPHEADER') or define('CURLOPT_HTTPHEADER', 10023);
defined('CURLOPT_HEADERFUNCTION') or define('CURLOPT_HEADERFUNCTION', 20079);
defined('CURLOPT_CAINFO') or define('CURLOPT_CAINFO', 10065);
function extension_loaded($extension) {
return HttpClientTest::$functions->extension_loaded($extension);
}
function fopen($url, $mode, $use_include_path, $context)
{
return HttpClientTest::$functions->fopen($url, $mode, $use_include_path, $context);
}
function stream_context_create(array $params)
{
return HttpClientTest::$functions->stream_context_create($params);
}
function curl_init() {
return HttpClientTest::$functions->curl_init();
}
function curl_setopt_array($ch, array $params) {
HttpClientTest::$functions->curl_setopt_array($ch, $params);
}
function curl_setopt($ch, $option, $value) {
HttpClientTest::$functions->curl_setopt($ch, $option, $value);
}
function curl_exec($ch) {
return HttpClientTest::$functions->curl_exec($ch);
}
function curl_close($ch) {
HttpClientTest::$functions->curl_close($ch);
}
class HttpClientTest extends \PHPUnit_Framework_TestCase
{
public static $functions;
public function setUp()
{
self::$functions = $this
->getMockBuilder('stdClass')
->setMethods(array('extension_loaded', 'fopen', 'stream_context_create',
'curl_init', 'curl_setopt_array', 'curl_setopt', 'curl_exec', 'curl_close'))
->getMock();
}
public function testWithServerError()
{
$this->expectException('\JsonRPC\Exception\ServerErrorException');
$httpClient = new HttpClient();
$httpClient->handleExceptions(array(
'HTTP/1.0 301 Moved Permanently',
'Connection: close',
'HTTP/1.1 500 Internal Server Error',
));
}
public function testWithConnectionFailure()
{
$this->expectException('\JsonRPC\Exception\ConnectionFailureException');
$httpClient = new HttpClient();
$httpClient->handleExceptions(array(
'HTTP/1.1 404 Not Found',
));
}
public function testWithAccessForbidden()
{
$this->expectException('\JsonRPC\Exception\AccessDeniedException');
$httpClient = new HttpClient();
$httpClient->handleExceptions(array(
'HTTP/1.1 403 Forbidden',
));
}
public function testWithAccessNotAllowed()
{
$this->expectException('\JsonRPC\Exception\AccessDeniedException');
$httpClient = new HttpClient();
$httpClient->handleExceptions(array(
'HTTP/1.0 401 Unauthorized',
));
}
public function testWithCallback()
{
self::$functions
->expects($this->at(0))
->method('extension_loaded')
->with('curl')
->will($this->returnValue(false));
self::$functions
->expects($this->at(1))
->method('stream_context_create')
->with(array(
'http' => array(
'method' => 'POST',
'protocol_version' => 1.1,
'timeout' => 5,
'max_redirects' => 2,
'header' => implode("\r\n", array(
'User-Agent: JSON-RPC PHP Client <https://github.com/fguillot/JsonRPC>',
'Content-Type: application/json',
'Accept: application/json',
'Connection: close',
'Content-Length: 4',
)),
'content' => 'test',
'ignore_errors' => true,
),
'ssl' => array(
'verify_peer' => true,
'verify_peer_name' => true,
)
))
->will($this->returnValue('context'));
self::$functions
->expects($this->at(2))
->method('fopen')
->with('url', 'r', false, 'context')
->will($this->returnValue(false));
$httpClient = new HttpClient('url');
$httpClient->withBeforeRequestCallback(function(HttpClient $client, $payload) {
$client->withHeaders(array('Content-Length: '.strlen($payload)));
});
$this->expectException('\JsonRPC\Exception\ConnectionFailureException');
$httpClient->execute('test');
}
public function testWithCurl()
{
self::$functions
->expects($this->at(0))
->method('extension_loaded')
->with('curl')
->will($this->returnValue(true));
self::$functions
->expects($this->at(1))
->method('curl_init')
->will($this->returnValue('curl'));
self::$functions
->expects($this->at(2))
->method('curl_setopt_array')
->with('curl', array(
CURLOPT_URL => 'url',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_MAXREDIRS => 2,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => 'test',
CURLOPT_HTTPHEADER => array(
'User-Agent: JSON-RPC PHP Client <https://github.com/fguillot/JsonRPC>',
'Content-Type: application/json',
'Accept: application/json',
'Connection: close',
'Content-Length: 4',
),
CURLOPT_HEADERFUNCTION => function ($curl, $header) use (&$headers) {
$headers[] = $header;
return strlen($header);
}
));
self::$functions
->expects($this->at(3))
->method('curl_setopt')
->with('curl', CURLOPT_CAINFO, 'test.crt');
self::$functions
->expects($this->at(4))
->method('curl_exec')
->with('curl')
->will($this->returnValue(false));
self::$functions
->expects($this->at(5))
->method('curl_close')
->with('curl');
$httpClient = new HttpClient('url');
$httpClient
->withSslLocalCert('test.crt')
->withBeforeRequestCallback(function(HttpClient $client, $payload) {
$client->withHeaders(array('Content-Length: '.strlen($payload)));
});
$this->expectException('\JsonRPC\Exception\ConnectionFailureException');
$httpClient->execute('test');
}
}
|