コード例 #1
0
        public void Should_return_conflict_status_when_the_container_exists_and_is_not_empty()
        {
            try
            {
                using (new TestHelper(authToken, storageUrl))
                {
                    var putStorageItem = new PutStorageItem(storageUrl, authToken, Constants.CONTAINER_NAME, Constants.StorageItemName, Constants.StorageItemName);
                    Assert.That(putStorageItem.ContentLength, Is.GreaterThan(0));

                    var putStorageItemResponse = new ResponseFactory<CloudFilesResponse>().Create(new CloudFilesRequest(putStorageItem));
                    Assert.That(putStorageItemResponse.Status, Is.EqualTo(HttpStatusCode.Created));
                }
                Assert.Fail("409 conflict expected");
            }
            catch (WebException we)
            {
                var response = (HttpWebResponse)we.Response;
                Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.Conflict));
            }
            finally
            {
                new ResponseFactory<CloudFilesResponse>().Create(new CloudFilesRequest(
                    new DeleteStorageItem(storageUrl, authToken, Constants.CONTAINER_NAME, Constants.StorageItemName)));
                new ResponseFactory<CloudFilesResponse>().Create(new CloudFilesRequest(
                    new DeleteContainer(storageUrl, authToken, Constants.CONTAINER_NAME)));
            }
        }
コード例 #2
0
 public void should_remove_all_slashes_when_creating_uri_and_storage_item_has_forward_slashes_at_the_beginning()
 {
     var memstream = new MemoryStream();
     PutStorageItem item = new PutStorageItem("http://storeme", "itemcont", "/stuffhacks.txt", memstream);
     Uri url = item.CreateUri();
     url.EndsWith("stuffhacks.txt");
 }
コード例 #3
0
        public void PutItemInContainer(string storageItemName, string remoteName)
        {
            var putStorageItem         = new PutStorageItem(storageUrl, containerName, remoteName, storageItemName);
            var putStorageItemResponse = new GenerateRequestByType().Submit(putStorageItem, authToken);

            Assert.That(putStorageItemResponse.Status, Is.EqualTo(HttpStatusCode.Created));
        }
コード例 #4
0
        public void Should_return_created_when_etag_is_not_supplied_because_it_is_optional()
        {
            using (TestHelper testHelper = new TestHelper(authToken, storageUrl))
            {
                var putStorageItem = new PutStorageItem(storageUrl, authToken, Constants.CONTAINER_NAME, Constants.StorageItemName, Constants.StorageItemName);
                putStorageItem.Headers.Remove("ETag");

                var response = new ResponseFactory<CloudFilesResponse>().Create(new CloudFilesRequest(putStorageItem));
                Assert.That(response.Status, Is.EqualTo(HttpStatusCode.Created));
                testHelper.DeleteItemFromContainer();
            }
        }
コード例 #5
0
 public void Should_return_created_when_a_stream_is_passed_instead_of_a_file_name()
 {
     using (var testHelper = new TestHelper(authToken, storageUrl))
     {
         var fs = new FileStream(Constants.StorageItemName, FileMode.Open, FileAccess.Read);
         var putStorageItem = new PutStorageItem(storageUrl, Constants.CONTAINER_NAME, Constants.StorageItemName, fs, null);
         var response = new GenerateRequestByType().Submit(putStorageItem, authToken);
         fs.Close();
         Assert.That(response.Status, Is.EqualTo(HttpStatusCode.Created));
         testHelper.DeleteItemFromContainer();
     }
 }
コード例 #6
0
        public void Should_return_created_as_status_when_the_file_does_not_already_exist()
        {
            using (var testHelper = new TestHelper(authToken, storageUrl))
            {
                var putStorageItem = new PutStorageItem(storageUrl, Constants.CONTAINER_NAME, Constants.StorageItemName, Constants.StorageItemName);

               // Assert.That(putStorageItem.ContentLength, Is.EqualTo(34)); //does not belong in this test

                var response = new GenerateRequestByType().Submit(putStorageItem, authToken);
                Assert.That(response.Status, Is.EqualTo(HttpStatusCode.Created));
                Assert.That(response.Headers[Constants.ETAG], Is.EqualTo(response.ETag));
                testHelper.DeleteItemFromContainer();
            }
        }
