/**
  * @covers WindowsAzure\Common\Internal\Filters\AuthenticationFilter::handleResponse
  */
 public function testHandleResponse()
 {
     // Setup
     $channel = new HttpClient();
     $url = new \WindowsAzure\Common\Internal\Http\Url('http://microsoft.com');
     $channel->setUrl($url);
     $response = null;
     $scheme = new SharedKeyAuthScheme('acount', 'key');
     $filter = new AuthenticationFilter($scheme);
     // Test
     $response = $filter->handleResponse($channel, $response);
     // Assert
     $this->assertNull($response);
 }
 /**
  * @covers WindowsAzure\Common\Internal\Filters\WrapFilter::handleResponse
  */
 public function testHandleResponse()
 {
     // Setup
     $channel = new HttpClient();
     $url = new Url('http://microsoft.com');
     $channel->setUrl($url);
     $response = null;
     $settings = ServiceBusSettings::createFromConnectionString(TestResources::getServiceBusConnectionString());
     $wrapUri = $settings->getWrapEndpointUri();
     $wrapFilter = new WrapFilter($wrapUri, $settings->getWrapName(), $settings->getWrapPassword(), $this->_wrapRestProxy);
     // Test
     $response = $wrapFilter->handleResponse($channel, $response);
     // Assert
     $this->assertNull($response);
 }
