Example #1
0
 /**
  * @test
  * @dataProvider provider
  */
 public function testValue($format, $bShouldBeValid)
 {
     if (!$bShouldBeValid) {
         try {
             $fmt = new AppFormat($format);
             $this->assertTrue(false, 'Assertion must be thrown here.');
         } catch (\InvalidArgumentException $e) {
             $this->assertTrue(true);
         }
     } else {
         $fmt = new AppFormat($format);
         $this->assertEquals($format, $fmt->get());
         $this->assertEquals($format, (string) $fmt);
         unset($fmt);
     }
 }
Example #2
0
    /**
     * @test
     */
    public function testConstruction()
    {
        $authToken = LoaderFactory::makeToken(new RestClientResponse(new HttpMessage('HTTP/1.1 200 OK
Content-Type: application/json; charset utf-8

{"access":{"token":{"expires": "2012-09-14T15:11:57.585-05:00","id": "858fb4c2-bf15-4dac-917d-8ec750ae9baa","tenant": {"id": "010101","name": "0101012"}}}}'), AppFormat::json()));
        $this->checkToken($authToken);
        $d = $authToken->getAuthDocument();
        $authToken = new AuthToken();
        $authToken->setExpires(new \DateTime('2012-09-14T15:11:57.585-05:00'))->setId('858fb4c2-bf15-4dac-917d-8ec750ae9baa')->setTenantId('010101')->setTenantName('0101012')->setAuthDocument($d)->setRegionEndpoints(array())->setZones(array());
        $this->checkToken($authToken);
        unset($authToken);
        unset($authToken);
    }
Example #3
0
 /**
  * {@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;
 }
Example #4
0
 public function providerHasError()
 {
     $json = '{"computeFault":{"code":500,"message":"Fault!","details":"Error Details..."}}';
     $xml = '<?xml version="1.0" encoding="UTF-8"?><computeFault xmlns="http://docs.openstack.org/compute/api/v1.1" code="500">' . '<message>Fault!</message><details>Error Details...</details></computeFault>';
     return array(array(AppFormat::json(), $json), array(AppFormat::xml(), $xml));
 }