コード例 #7
0
        public void Should_return_created_as_status_when_the_file_does_not_already_exist()
        {
            using (TestHelper testHelper = new TestHelper(authToken, storageUrl))
            {
                var putStorageItem = new PutStorageItem(storageUrl, authToken, Constants.CONTAINER_NAME, Constants.StorageItemName, Constants.StorageItemName);

                Assert.That(putStorageItem.ContentLength, Is.EqualTo(34));

                var response = new ResponseFactory<CloudFilesResponse>().Create(new CloudFilesRequest(putStorageItem));
                Assert.That(response.Status, Is.EqualTo(HttpStatusCode.Created));
                Assert.That(response.Headers[Constants.ETAG], Is.EqualTo(putStorageItem.ETag));
                testHelper.DeleteItemFromContainer();
            }
        }
コード例 #8
0
        public void Should_return_created_when_content_length_is_not_supplied_because_it_is_optional()
        {
            using (var testHelper = new TestHelper(authToken, storageUrl))
            {
                var putStorageItem = new PutStorageItem(storageUrl, Constants.CONTAINER_NAME, Constants.StorageItemName, Constants.StorageItemName);
                var uri = putStorageItem.CreateUri();
                var request = new CloudFilesRequest(uri);
                putStorageItem.Apply(request);
             //   request.ContentLength = 0;
                request.Headers.Add("X-Auth-Token", HttpUtility.UrlEncode(authToken));
                var response= request.GetResponse();

                Assert.That(response.Status, Is.EqualTo(HttpStatusCode.Created));
                Assert.That(response.Headers[Constants.ETAG], Is.EqualTo(request.ETag));
                testHelper.DeleteItemFromContainer();
            }
        }
コード例 #9
0
        public void Should_throw_an_exception_when_the_container_name_exceeds_the_maximum_length()
        {
            string containerName = new string('a', Constants.MaximumContainerNameLength + 1);
            try
            {
                using (new TestHelper(authToken, storageUrl, containerName))
                {
                    var putStorageItem = new PutStorageItem(storageUrl, authToken, containerName, Constants.StorageItemName, Constants.StorageItemName);
                    Assert.That(putStorageItem.ContentLength, Is.GreaterThan(0));

                    var putStorageItemResponse = new ResponseFactory<CloudFilesResponse>().Create(new CloudFilesRequest(putStorageItem));
                    Assert.That(putStorageItemResponse.Status, Is.EqualTo(HttpStatusCode.Created));
                }
                Assert.Fail("ContainerNameException expected");
            }
            catch (Exception ex)
            {
                Assert.That(ex, Is.TypeOf(typeof (ContainerNameException)));
            }
        }
コード例 #10
0
 private void putStorageItem(string containerName, Stream storageStream, string remoteStorageItemName, Dictionary<string, string> metadata)
 {
     var putStorageItem = new PutStorageItem(StorageUrl, containerName, remoteStorageItemName, storageStream, metadata);
     foreach (var callback in _callbackFuncs)
     {
         putStorageItem.Progress += callback;
     }
     _requestfactory.Submit(putStorageItem, AuthToken, _usercreds.ProxyCredentials);
 }
コード例 #11
0
 public void PutItemInContainer(string storageItemName, string remoteName)
 {
     var putStorageItem = new PutStorageItem(storageUrl, authToken, containerName, remoteName, storageItemName);
     var putStorageItemResponse = new ResponseFactory<CloudFilesResponse>().Create(new CloudFilesRequest(putStorageItem));
     Assert.That(putStorageItemResponse.Status, Is.EqualTo(HttpStatusCode.Created));
 }
コード例 #12
0
 public void Should_throw_a_WebException_with_status_code_422_when_the_ETag_passed_does_not_match_MD5_of_the_file()
 {
     using (new TestHelper(authToken, storageUrl))
     {
         var putStorageItem = new PutStorageItem(storageUrl, authToken, Constants.CONTAINER_NAME, Constants.StorageItemName, Constants.StorageItemName);
         putStorageItem.Headers.Remove("ETag");
         putStorageItem.Headers.Add("ETag", new string('A', 32));
         try
         {
             new ResponseFactory<CloudFilesResponse>().Create(new CloudFilesRequest(putStorageItem));
         }
         catch (Exception ex)
         {
             Assert.That(ex, Is.TypeOf(typeof (WebException)));
         }
     }
 }