예제 #3
0
 /**
  * Creates BatchResult object.
  * 
  * @param string            $body           The HTTP response body.
  * @param array             $operations     The batch operations.
  * @param array             $contexts       The batch operations context.
  * @param IAtomReaderWriter $atomSerializer The Atom reader and writer.
  * @param IMimeReaderWriter $mimeSerializer The MIME reader and writer.
  * 
  * @return \WindowsAzure\Table\Models\BatchResult
  * 
  * @throws \InvalidArgumentException 
  */
 public static function create($body, $operations, $contexts, $atomSerializer, $mimeSerializer)
 {
     $result = new BatchResult();
     $responses = self::_constructResponses($body, $mimeSerializer);
     $callbackName = __CLASS__ . '::_compareUsingContentId';
     $count = count($responses);
     $entries = array();
     // Sort $responses based on Content-ID so they match order of $operations.
     uasort($responses, $callbackName);
     for ($i = 0; $i < $count; $i++) {
         $context = $contexts[$i];
         $response = $responses[$i];
         $operation = $operations[$i];
         $type = $operation->getType();
         $body = $response->getBody();
         $headers = $response->getHeader();
         try {
             HttpClient::throwIfError($response->getStatus(), $response->getReasonPhrase(), $response->getBody(), $context->getStatusCodes());
             switch ($type) {
                 case BatchOperationType::INSERT_ENTITY_OPERATION:
                     $entries[] = InsertEntityResult::create($body, $headers, $atomSerializer);
                     break;
                 case BatchOperationType::UPDATE_ENTITY_OPERATION:
                 case BatchOperationType::MERGE_ENTITY_OPERATION:
                 case BatchOperationType::INSERT_REPLACE_ENTITY_OPERATION:
                 case BatchOperationType::INSERT_MERGE_ENTITY_OPERATION:
                     $entries[] = UpdateEntityResult::create($headers);
                     break;
                 case BatchOperationType::DELETE_ENTITY_OPERATION:
                     $entries[] = Resources::BATCH_ENTITY_DEL_MSG;
                     break;
                 default:
                     throw new \InvalidArgumentException();
             }
         } catch (ServiceException $e) {
             $entries[] = BatchError::create($e, $response->getHeader());
         }
     }
     $result->setEntries($entries);
     return $result;
 }
 public function testUploadLargeFileFromResource()
 {
     // Setup
     $assetName = TestResources::MEDIA_SERVICES_ASSET_NAME . $this->createSuffix();
     $assetOptions = Asset::OPTIONS_NONE;
     $accessPolicyName = TestResources::MEDIA_SERVICES_ACCESS_POLICY_NAME . $this->createSuffix();
     $accessPolicyNameRead = TestResources::MEDIA_SERVICES_ACCESS_POLICY_NAME . $this->createSuffix();
     $accessPolicyDuration = 30;
     $accessPolicyDurationRead = 300;
     $accessPolicyPermission = AccessPolicy::PERMISSIONS_WRITE;
     $accessPolicyPermissionRead = AccessPolicy::PERMISSIONS_READ;
     $locatorStartTime = new \DateTime('now -5 minutes');
     $locatorType = Locator::TYPE_SAS;
     $locatorStartTimeRead = new \DateTime('now -5 minutes');
     $locatorTypeRead = Locator::TYPE_SAS;
     $fileName = TestResources::MEDIA_SERVICES_DUMMY_FILE_NAME;
     $fileContent = $this->createLargeFile();
     $resource = fopen(VirtualFileSystem::newFile($fileContent), 'r');
     // Test
     $asset = new Asset($assetOptions);
     $asset->setName($assetName);
     $asset = $this->createAsset($asset);
     $access = new AccessPolicy($accessPolicyName);
     $access->setDurationInMinutes($accessPolicyDuration);
     $access->setPermissions($accessPolicyPermission);
     $access = $this->createAccessPolicy($access);
     $locator = new Locator($asset, $access, $locatorType);
     $locator->setStartTime($locatorStartTime);
     $locator = $this->createLocator($locator);
     $this->restProxy->uploadAssetFile($locator, $fileName, $resource);
     $this->restProxy->createFileInfos($asset);
     $assetFiles = $this->restProxy->getAssetFileList();
     $accessRead = new AccessPolicy($accessPolicyNameRead);
     $accessRead->setDurationInMinutes($accessPolicyDurationRead);
     $accessRead->setPermissions($accessPolicyPermissionRead);
     $accessRead = $this->createAccessPolicy($accessRead);
     $locatorRead = new Locator($asset, $accessRead, $locatorTypeRead);
     $locatorRead->setStartTime($locatorStartTimeRead);
     $locatorRead = $this->createLocator($locatorRead);
     // without sleep() Locator hasn't enough time to create URL, so that's why we have to use at least sleep(30)
     sleep(40);
     $method = Resources::HTTP_GET;
     $url = new Url($locatorRead->getBaseUri() . '/' . $fileName . $locatorRead->getContentAccessComponent());
     $filters = array();
     $statusCode = Resources::STATUS_OK;
     $httpClient = new HttpClient();
     $httpClient->setMethod($method);
     $httpClient->setExpectedStatusCode($statusCode);
     $downloadedFileContent = $httpClient->send($filters, $url);
     // Assert
     $this->assertEquals($assetName, $asset->getName());
     $this->assertEquals($assetOptions, $asset->getOptions());
     $this->assertNotNull($asset->getId());
     $this->assertNotNull($asset->getCreated());
     $this->assertEquals($accessPolicyName, $access->getName());
     $this->assertEquals($accessPolicyDuration, $access->getDurationInMinutes());
     $this->assertEquals($accessPolicyPermission, $access->getPermissions());
     $this->assertEquals($accessPolicyNameRead, $accessRead->getName());
     $this->assertEquals($accessPolicyDurationRead, $accessRead->getDurationInMinutes());
     $this->assertEquals($accessPolicyPermissionRead, $accessRead->getPermissions());
     $this->assertNotNull($asset->getId());
     $this->assertNotNull($asset->getCreated());
     $this->assertEquals($locatorType, $locator->getType());
     $this->assertEquals($locatorStartTime->getTimestamp(), $locator->getStartTime()->getTimestamp());
     $this->assertEquals($locatorTypeRead, $locatorRead->getType());
     $this->assertEquals($locatorStartTimeRead->getTimestamp(), $locatorRead->getStartTime()->getTimestamp());
     $this->assertEquals($asset->getId(), $locator->getAssetId());
     $this->assertEquals($access->getId(), $locator->getAccessPolicyId());
     $this->assertEquals($asset->getId(), $locatorRead->getAssetId());
     $this->assertEquals($accessRead->getId(), $locatorRead->getAccessPolicyId());
     $this->assertEquals(1, count($assetFiles));
     $this->assertEquals($asset->getId(), $assetFiles[0]->getParentAssetId());
     $this->assertEquals($fileName, $assetFiles[0]->getName());
     $this->assertEquals($fileContent, $downloadedFileContent);
 }
 private function _commitBlocks($url, $blockIds)
 {
     $baseUrl = new Url($url);
     $baseUrl->setQueryVariable(Resources::QP_COMP, 'blocklist');
     $xml = new \XMLWriter();
     $xml->openMemory();
     $xml->setIndent(true);
     $xml->startDocument('1.0', 'UTF-8');
     $xml->startElement('BlockList');
     foreach ($blockIds as $blockId) {
         $xml->writeElement('Latest', $blockId);
     }
     $xml->endElement();
     $xml->endDocument();
     $xmlContent = $xml->outputMemory();
     $method = Resources::HTTP_PUT;
     $filters = array();
     $statusCode = Resources::STATUS_CREATED;
     $headers = array(Resources::CONTENT_TYPE => Resources::BINARY_FILE_TYPE, Resources::X_MS_VERSION => Resources::STORAGE_API_LATEST_VERSION);
     $httpClient = new HttpClient();
     $httpClient->setMethod($method);
     $httpClient->setHeaders($headers);
     $httpClient->setExpectedStatusCode($statusCode);
     $httpClient->setBody($xmlContent);
     $httpClient->send($filters, $baseUrl);
 }
 /**
  * @covers WindowsAzure\MediaServices\MediaServicesRestProxy::createAsset
  * @covers WindowsAzure\MediaServices\MediaServicesRestProxy::createAccessPolicy
  * @covers WindowsAzure\MediaServices\MediaServicesRestProxy::createLocator
  */
 public function testCreatingOriginUrlForStreamingContent()
 {
     // Setup
     $asset = $this->createAssetWithFilesForStream();
     $accessPolicy = new AccessPolicy(TestResources::MEDIA_SERVICES_ACCESS_POLICY_NAME . $this->createSuffix());
     $accessPolicy->setDurationInMinutes(300);
     $accessPolicy->setPermissions(AccessPolicy::PERMISSIONS_READ);
     $accessPolicy = $this->createAccessPolicy($accessPolicy);
     $locator = new Locator($asset, $accessPolicy, Locator::TYPE_ON_DEMAND_ORIGIN);
     $locator->setName(TestResources::MEDIA_SERVICES_LOCATOR_NAME . $this->createSuffix());
     $locator->setStartTime(new \DateTime('now -5 minutes'));
     $locator = $this->createLocator($locator);
     $expectedFileContent = TestResources::getSmallIsmc();
     // without sleep() Locator hasn't enough time to create URL, so that's why we have to use at least sleep(30)
     sleep(40);
     // Test
     $method = Resources::HTTP_GET;
     $url = new Url($locator->getPath() . '/' . TestResources::MEDIA_SERVICES_ISM_FILE_NAME . '/' . TestResources::MEDIA_SERVICES_STREAM_APPEND);
     $filters = array();
     $statusCode = Resources::STATUS_OK;
     $httpClient = new HttpClient();
     $httpClient->setMethod($method);
     $httpClient->setExpectedStatusCode($statusCode);
     $result = $httpClient->send($filters, $url);
     // Assert
     $this->assertEquals($expectedFileContent, $result);
 }
 /**
  * @covers WindowsAzure\Common\Internal\Http\HttpClient::throwIfError
  */
 public function testThrowIfError()
 {
     // Setup
     $this->setExpectedException(get_class(new ServiceException('200')));
     HttpClient::throwIfError(200, null, null, array(10));
 }
 public function uploadFile($fileName, $fileContent)
 {
     $asset = new Asset(Asset::OPTIONS_NONE);
     $asset->setName(TestResources::MEDIA_SERVICES_ASSET_NAME . $this->createSuffix());
     $asset = $this->createAsset($asset);
     $access = new AccessPolicy(TestResources::MEDIA_SERVICES_ACCESS_POLICY_NAME . $this->createSuffix());
     $access->setDurationInMinutes(30);
     $access->setPermissions(AccessPolicy::PERMISSIONS_WRITE);
     $access = $this->createAccessPolicy($access);
     $locator = new Locator($asset, $access, Locator::TYPE_SAS);
     $locator->setName(TestResources::MEDIA_SERVICES_LOCATOR_NAME . $this->createSuffix());
     $locator->setStartTime(new \DateTime('now -5 minutes'));
     $locator = $this->createLocator($locator);
     $this->restProxy->uploadAssetFile($locator, $fileName, $fileContent);
     $this->restProxy->createFileInfos($asset);
     $accessPolicy = new AccessPolicy(TestResources::MEDIA_SERVICES_ACCESS_POLICY_NAME . $this->createSuffix());
     $accessPolicy->setDurationInMinutes(300);
     $accessPolicy->setPermissions(AccessPolicy::PERMISSIONS_READ);
     $accessPolicy = $this->createAccessPolicy($accessPolicy);
     $locator = new Locator($asset, $accessPolicy, Locator::TYPE_SAS);
     $locator->setName(TestResources::MEDIA_SERVICES_LOCATOR_NAME . $this->createSuffix());
     $locator->setStartTime(new \DateTime('now -5 minutes'));
     $locator = $this->createLocator($locator);
     // without sleep() Locator hasn't enough time to create URL, so that's why we have to use at least sleep(30)
     sleep(40);
     $method = Resources::HTTP_GET;
     $url = new Url($locator->getBaseUri() . '/' . $fileName . $locator->getContentAccessComponent());
     $filters = array();
     $statusCode = Resources::STATUS_OK;
     $httpClient = new HttpClient();
     $httpClient->setMethod($method);
     $httpClient->setExpectedStatusCode($statusCode);
     return $httpClient->send($filters, $url);
 }
