/**
  * 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;
 }
 /**
  * @covers MicrosoftAzure\Storage\Blob\BlobRestProxy::createBlockBlob
  * @covers MicrosoftAzure\Storage\Blob\BlobRestProxy::deleteBlob
  * @covers MicrosoftAzure\Storage\Blob\BlobRestProxy::getBlobMetadata
  * @covers MicrosoftAzure\Storage\Blob\BlobRestProxy::getBlobProperties
  * @covers MicrosoftAzure\Storage\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 {
         // 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 (ServiceException $e) {
         if (Utilities::startsWith($firstkey, '<')) {
             $this->assertEquals(TestResources::STATUS_BAD_REQUEST, $e->getCode(), 'getCode');
         } else {
             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);
 }
 /**
  * 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) {
             // Metadata name is case-presrved and case insensitive
             $MetadataName = str_ireplace(Resources::X_MS_META_HEADER_PREFIX, Resources::EMPTY_STRING, $key);
             $metadata[$MetadataName] = $value;
         }
     }
     return $metadata;
 }
 /**
  * @covers MicrosoftAzure\Storage\Common\Internal\Utilities::startsWith
  */
 public function testStartsWithIgnoreCase()
 {
     // Setup
     $string = 'MYString';
     $prefix = 'mY';
     // Test
     $actual = Utilities::startsWith($string, $prefix, true);
     // Assert
     $this->assertTrue($actual);
 }
 protected function hasSecureEndpoint()
 {
     $settings = StorageServiceSettings::createFromConnectionString($this->connectionString);
     $uri = $settings->getBlobEndpointUri();
     return Utilities::startsWith($uri, 'https://');
 }