/**
  * Computes canonicalized headers for headers array.
  *
  * @param array $headers request headers.
  * 
  * @see Constructing the Canonicalized Headers String section at 
  *      http://msdn.microsoft.com/en-us/library/windowsazure/dd179428.aspx
  * 
  * @return array
  */
 protected function computeCanonicalizedHeaders($headers)
 {
     $canonicalizedHeaders = array();
     $normalizedHeaders = array();
     $validPrefix = Resources::X_MS_HEADER_PREFIX;
     if (is_null($normalizedHeaders)) {
         return $canonicalizedHeaders;
     }
     foreach ($headers as $header => $value) {
         // Convert header to lower case.
         $header = strtolower($header);
         // Retrieve all headers for the resource that begin with x-ms-,
         // including the x-ms-date header.
         if (Utilities::startsWith($header, $validPrefix)) {
             // Unfold the string by replacing any breaking white space
             // (meaning what splits the headers, which is \r\n) with a single
             // space.
             $value = str_replace("\r\n", ' ', $value);
             // Trim any white space around the colon in the header.
             $value = ltrim($value);
             $header = rtrim($header);
             $normalizedHeaders[$header] = $value;
         }
     }
     // Sort the headers lexicographically by header name, in ascending order.
     // Note that each header may appear only once in the string.
     ksort($normalizedHeaders);
     foreach ($normalizedHeaders as $key => $value) {
         $canonicalizedHeaders[] = $key . ':' . $value;
     }
     return $canonicalizedHeaders;
 }
 /**
  * Computes canonicalized headers for headers array.
  *
  * @param array $headers request headers.
  * 
  * @see Constructing the Canonicalized Headers String section at 
  *      http://msdn.microsoft.com/en-us/library/windowsazure/dd179428.aspx
  * 
  * @return array
  */
 protected function computeCanonicalizedHeaders($headers)
 {
     $canonicalizedHeaders = array();
     if (!is_null($headers)) {
         foreach ($headers as $header => $value) {
             if (Utilities::startsWith($header, Resources::X_MS_HEADER_PREFIX)) {
                 $canonicalizedHeaders[] = strtolower($header) . ':' . trim($value);
             }
         }
     }
     sort($canonicalizedHeaders);
     return $canonicalizedHeaders;
 }
 /**
  * @covers WindowsAzure\ServiceBus\ServiceBusRestProxy::createQueue
  * @covers WindowsAzure\ServiceBus\ServiceBusRestProxy::deleteQueue
  * @covers WindowsAzure\ServiceBus\ServiceBusRestProxy::receiveQueueMessage
  */
 private function doOneTimeSetup()
 {
     $testAlphaExists = false;
     $queues = $this->restProxy->listQueues()->getQueueInfos();
     foreach ($queues as $queue) {
         $queueName = $queue->getTitle();
         if (Utilities::startsWith($queueName, 'Test') || Utilities::startsWith($queueName, 'test')) {
             if (strtolower($queueName) == strtolower('TestAlpha')) {
                 $testAlphaExists = true;
                 $count = $queue->getQueueDescription()->getMessageCount();
                 for ($i = 0; $i != $count; ++$i) {
                     $opts = new ReceiveMessageOptions();
                     $opts->setTimeout(20);
                     try {
                         $this->restProxy->receiveQueueMessage($queueName, $opts);
                     } catch (\Exception $ex) {
                         error_log($ex->getMessage());
                     }
                 }
             } else {
                 try {
                     $this->restProxy->deleteQueue($queueName);
                 } catch (\Exception $ex) {
                     error_log($ex->getMessage());
                 }
             }
         }
     }
     foreach ($this->restProxy->listTopics()->getTopicInfos() as $topic) {
         $topicName = $topic->getTitle();
         if (Utilities::startsWith($topicName, 'Test') || Utilities::startsWith($topicName, 'test')) {
             try {
                 $this->restProxy->deleteTopic($topicName);
             } catch (\Exception $ex) {
                 error_log($ex->getMessage());
             }
         }
     }
     if (!$testAlphaExists) {
         try {
             $this->restProxy->createQueue(new QueueInfo('TestAlpha'));
         } catch (\Exception $ex) {
             error_log($ex->getMessage());
         }
     }
 }
 /**
  * @covers WindowsAzure\Common\Internal\Utilities::startsWith
  */
 public function testStartsWithIgnoreCase()
 {
     // Setup
     $string = 'MYString';
     $prefix = 'mY';
     // Test
     $actual = Utilities::startsWith($string, $prefix, true);
     // Assert
     $this->assertTrue($actual);
 }
 /**
  * @covers WindowsAzure\Blob\BlobRestProxy::createBlockBlob
  * @covers WindowsAzure\Blob\BlobRestProxy::deleteBlob
  * @covers WindowsAzure\Blob\BlobRestProxy::getBlobMetadata
  * @covers WindowsAzure\Blob\BlobRestProxy::getBlobProperties
  * @covers WindowsAzure\Blob\BlobRestProxy::setBlobMetadata
  */
 private function setBlobMetadataWorker($container, $options, $metadata)
 {
     $blob = BlobServiceFunctionalTestData::getInterestingBlobName();
     // Make sure there is something to test
     $this->restProxy->createBlockBlob($container, $blob, "");
     // TODO: Bug https://github->com/WindowsAzure/azure-sdk-for-java/issues/74
     // means that createBlockBlob does not return the etag. So just read it separately.
     $blobInfo = $this->restProxy->getBlobProperties($container, $blob);
     if (!is_null($options)) {
         BlobServiceFunctionalTestData::fixEtagAccessCondition($options->getAccessCondition(), $blobInfo->getProperties()->getEtag());
     }
     $firstkey = '';
     if (!is_null($metadata) && count($metadata) > 0) {
         $firstkey = array_keys($metadata);
         $firstkey = $firstkey[0];
     }
     try {
         try {
             // And put in some properties
             $res = is_null($options) ? $this->restProxy->setBlobMetadata($container, $blob, $metadata) : $this->restProxy->setBlobMetadata($container, $blob, $metadata, $options);
             if (is_null($options)) {
                 $options = new SetBlobMetadataOptions();
             }
             if (!is_null($options->getTimeout()) && $options->getTimeout() < 1) {
                 $this->assertTrue(false, 'Expect negative timeouts in $options to throw');
             }
             if (!BlobServiceFunctionalTestData::passTemporalAccessCondition($options->getAccessCondition())) {
                 $this->assertTrue(false, 'Failing access condition should throw');
             }
             if (!BlobServiceFunctionalTestData::passTemporalAccessCondition($options->getAccessCondition())) {
                 $this->assertTrue(false, 'Expect failing access condition to throw');
             }
             if (Utilities::startsWith($firstkey, '<')) {
                 $this->assertTrue(false, 'Should get HTTP request error if the metadata is invalid');
             }
             $this->verifySetBlobMetadataWorker($res);
             $res2 = $this->restProxy->getBlobMetadata($container, $blob);
             $this->verifyGetBlobMetadataWorker($res2, $metadata);
         } catch (\HTTP_Request2_LogicException $e) {
             $this->assertTrue(Utilities::startsWith($firstkey, '<'), 'Should get HTTP request error only if the metadata is invalid');
         }
     } catch (ServiceException $e) {
         if (!is_null($options->getTimeout()) && $options->getTimeout() < 1) {
             $this->assertEquals(500, $e->getCode(), 'bad timeout: getCode');
         } else {
             if (!BlobServiceFunctionalTestData::passTemporalAccessCondition($options->getAccessCondition())) {
                 $this->assertEquals(412, $e->getCode(), 'bad temporal access condition: getCode');
             } else {
                 if (!BlobServiceFunctionalTestData::passEtagAccessCondition($options->getAccessCondition())) {
                     $this->assertEquals(412, $e->getCode(), 'bad etag access condition: getCode');
                 } else {
                     throw $e;
                 }
             }
         }
     }
     // Clean up.
     $this->restProxy->deleteBlob($container, $blob);
 }
 /**
  * Gets metadata array by parsing them from given headers.
  *
  * @param array $headers HTTP headers containing metadata elements.
  * 
  * @return array.
  */
 public function getMetadataArray($headers)
 {
     $metadata = array();
     foreach ($headers as $key => $value) {
         $isMetadataHeader = Utilities::startsWith(strtolower($key), Resources::X_MS_META_HEADER_PREFIX);
         if ($isMetadataHeader) {
             $MetadataName = str_replace(Resources::X_MS_META_HEADER_PREFIX, Resources::EMPTY_STRING, strtolower($key));
             $metadata[$MetadataName] = $value;
         }
     }
     return $metadata;
 }
 public function wrapAccessToken($uri, $name, $password, $scope)
 {
     if (!Utilities::startsWith($scope, 'http://test/scope2')) {
         ++$this->count1;
         $id = '1-' . $this->count1;
     } else {
         ++$this->count2;
         $id = '2-' . $this->count2;
     }
     $wrapResponse = new WrapAccessTokenResult();
     $wrapResponse->setExpiresIn(WrapTokenManagerTest::EXPIRES_IN_SEC);
     $wrapResponse->setAccessToken('testaccesstoken' . $id);
     return $wrapResponse;
 }
 /**
  * @covers WindowsAzure\Common\Internal\Utilities::startsWith
  */
 public function testStartsWithDoesNotStartWithPrefix()
 {
     // Setup
     $string = 'amyname';
     $prefix = 'my';
     // Test
     $actual = Utilities::startsWith($string, $prefix);
     $this->assertFalse($actual);
 }
 protected function hasSecureEndpoint()
 {
     $settings = StorageServiceSettings::createFromConnectionString($this->connectionString);
     $uri = $settings->getBlobEndpointUri();
     return Utilities::startsWith($uri, 'https://');
 }
 /**
  * @covers WindowsAzure\Blob\BlobRestProxy::createBlockBlob
  * @covers WindowsAzure\Blob\BlobRestProxy::deleteBlob
  * @covers WindowsAzure\Blob\BlobRestProxy::getBlobMetadata
  * @covers WindowsAzure\Blob\BlobRestProxy::getBlobProperties
  * @covers WindowsAzure\Blob\BlobRestProxy::setBlobMetadata
  */
 private function setBlobMetadataWorker($container, $options, $metadata)
 {
     $blob = BlobServiceFunctionalTestData::getInterestingBlobName($container);
     // Make sure there is something to test
     $createBlockBlobResult = $this->restProxy->createBlockBlob($container, $blob, "");
     if (!is_null($options)) {
         BlobServiceFunctionalTestData::fixETagAccessCondition($options->getAccessCondition(), $createBlockBlobResult->getETag());
     }
     $firstkey = '';
     if (!is_null($metadata) && count($metadata) > 0) {
         $firstkey = array_keys($metadata);
         $firstkey = $firstkey[0];
     }
     try {
         try {
             // And put in some properties
             $res = is_null($options) ? $this->restProxy->setBlobMetadata($container, $blob, $metadata) : $this->restProxy->setBlobMetadata($container, $blob, $metadata, $options);
             if (is_null($options)) {
                 $options = new SetBlobMetadataOptions();
             }
             if (!is_null($options->getTimeout()) && $options->getTimeout() < 1) {
                 $this->assertTrue(false, 'Expect negative timeouts in $options to throw');
             }
             if (!BlobServiceFunctionalTestData::passTemporalAccessCondition($options->getAccessCondition())) {
                 $this->assertTrue(false, 'Failing access condition should throw');
             }
             if (!BlobServiceFunctionalTestData::passTemporalAccessCondition($options->getAccessCondition())) {
                 $this->assertTrue(false, 'Expect failing access condition to throw');
             }
             if (Utilities::startsWith($firstkey, '<')) {
                 $this->assertTrue(false, 'Should get HTTP request error if the metadata is invalid');
             }
             $this->verifySetBlobMetadataWorker($res);
             $res2 = $this->restProxy->getBlobMetadata($container, $blob);
             $this->verifyGetBlobMetadataWorker($res2, $metadata);
         } catch (\HTTP_Request2_LogicException $e) {
             $this->assertTrue(Utilities::startsWith($firstkey, '<'), 'Should get HTTP request error only if the metadata is invalid');
         }
     } catch (ServiceException $e) {
         if (!is_null($options->getTimeout()) && $options->getTimeout() < 1) {
             $this->assertEquals(TestResources::STATUS_INTERNAL_SERVER_ERROR, $e->getCode(), 'bad timeout: getCode');
         } else {
             if (!BlobServiceFunctionalTestData::passTemporalAccessCondition($options->getAccessCondition())) {
                 $this->assertEquals(TestResources::STATUS_PRECONDITION_FAILED, $e->getCode(), 'bad temporal access condition: getCode');
             } else {
                 if (!BlobServiceFunctionalTestData::passETagAccessCondition($options->getAccessCondition())) {
                     $this->assertEquals(TestResources::STATUS_PRECONDITION_FAILED, $e->getCode(), 'bad etag access condition: getCode');
                 } else {
                     throw $e;
                 }
             }
         }
     }
     // Clean up.
     $this->restProxy->deleteBlob($container, $blob);
 }