예제 #1
0
        public void TestReadAndDeleteFile()
        {
            string fileName       = "my_file.txt";
            var    expectedResult = new Dictionary <string, object>();

            expectedResult["foo"] = "bar";
            string fileContents = @"{""foo"": ""bar""}";

            CreateFile(fileName, fileContents);

            var downloadLocation = new Uri(fileName, UriKind.RelativeOrAbsolute);
            var observer         = new MockBackgroundUploadResponseHandlerObserver(expectedResult, fileContents);

            var responseHandler =
                new BackgroundUploadResponseHandler(downloadLocation, observer);

            Action result = responseHandler.OnDoWork();

            result.Invoke();

            IDictionary <string, object> actualResult = observer.ActualResult;

            Assert.AreEqual(expectedResult["foo"], actualResult["foo"], "Did not receive the correct result.");

            Assert.AreEqual(fileContents, observer.ActualRawResult, "Did not receive the correct raw result");

            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                Assert.IsFalse(store.FileExists(fileName), "The created file was not deleted");
            }
        }
예제 #2
0
        public void TestReadAndDeleteFileWithError()
        {
            string fileName = "my_file.txt";
            var    error    = new Dictionary <string, object>();

            error["code"]    = "the code";
            error["message"] = "the message";

            var expectedResult = new Dictionary <string, object>();

            expectedResult["error"] = error;
            string fileContents = @"{""error"": {""code"": ""the code"", ""message"": ""the message""} }";

            CreateFile(fileName, fileContents);

            var downloadLocation = new Uri(fileName, UriKind.RelativeOrAbsolute);
            var observer         = new MockBackgroundUploadResponseHandlerObserver(expectedResult, fileContents);

            var responseHandler =
                new BackgroundUploadResponseHandler(downloadLocation, observer);

            Action result = responseHandler.OnDoWork();

            result.Invoke();

            observer.CheckOnErrorCalledCorrectly();

            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                Assert.IsFalse(store.FileExists(fileName), "The created file was not deleted");
            }
        }
        public void TestReadAndDeleteFileWithError()
        {
            string fileName = "my_file.txt";
            var error = new Dictionary<string,object>();
            error["code"] = "the code";
            error["message"] = "the message";

            var expectedResult = new Dictionary<string,object>();
            expectedResult["error"] = error;
            string fileContents = @"{""error"": {""code"": ""the code"", ""message"": ""the message""} }";
            CreateFile(fileName, fileContents);

            var downloadLocation = new Uri(fileName, UriKind.RelativeOrAbsolute);
            var observer = new MockBackgroundUploadResponseHandlerObserver(expectedResult, fileContents);

            var responseHandler =
                new BackgroundUploadResponseHandler(downloadLocation, observer);

            Action result = responseHandler.OnDoWork();
            result.Invoke();

            observer.CheckOnErrorCalledCorrectly();

            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                Assert.IsFalse(store.FileExists(fileName), "The created file was not deleted");
            }
        }
        public void TestReadAndDeleteFile()
        {
            string fileName = "my_file.txt";
            var expectedResult = new Dictionary<string, object>();
            expectedResult["foo"] = "bar";
            string fileContents = @"{""foo"": ""bar""}";
            CreateFile(fileName, fileContents);

            var downloadLocation = new Uri(fileName, UriKind.RelativeOrAbsolute);
            var observer = new MockBackgroundUploadResponseHandlerObserver(expectedResult, fileContents);

            var responseHandler =
                new BackgroundUploadResponseHandler(downloadLocation, observer);

            Action result = responseHandler.OnDoWork();
            result.Invoke();

            IDictionary<string, object> actualResult = observer.ActualResult;
            Assert.AreEqual(expectedResult["foo"], actualResult["foo"], "Did not receive the correct result.");

            Assert.AreEqual(fileContents, observer.ActualRawResult, "Did not receive the correct raw result");

            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                Assert.IsFalse(store.FileExists(fileName), "The created file was not deleted");
            }
        }
        public void TestOnErrorCalledWithException()
        {
            string fileName = "my_file.txt";
            Exception exception = new Exception();

            var downloadLocation = new Uri(fileName, UriKind.RelativeOrAbsolute);
            var observer = new MockBackgroundUploadResponseHandlerObserver(exception);

            var responseHandler = new BackgroundUploadResponseHandler(downloadLocation, observer);
            responseHandler.OnError(exception);

            observer.CheckOnErrorCalledCorrectly();
        }
예제 #6
0
        public void TestOnErrorCalledWithException()
        {
            string    fileName  = "my_file.txt";
            Exception exception = new Exception();

            var downloadLocation = new Uri(fileName, UriKind.RelativeOrAbsolute);
            var observer         = new MockBackgroundUploadResponseHandlerObserver(exception);

            var responseHandler = new BackgroundUploadResponseHandler(downloadLocation, observer);

            responseHandler.OnError(exception);

            observer.CheckOnErrorCalledCorrectly();
        }
        /// <summary>
        /// Called when a BackgroundTransferRequest's TransferStatus is set to Completed.
        /// This method will remove the request from the BackgroundTransferService and convert
        /// the result over to a LiveOperationResult and set it on the TaskCompletionSource.
        /// </summary>
        /// <param name="request"></param>
        private void OnTransferStatusComplete(BackgroundTransferRequest request)
        {
            Debug.Assert(request.TransferStatus == TransferStatus.Completed);
            Debug.Assert(BackgroundTransferHelper.IsUploadRequest(request));

            // Remove the transfer request in order to make room in the queue for more transfers.
            // Transfers are not automatically removed by the system.
            // Cancelled requests have already been removed from the system and cannot be removed twice.
            if (!BackgroundTransferHelper.IsCanceledRequest(request))
            {
                try
                {
                    this.backgroundTransferService.Remove(request);
                }
                catch (Exception exception)
                {
                    this.tcs.TrySetException(new LiveConnectException(
                        ApiOperation.ApiClientErrorCode,
                        ResourceHelper.GetString("BackgroundTransferServiceRemoveError"),
                        exception));
                    return;
                }
            }

            this.OnBackgroundTransferRequestCompleted(request);

            if (request.TransferError != null)
            {
                var exception = new LiveConnectException(ApiOperation.ApiServerErrorCode,
                                                         ResourceHelper.GetString("ServerError"),
                                                         request.TransferError);
                this.tcs.TrySetException(exception);
                return;
            }

            if (!BackgroundTransferHelper.IsSuccessfulStatusCode(request.StatusCode))
            {
                var exception = new LiveConnectException(ApiOperation.ApiServerErrorCode,
                                                         ResourceHelper.GetString("ServerError"));
                this.tcs.TrySetException(exception);
                return;
            }

            // Once we know we have a *good* upload, we have to send it to the response handler
            // to read it's JSON response body. We are an observer to this class, so it will call us back
            // with its result.
            var responseHandler = new BackgroundUploadResponseHandler(
                request.DownloadLocation,
                this);
            responseHandler.ReadJsonResponseFromDownloadLocation();
        }