예제 #9
0
 /**
  * Upload asset file to storage.
  *
  * @param WindowsAzure\MediaServices\Models\Locator $locator Write locator for
  * file upload
  *
  * @param string                                    $name    Uploading filename
  * @param string                                    $body    Uploading content
  *
  * @return none
  */
 public function uploadAssetFile($locator, $name, $body)
 {
     Validate::isA($locator, 'WindowsAzure\\Mediaservices\\Models\\Locator', 'locator');
     Validate::isString($name, 'name');
     Validate::notNull($body, 'body');
     $method = Resources::HTTP_PUT;
     $urlFile = $locator->getBaseUri() . '/' . $name;
     $url = new Url($urlFile . $locator->getContentAccessComponent());
     $filters = array();
     $statusCode = Resources::STATUS_CREATED;
     $headers = array(Resources::CONTENT_TYPE => Resources::BINARY_FILE_TYPE, Resources::X_MS_VERSION => Resources::STORAGE_API_LATEST_VERSION, Resources::X_MS_BLOB_TYPE => BlobType::BLOCK_BLOB);
     $httpClient = new HttpClient();
     $httpClient->setMethod($method);
     $httpClient->setHeaders($headers);
     $httpClient->setExpectedStatusCode($statusCode);
     $httpClient->setBody($body);
     $httpClient->send($filters, $url);
 }
 /**
  * @covers WindowsAzure\Common\Internal\Filters\SharedKeyFilter::handleResponse
  */
 public function testHandleResponse()
 {
     // Setup
     $channel = new HttpClient();
     $url = new \WindowsAzure\Common\Internal\Http\Url('http://microsoft.com');
     $channel->setUrl($url);
     $response = null;
     $filter = new SharedKeyFilter('acount', 'key', Resources::QUEUE_TYPE_NAME);
     // Test
     $response = $filter->handleResponse($channel, $response);
     // Assert
     $this->assertNull($response);
 }