コード例 #13
0
 public void PutItemInContainer(string storageItemName, string remoteName)
 {
     var putStorageItem = new PutStorageItem(storageUrl, containerName, remoteName, storageItemName);
     var putStorageItemResponse = new GenerateRequestByType().Submit(putStorageItem, authToken);
     Assert.That(putStorageItemResponse.Status, Is.EqualTo(HttpStatusCode.Created));
 }
コード例 #14
0
        public void Should_throw_a_WebException_with_status_code_422_when_the_ETag_passed_does_not_match_MD5_of_the_file()
        {
            using (new TestHelper(authToken, storageUrl))
            {
                var putStorageItem = new PutStorageItem(storageUrl, Constants.CONTAINER_NAME, Constants.StorageItemName, Constants.StorageItemName);

                try
                {
                    var uri = putStorageItem.CreateUri();
                    var response = new CloudFilesRequest(uri);
                    putStorageItem.Apply(response);
                    response.Headers.Remove("ETag");
                    response.Headers.Add("ETag", new string('A', 32));
                    response.Headers.Add("X-Auth-Token", HttpUtility.UrlEncode(authToken));
                    response.GetResponse();
                  //  new GenerateRequestByType().Submit(putStorageItem, authToken);
                }
                catch (Exception ex)
                {
                    Assert.That(ex, Is.TypeOf(typeof (WebException)));
                }
            }
        }
コード例 #15
0
        public void Should_return_created_as_status()
        {
            var metadata = new Dictionary<string, string>
                                                      {
                                                          {Constants.MetadataKey, Constants.MetadataValue}
                                                      };
            using (var testHelper = new TestHelper(authToken, storageUrl))
            {
                var putStorageItem = new PutStorageItem(storageUrl, Constants.CONTAINER_NAME, Constants.StorageItemName, Constants.StorageItemName, metadata);

               // Assert.That(putStorageItem.ContentLength, Is.GreaterThan(0));

                var response = new GenerateRequestByType().Submit(putStorageItem, authToken);
                Assert.That(response.Status, Is.EqualTo(HttpStatusCode.Created));
                Assert.That(response.Headers[Constants.ETAG], Is.EqualTo(response.ETag));
                testHelper.DeleteItemFromContainer();
            }
        }
コード例 #16
0
        public void Should_still_come_back_as_pdf_even_when_sent_up_as_octet_stream()
        {
            using (var testHelper = new TestHelper(authToken, storageUrl))
            {
                var file = new FileInfo(Constants.StorageItemNamePdf);
                var metadata = new Dictionary<string, string> {{"Source", "1"}, {"Note", "2"}};
                const string DUMMY_FILE_NAME = "HAHAHA";

                var putStorageItem = new PutStorageItem(storageUrl, Constants.CONTAINER_NAME, DUMMY_FILE_NAME, file.Open(FileMode.Open), metadata);

             //   Assert.That(putStorageItem.ContentLength, Is.GreaterThan(0));
               // Assert.That(putStorageItem.ContentType, Is.EqualTo("application/octet-stream"));

                var response = new GenerateRequestByType().Submit(putStorageItem, authToken);
                Assert.That(response.Status, Is.EqualTo(HttpStatusCode.Created));
                Assert.That(response.Headers[Constants.ETAG], Is.EqualTo(response.ETag));

                var getStorageItem = new GetStorageItem(storageUrl, Constants.CONTAINER_NAME, DUMMY_FILE_NAME);
                var getStorageItemResponse = new GenerateRequestByType().Submit(getStorageItem, authToken);
                Assert.That(getStorageItemResponse.ContentType, Is.EqualTo("application/octet-stream"));
                getStorageItemResponse.Dispose();

                testHelper.DeleteItemFromContainer(DUMMY_FILE_NAME);
            }
        }
