/** * @test * @dataProvider providerHasError */ public function testHasError($format, $content) { $message = $this->getMock('\\http\\Client\\Response', array('getBody', 'getResponseCode'), array('')); //It returns 500 error $message->expects($this->any())->method('getResponseCode')->will($this->returnValue(500)); //Content should have error message in appropriated application format $message->expects($this->once())->method('getBody')->will($this->returnValue((new Body())->append($content))); $response = new RestClientResponse($message, $format); try { $response->hasError(); $this->assertTrue(false, 'Exception are expected to be thrown here.'); } catch (RestClientException $e) { $this->assertTrue(true); $this->assertInstanceOf($this->getOpenStackClassName('Client\\ErrorData'), $e->error); $this->assertEquals(500, $e->error->code); $this->assertEquals('Fault!', $e->error->message); $this->assertEquals('Error Details...', $e->error->details); } }
/** * {@inheritdoc} * @see Scalr\Service\OpenStack\Client.ClientInterface::call() */ public function call($service, $path = '/', array $options = null, $verb = 'GET', AppFormat $accept = null, $auth = true) { $attempts = 1; if ($accept === null) { $accept = AppFormat::json(); } if (substr($path, 0, 1) !== '/') { $path = '/' . $path; } if ($options === null) { $options = array(); } if (isset($options['content-type'])) { $ctype = (string) $options['content-type']; unset($options['content-type']); } $req = $this->createHttpRequest(); if (isset($options['__speedup'])) { $curOptions = $req->getOptions(); $curOptions['timeout'] = 3; $curOptions['connecttimeout'] = 3; $req->setOptions($curOptions); unset($options['__speedup']); } if (isset($options['_headers'])) { $xHeaders = (array) $options['_headers']; unset($options['_headers']); } $customOptions = array(); foreach ($options as $key => $value) { if (substr($key, 0, 1) === '_') { $customOptions[substr($key, 1)] = $value; unset($options[$key]); } } $req->setMethod(constant('HTTP_METH_' . $verb)); $ctype = 'json'; $headers = array('Accept' => 'application/' . (string) $accept, 'Content-Type' => 'application/' . (isset($ctype) ? $ctype : 'json') . '; charset=UTF-8'); if (isset($xHeaders)) { foreach ($xHeaders as $k => $v) { if (is_string($k)) { $headers[$k] = $v; } } } if ($auth) { $token = $this->getConfig()->getAuthToken(); if (!$token instanceof AuthToken || $token->isExpired()) { $this->getConfig()->setAuthToken(null); //We need to sign on at first. $bAuthRequestSent = true; $authResult = $this->auth(); if (($token = $this->getConfig()->getAuthToken()) === null) { throw new RestClientException('Authentication to OpenStack server failed.'); } } $headers['X-Auth-Token'] = $token->getId(); } $endpoint = $service instanceof ServiceInterface ? $service->getEndpointUrl() : $service; if (substr($endpoint, -1) === '/') { //removes trailing slashes $endpoint = rtrim($endpoint, '/'); } $req->addHeaders($headers); $req->setUrl($endpoint . $path); if ($verb == 'POST') { $req->setBody(json_encode($options)); } elseif ($verb == 'PUT') { if (isset($customOptions['putData'])) { $req->setPutData($customOptions['putData']); } else { if (isset($customOptions['putFile'])) { $req->setPutFile($customOptions['putFile']); } } } else { $req->addQueryData($options); } $message = $this->tryCall($req, $attempts); $response = new RestClientResponse($message, $accept); $response->setRawRequestMessage($req->getRawRequestMessage()); if ($this->debug) { echo "\nURL: " . $req->getUrl() . "\n"; echo $req->getRawRequestMessage() . "\n"; echo $req->getRawResponseMessage() . "\n"; } if ($response->getResponseCode() === 401 && !isset($bAuthRequestSent) && $this->getConfig()->getAuthToken() !== null) { //When this token isn't expired and by some reason it causes unauthorized response //we should reset authurization token and force authentication request $this->getConfig()->setAuthToken(null); $response = call_user_func_array(array($this, __METHOD__), func_get_args()); } return $response; }