public async Task UploadBlob_StorageReturnsError_AppropriateExceptionIsThrown()
        {
            var mobileServiceHandler = new TestHttpHandler(HttpStatusCode.OK);
            var sasToken             = "http://the.sas.token.com/url?query=string";

            mobileServiceHandler.SetResponse("{\"uri\":\"" + sasToken + "\"}", "application/json");
            var client = new MobileServiceClient(
                AppUrl,
                AppKey,
                mobileServiceHandler);

            var storageHandler = new TestHttpHandler(HttpStatusCode.InternalServerError);

            storageHandler.SetResponse("An error occurred");
            ResourceBrokerStorageClient.HttpClientCreator = () => new HttpClient(storageHandler);

            var ms = GetBlobContent();

            try
            {
                var url = await client.UploadFileToBlobStorage("container", "file", "text/plain", ms);
            }
            catch (InvalidOperationException e)
            {
                Assert.IsNotNull(e.InnerException);
                Assert.IsInstanceOfType(e.InnerException, typeof(HttpRequestException));
            }
        }
        public async Task UploadBlob_OverloadWithResourceApiNameChangesUrl()
        {
            var mobileServiceHandler = new TestHttpHandler(HttpStatusCode.OK);
            var sasToken             = "http://the.sas.token.com/url?query=string";
            var sasTokenNoQuery      = "http://the.sas.token.com/url";

            mobileServiceHandler.SetResponse("{\"uri\":\"" + sasToken + "\"}", "application/json");
            var client = new MobileServiceClient(
                AppUrl,
                AppKey,
                mobileServiceHandler);

            var storageHandler = new TestHttpHandler(HttpStatusCode.Created);

            ResourceBrokerStorageClient.HttpClientCreator = () => new HttpClient(storageHandler);

            var ms  = GetBlobContent();
            var url = await client.UploadFileToBlobStorage("differentBrokerApi", "container", "file", "text/plain", ms);

            Assert.AreEqual(AppUrl + "api/differentBrokerApi/blob", mobileServiceHandler.RequestUrl);
        }
        public async Task UploadBlob_UrlReturnedFromBroker_IsUsedToUploadToStorage()
        {
            var mobileServiceHandler = new TestHttpHandler(HttpStatusCode.OK);
            var sasToken             = "http://the.sas.token.com/url?query=string";
            var sasTokenNoQuery      = "http://the.sas.token.com/url";

            mobileServiceHandler.SetResponse("{\"uri\":\"" + sasToken + "\"}", "application/json");
            var client = new MobileServiceClient(
                AppUrl,
                AppKey,
                mobileServiceHandler);

            var storageHandler = new TestHttpHandler(HttpStatusCode.Created);

            ResourceBrokerStorageClient.HttpClientCreator = () => new HttpClient(storageHandler);

            var ms  = GetBlobContent();
            var url = await client.UploadFileToBlobStorage("container", "file", "text/plain", ms);

            Assert.AreEqual(AppUrl + "api/resources/blob", mobileServiceHandler.RequestUrl);
            Assert.AreEqual(sasTokenNoQuery, url.ToString());
            Assert.AreEqual(sasToken, storageHandler.RequestUrl);
            Assert.AreEqual(testBlobContent, storageHandler.RequestContent);
        }
        public async Task UploadBlob_BrokerReturnsInvalidResponse_ExpectedExceptionIsCaught()
        {
            var mobileServiceHandler = new TestHttpHandler(HttpStatusCode.OK);

            mobileServiceHandler.SetResponse("{\"other\":\"value\"}", "application/json");
            var client = new MobileServiceClient(
                AppUrl,
                AppKey,
                mobileServiceHandler);

            var storageHandler = new TestHttpHandler(HttpStatusCode.Created);

            ResourceBrokerStorageClient.HttpClientCreator = () => new HttpClient(storageHandler);

            var ms = GetBlobContent();

            try
            {
                var url = await client.UploadFileToBlobStorage("container", "file", "text/plain", ms);

                Assert.Fail("Upload call should have thrown an exception");
            }
            catch (InvalidOperationException) { }
        }