コード例 #17
0
        public void Should_set_content_type_of_gif_for_local_file_upload()
        {
            using (TestHelper testHelper = new TestHelper(authToken, storageUrl))
            {
                var putStorageItem = new PutStorageItem(storageUrl, authToken, Constants.CONTAINER_NAME, Constants.StorageItemNameGif, Constants.StorageItemNameGif);

                Assert.That(putStorageItem.ContentLength, Is.GreaterThan(0));
                Assert.That(putStorageItem.ContentType, Is.EqualTo("image/gif"));

                var response = new ResponseFactory<CloudFilesResponse>().Create(new CloudFilesRequest(putStorageItem));
                Assert.That(response.Status, Is.EqualTo(HttpStatusCode.Created));
                Assert.That(response.Headers[Constants.ETAG], Is.EqualTo(putStorageItem.ETag));
                testHelper.DeleteItemFromContainer(Constants.StorageItemNameGif);
            }
        }
コード例 #18
0
        public void Should_set_content_type_of_jpg_for_stream_upload()
        {
            using (var testHelper = new TestHelper(authToken, storageUrl))
            {
                var fileStream = new FileStream(Constants.StorageItemNameJpg, FileMode.Open, FileAccess.Read);
                var putStorageItem = new PutStorageItem(storageUrl, Constants.CONTAINER_NAME, Constants.StorageItemNameJpg, fileStream);

                var response = new GenerateRequestByType().Submit(putStorageItem, authToken);
                Assert.That(response.Status, Is.EqualTo(HttpStatusCode.Created));
                Assert.That(response.Headers[Constants.ETAG], Is.EqualTo(response.ETag));
                testHelper.DeleteItemFromContainer(Constants.StorageItemNameJpg);
            }
        }
コード例 #19
0
        public void Should_set_content_type_of_jpg_for_local_file_upload()
        {
            using (var testHelper = new TestHelper(authToken, storageUrl))
            {
                var putStorageItem = new PutStorageItem(storageUrl, Constants.CONTAINER_NAME, Constants.StorageItemNameJpg, Constants.StorageItemNameJpg);

            //    Assert.That(putStorageItem.ContentLength, Is.GreaterThan(0));
              //  Assert.That(putStorageItem.ContentType, Is.EqualTo("image/jpeg"));

                var response = new GenerateRequestByType().Submit(putStorageItem, authToken);
                Assert.That(response.Status, Is.EqualTo(HttpStatusCode.Created));
                Assert.That(response.Headers[Constants.ETAG], Is.EqualTo(response.ETag));
                testHelper.DeleteItemFromContainer(Constants.StorageItemNameJpg);
            }
        }
コード例 #20
0
 private void putStorageItem(string containerName, string localFilePath, Dictionary<string, string> metadata)
 {
     var remoteName = Path.GetFileName(localFilePath);
     var localName = localFilePath.Replace("/", "\\");
     var putStorageItem = new PutStorageItem(StorageUrl, containerName, remoteName, localName, metadata);
     foreach (var callback in _callbackFuncs)
     {
         putStorageItem.Progress += callback;
     }
     _requestfactory.Submit(putStorageItem, AuthToken, _usercreds.ProxyCredentials);
 }
コード例 #21
0
        /// <summary>
        /// This method uploads a storage object to cloudfiles with an alternate name
        /// </summary>
        /// <example>
        /// <code>
        /// UserCredentials userCredentials = new UserCredentials("username", "api key");
        /// IConnection connection = new Connection(userCredentials);
        /// Dictionary{string, string} metadata = new Dictionary{string, string}();
        /// metadata.Add("key1", "value1");
        /// metadata.Add("key2", "value2");
        /// metadata.Add("key3", "value3");
        /// FileInfo file = new FileInfo("C:\Local\File\Path\file.txt");
        /// connection.PutStorageItem("container name", file.Open(FileMode.Open), "RemoteFileName.txt", metadata);
        /// </code>
        /// </example>
        /// <param name="containerName">The name of the container to put the storage object in</param>
        /// <param name="storageStream">The file stream to upload</param>
        /// <param name="metadata">An optional parameter containing a dictionary of meta tags to associate with the storage object</param>
        /// <param name="remoteStorageItemName">The name of the storage object as it will be called on cloudfiles</param>
        /// <exception cref="ArgumentNullException">Thrown when any of the reference parameters are null</exception>
        public void PutStorageItem(string containerName, Stream storageStream, string remoteStorageItemName, Dictionary<string, string> metadata)
        {
            if (string.IsNullOrEmpty(containerName) ||
                string.IsNullOrEmpty(remoteStorageItemName))
                throw new ArgumentNullException();

            Log.Info(this, "Putting storage item stream with metadata into container '"
                + containerName + "' named "
                + remoteStorageItemName + " for user "
                + _usercreds.Username);

            try
            {
                var putStorageItem = new PutStorageItem(StorageUrl, containerName, remoteStorageItemName, storageStream, metadata);
                foreach (var callback in callbackFuncs)
                {
                    putStorageItem.Progress += callback;
                }
                _requestfactory.Submit(putStorageItem, AuthToken, _usercreds.ProxyCredentials);
            }
            catch (WebException webException)
            {
                Log.Error(this, "Error putting storage item stream with metadata into container '"
                    + containerName + "' named "
                    + remoteStorageItemName + " for user "
                    + _usercreds.Username, webException);

                var webResponse = (HttpWebResponse)webException.Response;
                if (webResponse == null) throw;
                if (webResponse.StatusCode == HttpStatusCode.BadRequest)
                    throw new ContainerNotFoundException("The requested container does not exist");
                if (webResponse.StatusCode == HttpStatusCode.PreconditionFailed)
                    throw new PreconditionFailedException(webException.Message);

                throw;
                //following exception is cause when status code is 422 (unprocessable entity)
                //unfortunately, the HttpStatusCode enum does not have that value
                //throw new InvalidETagException("The ETag supplied in the request does not match the ETag calculated by the server");
            }
        }
コード例 #22
0
        /// <summary>
        /// This method uploads a storage object to cloudfiles with meta tags
        /// </summary>
        /// <example>
        /// <code>
        /// UserCredentials userCredentials = new UserCredentials("username", "api key");
        /// IConnection connection = new Connection(userCredentials);
        /// Dictionary{string, string} metadata = new Dictionary{string, string}();
        /// metadata.Add("key1", "value1");
        /// metadata.Add("key2", "value2");
        /// metadata.Add("key3", "value3");
        /// connection.PutStorageItem("container name", "C:\Local\File\Path\file.txt", metadata);
        /// </code>
        /// </example>
        /// <param name="containerName">The name of the container to put the storage object in</param>
        /// <param name="localFilePath">The complete file path of the storage object to be uploaded</param>
        /// <param name="metadata">An optional parameter containing a dictionary of meta tags to associate with the storage object</param>
        /// <exception cref="ArgumentNullException">Thrown when any of the reference parameters are null</exception>
        public void PutStorageItem(string containerName, string localFilePath, Dictionary<string, string> metadata)
        {
            if (string.IsNullOrEmpty(containerName) ||
                string.IsNullOrEmpty(localFilePath))
                throw new ArgumentNullException();

            Log.Info(this, "Putting storage item "
                + localFilePath + " with metadata into container '"
                + containerName + "' for user "
                + _usercreds.Username);

            try
            {
                var remoteName = Path.GetFileName(localFilePath);
                var localName = localFilePath.Replace("/", "\\");
                var putStorageItem = new PutStorageItem(StorageUrl, containerName, remoteName, localName, metadata);
                foreach (var callback in callbackFuncs)
                {
                    putStorageItem.Progress += callback;
                }
                _requestfactory.Submit(putStorageItem, AuthToken, _usercreds.ProxyCredentials);
            }
            catch (WebException webException)
            {
                Log.Error(this, "Error putting storage item "
                    + localFilePath + " with metadata into container '"
                    + containerName + "' for user "
                    + _usercreds.Username, webException);

                var webResponse = (HttpWebResponse)webException.Response;
                if (webResponse == null) throw;
                if (webResponse.StatusCode == HttpStatusCode.BadRequest)
                    throw new ContainerNotFoundException("The requested container does not exist");
                if (webResponse.StatusCode == HttpStatusCode.PreconditionFailed)
                    throw new PreconditionFailedException(webException.Message);

                throw;
            }
        }