/// <summary>
        /// Checks a get blob range response for consistency with the given parameters, and closes the response.
        /// </summary>
        /// <param name="response">The HTTP web response to check.</param>
        /// <param name="context">The testing context.</param>
        /// <param name="content">The expected content returned in the response.</param>
        /// <param name="expectedStartRange">The expected start range returned in the response header.</param>
        /// <param name="expectedEndRange">The expected end range returned in the response header.</param>
        /// <param name="expectedTotalBytes">The expected total number of bytes in the blob.</param>
        /// <param name="expectedError">The expected error code, or null if the operation is expected to succeed.</param>
        public static void CheckBlobRangeResponse(
            HttpWebResponse response,
            BlobContext context,
            byte[] content,
            long expectedStartRange,
            long expectedEndRange,
            long expectedTotalBytes,
            HttpStatusCode?expectedError)
        {
            Assert.IsNotNull(response);
            if (expectedError == null)
            {
                Assert.AreEqual(HttpStatusCode.PartialContent, response.StatusCode);
                Assert.IsNotNull(content);
                BlobTestUtils.LastModifiedHeader(response);
                BlobTestUtils.ContentLengthHeader(response, content.Length);
                BlobTestUtils.ETagHeader(response);
                BlobTestUtils.RequestIdHeader(response);
                BlobTestUtils.Contents(response, content);
                BlobTestUtils.ContentRangeHeader(response, expectedStartRange, expectedEndRange, expectedTotalBytes);
            }
            else
            {
                Assert.AreEqual(expectedError, response.StatusCode, response.StatusDescription);
            }

            response.Close();
        }
        public static HttpWebRequest GetBlobRequest(BlobContext context, string containerName, string blobName, AccessCondition accessCondition)
        {
            bool valid = BlobTests.ContainerNameValidator(containerName) &&
                         BlobTests.BlobNameValidator(blobName) &&
                         BlobTests.LeaseIdValidator(accessCondition);

            Uri              uri       = BlobTests.ConstructGetUri(context.Address, containerName, blobName);
            HttpWebRequest   request   = null;
            OperationContext opContext = new OperationContext();

            try
            {
                request = BlobHttpWebRequestFactory.Get(uri, context.Timeout, null /* snapshot */, accessCondition, opContext);
            }
            catch (InvalidOperationException)
            {
                if (valid)
                {
                    Assert.Fail();
                }
            }
            if (valid)
            {
                Assert.IsNotNull(request);
                Assert.IsNotNull(request.Method);
                Assert.AreEqual("GET", request.Method);
                BlobTestUtils.RangeHeader(request, null);
                BlobTestUtils.LeaseIdHeader(request, accessCondition == null ? null : accessCondition.LeaseId);
            }
            return(request);
        }
Exemple #3
0
        public void CopyFromToRestoreSnapshot(BlobContext context, string containerName, string blobName)
        {
            string oldText = "Old stuff";
            string newText = "New stuff";

            StorageCredentials  accountAndKey = new StorageCredentials(context.Account, context.Key);
            CloudStorageAccount account       = new CloudStorageAccount(accountAndKey, false);
            CloudBlobClient     blobClient    = new CloudBlobClient(new Uri(context.Address), account.Credentials);
            CloudBlobContainer  container     = blobClient.GetContainerReference(containerName);
            CloudBlockBlob      blob          = container.GetBlockBlobReference(blobName);

            BlobTestBase.UploadText(blob, oldText, Encoding.UTF8);
            CloudBlockBlob snapshot = blob.CreateSnapshot();

            Assert.IsNotNull(snapshot.SnapshotTime);
            BlobTestBase.UploadText(blob, newText, Encoding.UTF8);

            Uri sourceUri = new Uri(snapshot.Uri.AbsoluteUri + "?snapshot=" + BlobRequest.ConvertDateTimeToSnapshotString(snapshot.SnapshotTime.Value));
            OperationContext opContext = new OperationContext();
            HttpWebRequest   request   = BlobHttpWebRequestFactory.CopyFrom(blob.Uri, 30, sourceUri, null, null, opContext);

            Assert.IsTrue(request != null, "Failed to create HttpWebRequest");
            BlobTests.SignRequest(request, context);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            Assert.AreEqual <HttpStatusCode>(response.StatusCode, HttpStatusCode.Accepted);

            string text = BlobTestBase.DownloadText(blob, Encoding.UTF8);

            Assert.AreEqual <string>(text, oldText);

            blob.Delete(DeleteSnapshotsOption.IncludeSnapshots, null, null);
        }
Exemple #4
0
        /// <summary>
        /// Generates a get blob request over the specified range, and checks the request for consistency.
        /// </summary>
        /// <param name="context">The testing context.</param>
        /// <param name="containerName">The name of the container.</param>
        /// <param name="blobName">The name of the blob.</param>
        /// <param name="offset">The offset to the range.</param>
        /// <param name="count">The number of elements in the range.</param>
        /// <param name="leaseId">The lease ID, or null if the blob is not leased.</param>
        /// <returns>A web request for getting a blob range.</returns>
        public static HttpRequestMessage GetBlobRangeRequest(BlobContext context, string containerName, string blobName, long offset, long?count, AccessCondition accessCondition)
        {
            bool valid = BlobTests.ContainerNameValidator(containerName) &&
                         BlobTests.BlobNameValidator(blobName) &&
                         BlobTests.LeaseIdValidator(accessCondition);

            Uri uri = BlobTests.ConstructGetUri(context.Address, containerName, blobName);
            HttpRequestMessage request   = null;
            OperationContext   opContext = new OperationContext();

            try
            {
                request = BlobHttpRequestMessageFactory.Get(uri, context.Timeout, null /* snapshot */, offset, count, false, accessCondition, null, opContext, SharedKeyCanonicalizer.Instance, context.Credentials);
            }
            catch (InvalidOperationException)
            {
                if (valid)
                {
                    Assert.Fail();
                }
            }

            if (valid)
            {
                Assert.IsNotNull(request);
                Assert.IsNotNull(request.Method);
                Assert.AreEqual(HttpMethod.Get, request.Method);
                BlobTestUtils.RangeHeader(request, offset, count.HasValue ? (long?)(count.Value + offset - 1) : null);
                BlobTestUtils.LeaseIdHeader(request, accessCondition == null ? null : accessCondition.LeaseId);
            }

            return(request);
        }
Exemple #5
0
        public async static void AcquireAndReleaseLeaseTest(BlobContext context, string containerName, string blobName)
        {
            CloudStorageAccount account   = new CloudStorageAccount(new StorageCredentials(context.Account, context.Key), false);
            CloudBlobClient     client    = new CloudBlobClient(new Uri(context.Address), account.Credentials);
            CloudBlobContainer  container = client.GetContainerReference(containerName);
            CloudBlockBlob      blob      = container.GetBlockBlobReference(blobName);

            BlobTestBase.UploadText(blob, "Text sent to cloud", Encoding.UTF8);

            // acquire a release on the blob and check LeaseStatus to be "locked"
            OperationContext   opContext   = new OperationContext();
            HttpRequestMessage blobRequest = BlobHttpRequestMessageFactory.Lease(
                blob.Uri,
                context.Timeout,
                LeaseAction.Acquire,
                null /* proposed lease ID */,
                60 /* lease duration */,
                null /* break period */,
                null /* access condition */,
                null,
                opContext,
                SharedKeyCanonicalizer.Instance,
                context.Credentials);

            string leaseId = null;

            using (HttpResponseMessage response = await BlobTestUtils.GetResponse(blobRequest, context))
            {
                leaseId = HttpResponseParsers.GetHeader(response, "x-ms-lease-id");
                Assert.AreEqual <HttpStatusCode>(response.StatusCode, HttpStatusCode.Created);
            }

            blob.FetchAttributes();
            Assert.AreEqual <LeaseStatus>(blob.Properties.LeaseStatus, LeaseStatus.Locked);

            // release the release on the blob and check LeaseStatus to be "unlocked"
            opContext   = new OperationContext();
            blobRequest = BlobHttpRequestMessageFactory.Lease(
                blob.Uri,
                context.Timeout,
                LeaseAction.Release,
                null /* proposed lease ID */,
                null /* lease duration */,
                null /* break period */,
                AccessCondition.GenerateLeaseCondition(leaseId),
                null,
                opContext,
                SharedKeyCanonicalizer.Instance,
                context.Credentials);
            using (HttpResponseMessage response = await BlobTestUtils.GetResponse(blobRequest, context))
            {
                Assert.AreEqual <HttpStatusCode>(response.StatusCode, HttpStatusCode.OK);
            }

            blob.FetchAttributes();
            Assert.AreEqual <LeaseStatus>(blob.Properties.LeaseStatus, LeaseStatus.Unlocked);

            blob.Delete();
        }
 static void SetRequestAsync(HttpWebRequest request, BlobContext context, byte[] content)
 {
     request.BeginGetRequestStream(new AsyncCallback(BlobTestUtils.ReadCallback), request);
     setRequestAsyncSem.WaitOne();
     Assert.IsNotNull(setRequestAsyncStream);
     setRequestAsyncStream.Write(content, 0, content.Length);
     setRequestAsyncStream.Close();
 }
        static void SetRequestSync(HttpWebRequest request, BlobContext context, byte[] content)
        {
            Stream stream = request.GetRequestStream();

            Assert.IsNotNull(stream);
            stream.Write(content, 0, content.Length);
            stream.Close();
        }
 public static void SetRequest(HttpWebRequest request, BlobContext context, byte[] content)
 {
     Assert.IsNotNull(request);
     if (context.IsAsync)
     {
         BlobTestUtils.SetRequestAsync(request, context, content);
     }
     else
     {
         BlobTestUtils.SetRequestSync(request, context, content);
     }
 }
        public static HttpWebRequest DeleteBlobRequest(BlobContext context, string containerName, string blobName, AccessCondition accessCondition)
        {
            Uri uri = BlobClientTests.ConstructUri(context.Address, containerName, blobName);
            OperationContext opContext = new OperationContext();
            HttpWebRequest   request   = BlobHttpWebRequestFactory.Delete(uri, context.Timeout, null /* snapshot */, DeleteSnapshotsOption.None, accessCondition, true, opContext);

            Assert.IsNotNull(request);
            Assert.IsNotNull(request.Method);
            Assert.AreEqual("DELETE", request.Method);
            BlobTestUtils.RangeHeader(request, null);
            BlobTestUtils.LeaseIdHeader(request, null);
            return(request);
        }
        public static HttpWebRequest GetBlockListRequest(BlobContext context, string containerName, string blobName, BlockListingFilter typesOfBlocks, AccessCondition accessCondition)
        {
            Uri uri = BlobClientTests.ConstructUri(context.Address, containerName, blobName);
            OperationContext opContext = new OperationContext();
            HttpWebRequest   request   = BlobHttpWebRequestFactory.GetBlockList(uri, context.Timeout, null /* snapshot */, typesOfBlocks, accessCondition, opContext);

            Assert.IsNotNull(request);
            Assert.IsNotNull(request.Method);
            Assert.AreEqual("GET", request.Method);
            BlobTestUtils.RangeHeader(request, null);
            BlobTestUtils.LeaseIdHeader(request, null);
            return(request);
        }
        public static HttpWebRequest ListContainersRequest(BlobContext context, ListingContext listingContext)
        {
            Uri uri = BlobClientTests.ConstructUri(context.Address);
            OperationContext opContext = new OperationContext();
            HttpWebRequest   request   = ContainerHttpWebRequestFactory.List(uri, context.Timeout, listingContext, ContainerListingDetails.Metadata, opContext);

            Assert.IsNotNull(request);
            Assert.IsNotNull(request.Method);
            Assert.AreEqual("GET", request.Method);
            BlobTestUtils.RangeHeader(request, null);
            BlobTestUtils.LeaseIdHeader(request, null);
            return(request);
        }
        public static HttpWebRequest CreateContainerRequest(BlobContext context, string containerName)
        {
            Uri uri = BlobClientTests.ConstructUri(context.Address, containerName);
            OperationContext opContext = new OperationContext();
            HttpWebRequest   request   = ContainerHttpWebRequestFactory.Create(uri, context.Timeout, opContext);

            Assert.IsNotNull(request);
            Assert.IsNotNull(request.Method);
            Assert.AreEqual("PUT", request.Method);
            BlobTestUtils.RangeHeader(request, null);
            BlobTestUtils.LeaseIdHeader(request, null);
            return(request);
        }
 public static void GetBlockListResponse(HttpWebResponse response, BlobContext context, HttpStatusCode?expectedError)
 {
     Assert.IsNotNull(response);
     if (expectedError == null)
     {
         Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, response.StatusDescription);
         BlobTestUtils.ContentTypeHeader(response, "application/xml");
         BlobTestUtils.RequestIdHeader(response);
     }
     else
     {
         Assert.AreEqual(expectedError, response.StatusCode, response.StatusDescription);
     }
 }
Exemple #14
0
 public static void ListContainersResponse(HttpResponseMessage response, BlobContext context, HttpStatusCode?expectedError)
 {
     Assert.IsNotNull(response);
     if (expectedError == null)
     {
         Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, response.ReasonPhrase);
         BlobTestUtils.ContentTypeHeader(response, "application/xml");
         BlobTestUtils.RequestIdHeader(response);
     }
     else
     {
         Assert.AreEqual(expectedError, response.StatusCode, response.ReasonPhrase);
     }
 }
Exemple #15
0
        /// <summary>
        /// Generates and validates a request to break a lease.
        /// </summary>
        /// <param name="context">The blob context.</param>
        /// <param name="containerName">The container name.</param>
        /// <param name="blobName">The blob name.</param>
        /// <param name="breakPeriod">The break period.</param>
        /// <returns>A web request for the operation.</returns>
        public static HttpRequestMessage BreakLeaseRequest(BlobContext context, string containerName, string blobName, int?breakPeriod, AccessCondition accessCondition)
        {
            HttpRequestMessage request;
            OperationContext   opContext = new OperationContext();

            if (blobName != null)
            {
                // blob lease
                Uri uri = BlobTests.ConstructPutUri(context.Address, containerName, blobName);
                request = BlobHttpRequestMessageFactory.Lease(
                    uri,
                    context.Timeout,
                    LeaseAction.Break,
                    null /* proposed lease ID */,
                    null /* lease duration */,
                    breakPeriod,
                    accessCondition,
                    null,
                    opContext,
                    SharedKeyCanonicalizer.Instance,
                    context.Credentials);
            }
            else
            {
                // container lease
                Uri uri = BlobClientTests.ConstructUri(context.Address, containerName);
                request = ContainerHttpRequestMessageFactory.Lease(
                    uri,
                    context.Timeout,
                    LeaseAction.Break,
                    null /* proposed lease ID */,
                    null /* lease duration */,
                    breakPeriod,
                    accessCondition,
                    null,
                    opContext,
                    SharedKeyCanonicalizer.Instance,
                    context.Credentials);
            }
            Assert.IsNotNull(request);
            Assert.AreEqual(HttpMethod.Put, request.Method);
            BlobTestUtils.VersionHeader(request, false);
            BlobTestUtils.LeaseIdHeader(request, null);
            BlobTestUtils.LeaseActionHeader(request, "break");
            BlobTestUtils.LeaseDurationHeader(request, null);
            BlobTestUtils.ProposedLeaseIdHeader(request, null);
            BlobTestUtils.BreakPeriodHeader(request, breakPeriod.HasValue ? breakPeriod.Value.ToString() : null);
            return(request);
        }
        public static HttpWebRequest PutBlockListRequest(BlobContext context, string containerName, string blobName, BlobProperties blobProperties, AccessCondition accessCondition)
        {
            Uri              uri       = BlobTests.ConstructPutUri(context.Address, containerName, blobName);
            HttpWebRequest   request   = null;
            OperationContext opContext = new OperationContext();

            request = BlobHttpWebRequestFactory.PutBlockList(uri, context.Timeout, blobProperties, accessCondition, opContext);
            Assert.IsNotNull(request);
            Assert.IsNotNull(request.Method);
            Assert.AreEqual("PUT", request.Method);
            BlobTestUtils.VersionHeader(request, false);
            BlobTestUtils.ContentLanguageHeader(request, null);
            BlobTestUtils.ContentMd5Header(request, null);
            return(request);
        }
Exemple #17
0
        public static HttpRequestMessage PutBlockListRequest(BlobContext context, string containerName, string blobName, BlobProperties blobProperties, AccessCondition accessCondition)
        {
            Uri uri = BlobTests.ConstructPutUri(context.Address, containerName, blobName);
            HttpRequestMessage request   = null;
            OperationContext   opContext = new OperationContext();

            request = BlobHttpRequestMessageFactory.PutBlockList(uri, context.Timeout, blobProperties, accessCondition, null, opContext, SharedKeyCanonicalizer.Instance, context.Credentials);
            Assert.IsNotNull(request);
            Assert.IsNotNull(request.Method);
            Assert.AreEqual(HttpMethod.Put, request.Method);
            BlobTestUtils.VersionHeader(request, false);
            BlobTestUtils.ContentLanguageHeader(request, null);
            BlobTestUtils.ContentMd5Header(request, null);
            return(request);
        }
 public static void PutBlockListResponse(HttpWebResponse response, BlobContext context, HttpStatusCode?expectedError)
 {
     Assert.IsNotNull(response);
     if (expectedError == null)
     {
         Assert.AreEqual(HttpStatusCode.Created, response.StatusCode, response.StatusDescription);
         BlobTestUtils.ContentMd5Header(response);
         BlobTestUtils.RequestIdHeader(response);
         BlobTestUtils.ContentLengthHeader(response, -1);
     }
     else
     {
         Assert.AreEqual(expectedError, response.StatusCode, response.StatusDescription);
     }
     response.Close();
 }
Exemple #19
0
        public static HttpRequestMessage DeleteContainerRequest(BlobContext context, string containerName, AccessCondition accessCondition)
        {
            Uri uri = BlobClientTests.ConstructUri(context.Address, containerName);
            OperationContext   opContext = new OperationContext();
            HttpRequestMessage request   = ContainerHttpRequestMessageFactory.Delete(uri, context.Timeout, accessCondition, null,
                                                                                     opContext,
                                                                                     SharedKeyCanonicalizer.Instance,
                                                                                     context.Credentials);

            Assert.IsNotNull(request);
            Assert.IsNotNull(request.Method);
            Assert.AreEqual(HttpMethod.Delete, request.Method);
            BlobTestUtils.RangeHeader(request, null);
            BlobTestUtils.LeaseIdHeader(request, null);
            return(request);
        }
        public static HttpWebResponse GetResponse(HttpWebRequest request, BlobContext context)
        {
            Assert.IsNotNull(request);
            HttpWebResponse response = null;

            try
            {
                response = (HttpWebResponse)request.GetResponse();
            }
            catch (WebException ex)
            {
                response = (HttpWebResponse)ex.Response;
            }
            Assert.IsNotNull(response);
            return(response);
        }
Exemple #21
0
        public static HttpRequestMessage ListContainersRequest(BlobContext context, ListingContext listingContext)
        {
            Uri uri = BlobClientTests.ConstructUri(context.Address);
            OperationContext   opContext = new OperationContext();
            HttpRequestMessage request   = ContainerHttpRequestMessageFactory.List(uri, context.Timeout, listingContext, ContainerListingDetails.Metadata, null,
                                                                                   opContext,
                                                                                   SharedKeyCanonicalizer.Instance,
                                                                                   context.Credentials);

            Assert.IsNotNull(request);
            Assert.IsNotNull(request.Method);
            Assert.AreEqual(HttpMethod.Get, request.Method);
            BlobTestUtils.RangeHeader(request, null);
            BlobTestUtils.LeaseIdHeader(request, null);
            return(request);
        }
        public static void SignRequest(HttpWebRequest request, BlobContext context)
        {
            Assert.IsNotNull(request);
            Assert.IsNotNull(context);

            OperationContext opContext             = new OperationContext();
            SharedKeyAuthenticationHandler handler = new SharedKeyAuthenticationHandler(
                SharedKeyCanonicalizer.Instance,
                context.Credentials,
                context.Account);

            handler.SignRequest(request, opContext);

            BlobTestUtils.AuthorizationHeader(request, true, context.Account);
            BlobTestUtils.DateHeader(request, true);
        }
Exemple #23
0
        public static HttpRequestMessage GetBlockListRequest(BlobContext context, string containerName, string blobName, BlockListingFilter typesOfBlocks, AccessCondition accessCondition)
        {
            Uri uri = BlobClientTests.ConstructUri(context.Address, containerName, blobName);
            OperationContext   opContext = new OperationContext();
            HttpRequestMessage request   = BlobHttpRequestMessageFactory.GetBlockList(uri, context.Timeout, null /* snapshot */, typesOfBlocks, accessCondition, null,
                                                                                      opContext,
                                                                                      SharedKeyCanonicalizer.Instance,
                                                                                      context.Credentials);

            Assert.IsNotNull(request);
            Assert.IsNotNull(request.Method);
            Assert.AreEqual(HttpMethod.Get, request.Method);
            BlobTestUtils.RangeHeader(request, null);
            BlobTestUtils.LeaseIdHeader(request, null);
            return(request);
        }
Exemple #24
0
 public static void PutBlockListResponse(HttpResponseMessage response, BlobContext context, HttpStatusCode?expectedError)
 {
     Assert.IsNotNull(response);
     if (expectedError == null)
     {
         Assert.AreEqual(HttpStatusCode.Created, response.StatusCode, response.ReasonPhrase);
         BlobTestUtils.ContentMd5Header(response);
         BlobTestUtils.RequestIdHeader(response);
         BlobTestUtils.ContentLengthHeader(response, 0);
     }
     else
     {
         Assert.AreEqual(expectedError, response.StatusCode, response.ReasonPhrase);
     }
     response.Dispose();
 }
        public static HttpWebRequest PutBlobRequest(BlobContext context, string containerName, string blobName,
                                                    BlobProperties properties, BlobType blobType, byte[] content, long pageBlobSize, AccessCondition accessCondition)
        {
            bool valid = BlobTests.ContainerNameValidator(containerName) &&
                         BlobTests.BlobNameValidator(blobName) &&
                         BlobTests.PutPropertiesValidator(properties) &&
                         BlobTestUtils.ContentValidator(content);

            bool fatal = !BlobTests.PutPropertiesValidator(properties);

            Uri              uri       = BlobTests.ConstructPutUri(context.Address, containerName, blobName);
            HttpWebRequest   request   = null;
            OperationContext opContext = new OperationContext();

            try
            {
                request = BlobHttpWebRequestFactory.Put(uri, context.Timeout, properties, blobType, pageBlobSize, accessCondition, opContext);
                if (fatal)
                {
                    Assert.Fail();
                }
            }
            catch (InvalidOperationException)
            {
                if (valid)
                {
                    Assert.Fail();
                }
            }
            if (valid)
            {
                Assert.IsNotNull(request);
                Assert.IsNotNull(request.Method);
                Assert.AreEqual("PUT", request.Method);
                BlobTestUtils.VersionHeader(request, false);
                BlobTestUtils.ContentTypeHeader(request, null);
                BlobTestUtils.ContentDispositionHeader(request, properties.ContentDisposition);
                BlobTestUtils.ContentEncodingHeader(request, properties.ContentEncoding);
                BlobTestUtils.ContentLanguageHeader(request, null);
                BlobTestUtils.ContentMd5Header(request, null);
                BlobTestUtils.CacheControlHeader(request, null);
                BlobTestUtils.BlobTypeHeader(request, properties.BlobType);
                BlobTestUtils.BlobSizeHeader(request, (properties.BlobType == BlobType.PageBlob) ? properties.Length : (long?)null);
            }
            return(request);
        }
        public static HttpWebRequest PutBlobRequest(BlobContext context, string containerName, string blobName,
            BlobProperties properties, BlobType blobType, byte[] content, long pageBlobSize, AccessCondition accessCondition)
        {
            bool valid = BlobTests.ContainerNameValidator(containerName) &&
                BlobTests.BlobNameValidator(blobName) &&
                BlobTests.PutPropertiesValidator(properties) &&
                BlobTestUtils.ContentValidator(content);

            bool fatal = !BlobTests.PutPropertiesValidator(properties);

            Uri uri = BlobTests.ConstructPutUri(context.Address, containerName, blobName);
            HttpWebRequest request = null;
            OperationContext opContext = new OperationContext();
            try
            {
                request = BlobHttpWebRequestFactory.Put(uri, context.Timeout, properties, blobType, pageBlobSize, accessCondition, opContext);
                if (fatal)
                {
                    Assert.Fail();
                }
            }
            catch (InvalidOperationException)
            {
                if (valid)
                {
                    Assert.Fail();
                }
            }
            if (valid)
            {
                Assert.IsNotNull(request);
                Assert.IsNotNull(request.Method);
                Assert.AreEqual("PUT", request.Method);
                BlobTestUtils.VersionHeader(request, false);
                BlobTestUtils.ContentTypeHeader(request, null);
                BlobTestUtils.ContentDispositionHeader(request, properties.ContentDisposition);
                BlobTestUtils.ContentEncodingHeader(request, properties.ContentEncoding);
                BlobTestUtils.ContentLanguageHeader(request, null);
                BlobTestUtils.ContentMd5Header(request, null);
                BlobTestUtils.CacheControlHeader(request, null);
                BlobTestUtils.BlobTypeHeader(request, properties.BlobType);
                BlobTestUtils.BlobSizeHeader(request, (properties.BlobType == BlobType.PageBlob) ? properties.Length : (long?)null);
            }
            return request;
        }
 public static void PutBlobResponse(HttpWebResponse response, BlobContext context, HttpStatusCode? expectedError)
 {
     Assert.IsNotNull(response);
     if (expectedError == null)
     {
         Assert.AreEqual(HttpStatusCode.Created, response.StatusCode, response.StatusDescription);
         BlobTestUtils.ETagHeader(response);
         BlobTestUtils.LastModifiedHeader(response);
         BlobTestUtils.ContentMd5Header(response);
         BlobTestUtils.RequestIdHeader(response);
         BlobTestUtils.ContentLengthHeader(response, -1); //Should this be -1 or 0?
     }
     else
     {
         Assert.AreEqual(expectedError, response.StatusCode, response.StatusDescription);
     }
     response.Close();
 }
 public static void GetBlobResponse(HttpWebResponse response, BlobContext context, BlobProperties properties, byte[] content, HttpStatusCode?expectedError)
 {
     Assert.IsNotNull(response);
     if (expectedError == null)
     {
         Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
         BlobTestUtils.LastModifiedHeader(response);
         BlobTestUtils.ContentLengthHeader(response, content.Length);
         BlobTestUtils.ETagHeader(response);
         BlobTestUtils.RequestIdHeader(response);
         BlobTestUtils.Contents(response, content);
     }
     else
     {
         Assert.AreEqual(expectedError, response.StatusCode, response.StatusDescription);
     }
     response.Close();
 }
        /// <summary>
        /// Generates and validates a request to release a lease.
        /// </summary>
        /// <param name="context">The blob context.</param>
        /// <param name="containerName">The container name.</param>
        /// <param name="blobName">The blob name.</param>
        /// <param name="leaseID">The lease ID.</param>
        /// <returns>A web request for the operation.</returns>
        public static HttpWebRequest ReleaseLeaseRequest(BlobContext context, string containerName, string blobName, AccessCondition accessCondition)
        {
            HttpWebRequest   request;
            OperationContext opContext = new OperationContext();

            if (blobName != null)
            {
                // blob lease
                Uri uri = BlobTests.ConstructPutUri(context.Address, containerName, blobName);
                request = BlobHttpWebRequestFactory.Lease(
                    uri,
                    context.Timeout,
                    LeaseAction.Release,
                    null /* proposed lease ID */,
                    null /* lease duration */,
                    null /* break period */,
                    accessCondition,
                    opContext);
            }
            else
            {
                // container lease
                Uri uri = BlobClientTests.ConstructUri(context.Address, containerName);
                request = ContainerHttpWebRequestFactory.Lease(
                    uri,
                    context.Timeout,
                    LeaseAction.Release,
                    null /* proposed lease ID */,
                    null /* lease duration */,
                    null /* break period */,
                    accessCondition,
                    opContext);
            }
            Assert.IsNotNull(request);
            Assert.AreEqual("PUT", request.Method);
            BlobTestUtils.VersionHeader(request, false);
            BlobTestUtils.LeaseIdHeader(request, accessCondition == null ? null : accessCondition.LeaseId);
            BlobTestUtils.LeaseActionHeader(request, "release");
            BlobTestUtils.LeaseDurationHeader(request, null);
            BlobTestUtils.ProposedLeaseIdHeader(request, null);
            BlobTestUtils.BreakPeriodHeader(request, null);
            return(request);
        }
 public static void GetBlockListResponse(HttpWebResponse response, BlobContext context, HttpStatusCode? expectedError)
 {
     Assert.IsNotNull(response);
     if (expectedError == null)
     {
         Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, response.StatusDescription);
         BlobTestUtils.ContentTypeHeader(response, "application/xml");
         BlobTestUtils.RequestIdHeader(response);
     }
     else
     {
         Assert.AreEqual(expectedError, response.StatusCode, response.StatusDescription);
     }
 }
        public static void SignRequest(HttpWebRequest request, BlobContext context)
        {
            Assert.IsNotNull(request);
            Assert.IsNotNull(context);

            OperationContext opContext = new OperationContext();
            SharedKeyAuthenticationHandler handler = new SharedKeyAuthenticationHandler(
                SharedKeyCanonicalizer.Instance,
                context.Credentials,
                context.Account);
            handler.SignRequest(request, opContext);

            BlobTestUtils.AuthorizationHeader(request, true, context.Account);
            BlobTestUtils.DateHeader(request, true);
        }
        public static async Task <HttpResponseMessage> GetResponse(HttpRequestMessage request, BlobContext context, CancellationTokenSource token = null)
        {
            Assert.IsNotNull(request);
            HttpResponseMessage response = null;

            try
            {
                var httpClient = HttpClientFactory.Instance;

                if (token != null)
                {
                    response = await httpClient.SendAsync(request, token.Token);
                }
                else
                {
                    response = await httpClient.SendAsync(request);
                }
            }
            catch (HttpRequestException)
            {
            }
            Assert.IsNotNull(response);
            return(response);
        }
 public static HttpWebRequest GetBlockListRequest(BlobContext context, string containerName, string blobName, BlockListingFilter typesOfBlocks, AccessCondition accessCondition)
 {
     Uri uri = BlobClientTests.ConstructUri(context.Address, containerName, blobName);
     OperationContext opContext = new OperationContext();
     HttpWebRequest request = BlobHttpWebRequestFactory.GetBlockList(uri, context.Timeout, null /* snapshot */, typesOfBlocks, accessCondition, opContext);
     Assert.IsNotNull(request);
     Assert.IsNotNull(request.Method);
     Assert.AreEqual("GET", request.Method);
     BlobTestUtils.RangeHeader(request, null);
     BlobTestUtils.LeaseIdHeader(request, null);
     return request;
 }
 public static HttpWebRequest DeleteBlobRequest(BlobContext context, string containerName, string blobName, AccessCondition accessCondition)
 {
     Uri uri = BlobClientTests.ConstructUri(context.Address, containerName, blobName);
     OperationContext opContext = new OperationContext();
     HttpWebRequest request = BlobHttpWebRequestFactory.Delete(uri, context.Timeout, null /* snapshot */, DeleteSnapshotsOption.None, accessCondition, opContext);
     Assert.IsNotNull(request);
     Assert.IsNotNull(request.Method);
     Assert.AreEqual("DELETE", request.Method);
     BlobTestUtils.RangeHeader(request, null);
     BlobTestUtils.LeaseIdHeader(request, null);
     return request;
 }
 public static HttpWebRequest ListContainersRequest(BlobContext context, ListingContext listingContext)
 {
     Uri uri = BlobClientTests.ConstructUri(context.Address);
     OperationContext opContext = new OperationContext();
     HttpWebRequest request = ContainerHttpWebRequestFactory.List(uri, context.Timeout, listingContext, ContainerListingDetails.Metadata, opContext);
     Assert.IsNotNull(request);
     Assert.IsNotNull(request.Method);
     Assert.AreEqual("GET", request.Method);
     BlobTestUtils.RangeHeader(request, null);
     BlobTestUtils.LeaseIdHeader(request, null);
     return request;
 }
 public static HttpWebResponse GetResponse(HttpWebRequest request, BlobContext context)
 {
     Assert.IsNotNull(request);
     HttpWebResponse response = null;
     try
     {
         response = (HttpWebResponse)request.GetResponse();
     }
     catch (WebException ex)
     {
         response = (HttpWebResponse)ex.Response;
     }
     Assert.IsNotNull(response);
     return response;
 }
 public static void SetRequest(HttpWebRequest request, BlobContext context, byte[] content)
 {
     Assert.IsNotNull(request);
     if (context.IsAsync)
     {
         BlobTestUtils.SetRequestAsync(request, context, content);
     }
     else
     {
         BlobTestUtils.SetRequestSync(request, context, content);
     }
 }
 static void SetRequestSync(HttpWebRequest request, BlobContext context, byte[] content)
 {
     Stream stream = request.GetRequestStream();
     Assert.IsNotNull(stream);
     stream.Write(content, 0, content.Length);
     stream.Close();
 }
 public BlobClientTests(bool owner, bool isAsync, int timeout)
 {
     BlobContext = new BlobContext(owner, isAsync, timeout);
 }
 public static HttpWebRequest PutBlockListRequest(BlobContext context, string containerName, string blobName, BlobProperties blobProperties, AccessCondition accessCondition)
 {
     Uri uri = BlobTests.ConstructPutUri(context.Address, containerName, blobName);
     HttpWebRequest request = null;
     OperationContext opContext = new OperationContext();
     request = BlobHttpWebRequestFactory.PutBlockList(uri, context.Timeout, blobProperties, accessCondition, opContext);
     Assert.IsNotNull(request);
     Assert.IsNotNull(request.Method);
     Assert.AreEqual("PUT", request.Method);
     BlobTestUtils.VersionHeader(request, false);
     BlobTestUtils.ContentLanguageHeader(request, null);
     BlobTestUtils.ContentMd5Header(request, null);
     return request;
 }
        /// <summary>
        /// Checks a get blob range response for consistency with the given parameters, and closes the response.
        /// </summary>
        /// <param name="response">The HTTP web response to check.</param>
        /// <param name="context">The testing context.</param>
        /// <param name="content">The expected content returned in the response.</param>
        /// <param name="expectedStartRange">The expected start range returned in the response header.</param>
        /// <param name="expectedEndRange">The expected end range returned in the response header.</param>
        /// <param name="expectedTotalBytes">The expected total number of bytes in the blob.</param>
        /// <param name="expectedError">The expected error code, or null if the operation is expected to succeed.</param>
        public static void CheckBlobRangeResponse(
            HttpWebResponse response,
            BlobContext context,
            byte[] content,
            long expectedStartRange,
            long expectedEndRange,
            long expectedTotalBytes,
            HttpStatusCode? expectedError)
        {
            Assert.IsNotNull(response);
            if (expectedError == null)
            {
                Assert.AreEqual(HttpStatusCode.PartialContent, response.StatusCode);
                Assert.IsNotNull(content);
                BlobTestUtils.LastModifiedHeader(response);
                BlobTestUtils.ContentLengthHeader(response, content.Length);
                BlobTestUtils.ETagHeader(response);
                BlobTestUtils.RequestIdHeader(response);
                BlobTestUtils.Contents(response, content);
                BlobTestUtils.ContentRangeHeader(response, expectedStartRange, expectedEndRange, expectedTotalBytes);
            }
            else
            {
                Assert.AreEqual(expectedError, response.StatusCode, response.StatusDescription);
            }

            response.Close();
        }
 public static void GetBlobResponse(HttpWebResponse response, BlobContext context, BlobProperties properties, byte[] content, HttpStatusCode? expectedError)
 {
     Assert.IsNotNull(response);
     if (expectedError == null)
     {
         Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
         BlobTestUtils.LastModifiedHeader(response);
         BlobTestUtils.ContentLengthHeader(response, content.Length);
         BlobTestUtils.ETagHeader(response);
         BlobTestUtils.RequestIdHeader(response);
         BlobTestUtils.Contents(response, content);
     }
     else
     {
         Assert.AreEqual(expectedError, response.StatusCode, response.StatusDescription);
     }
     response.Close();
 }
        public static void AcquireAndReleaseLeaseTest(BlobContext context, string containerName, string blobName)
        {
            CloudStorageAccount account = new CloudStorageAccount(new StorageCredentials(context.Account, context.Key), false);
            CloudBlobClient client = new CloudBlobClient(new Uri(context.Address), account.Credentials);
            CloudBlobContainer container = client.GetContainerReference(containerName);
            CloudBlockBlob blob = container.GetBlockBlobReference(blobName);

            BlobTestBase.UploadText(blob, "Text sent to cloud", Encoding.UTF8);

            // acquire a release on the blob and check LeaseStatus to be "locked"
            OperationContext opContext = new OperationContext();
            HttpWebRequest blobRequest = BlobHttpWebRequestFactory.Lease(
                blob.Uri,
                context.Timeout,
                LeaseAction.Acquire,
                null /* proposed lease ID */,
                60 /* lease duration */,
                null /* break period */,
                null /* access condition */,
                opContext);
            BlobTests.SignRequest(blobRequest, context);
            string leaseId = null;
            using (HttpWebResponse response = (HttpWebResponse)blobRequest.GetResponse())
            {
                leaseId = response.Headers["x-ms-lease-id"];
                Assert.AreEqual<HttpStatusCode>(response.StatusCode, HttpStatusCode.Created);
            }

            blob.FetchAttributes();
            Assert.AreEqual<LeaseStatus>(blob.Properties.LeaseStatus, LeaseStatus.Locked);

            // release the release on the blob and check LeaseStatus to be "unlocked"
            opContext = new OperationContext();
            blobRequest = BlobHttpWebRequestFactory.Lease(
                blob.Uri,
                context.Timeout,
                LeaseAction.Release,
                null /* proposed lease ID */,
                null /* lease duration */,
                null /* break period */,
                AccessCondition.GenerateLeaseCondition(leaseId),
                opContext);
            BlobTests.SignRequest(blobRequest, context);
            using (HttpWebResponse response = (HttpWebResponse)blobRequest.GetResponse())
            {
                Assert.AreEqual<HttpStatusCode>(response.StatusCode, HttpStatusCode.OK);
            }

            blob.FetchAttributes();
            Assert.AreEqual<LeaseStatus>(blob.Properties.LeaseStatus, LeaseStatus.Unlocked);

            blob.Delete();
        }
 public static HttpWebRequest CreateContainerRequest(BlobContext context, string containerName)
 {
     Uri uri = BlobClientTests.ConstructUri(context.Address, containerName);
     OperationContext opContext = new OperationContext();
     HttpWebRequest request = ContainerHttpWebRequestFactory.Create(uri, context.Timeout, opContext);
     Assert.IsNotNull(request);
     Assert.IsNotNull(request.Method);
     Assert.AreEqual("PUT", request.Method);
     BlobTestUtils.RangeHeader(request, null);
     BlobTestUtils.LeaseIdHeader(request, null);
     return request;
 }
        public static HttpWebRequest GetBlobRequest(BlobContext context, string containerName, string blobName, AccessCondition accessCondition)
        {
            bool valid = BlobTests.ContainerNameValidator(containerName) &&
                BlobTests.BlobNameValidator(blobName) &&
                BlobTests.LeaseIdValidator(accessCondition);

            Uri uri = BlobTests.ConstructGetUri(context.Address, containerName, blobName);
            HttpWebRequest request = null;
            OperationContext opContext = new OperationContext();
            try
            {
                request = BlobHttpWebRequestFactory.Get(uri, context.Timeout, null /* snapshot */, accessCondition, opContext);
            }
            catch (InvalidOperationException)
            {
                if (valid)
                {
                    Assert.Fail();
                }
            }
            if (valid)
            {
                Assert.IsNotNull(request);
                Assert.IsNotNull(request.Method);
                Assert.AreEqual("GET", request.Method);
                BlobTestUtils.RangeHeader(request, null);
                BlobTestUtils.LeaseIdHeader(request, accessCondition == null ? null : accessCondition.LeaseId);
            }
            return request;
        }
 static void SetRequestAsync(HttpWebRequest request, BlobContext context, byte[] content)
 {
     request.BeginGetRequestStream(new AsyncCallback(BlobTestUtils.ReadCallback), request);
     setRequestAsyncSem.WaitOne();
     Assert.IsNotNull(setRequestAsyncStream);
     setRequestAsyncStream.Write(content, 0, content.Length);
     setRequestAsyncStream.Close();
 }
Exemple #47
0
 public BlobClientTests(bool owner, bool isAsync, int timeout)
 {
     BlobContext = new BlobContext(owner, isAsync, timeout);
 }
 /// <summary>
 /// Generates and validates a request to change a lease.
 /// </summary>
 /// <param name="context">The blob context.</param>
 /// <param name="containerName">The container name.</param>
 /// <param name="blobName">The blob name.</param>
 /// <param name="leaseID">The lease ID.</param>
 /// <param name="proposedLeaseId">The proposed lease ID.</param>
 /// <returns>A web request for the operation.</returns>
 public static HttpWebRequest ChangeLeaseRequest(BlobContext context, string containerName, string blobName, string proposedLeaseId, AccessCondition accessCondition)
 {
     HttpWebRequest request;
     OperationContext opContext = new OperationContext();
     if (blobName != null)
     {
         // blob lease
         Uri uri = BlobTests.ConstructPutUri(context.Address, containerName, blobName);
         request = BlobHttpWebRequestFactory.Lease(
             uri,
             context.Timeout,
             LeaseAction.Change,
             proposedLeaseId,
             null /* lease duration */,
             null /* break period */,
             accessCondition,
             opContext);
     }
     else
     {
         // container lease
         Uri uri = BlobClientTests.ConstructUri(context.Address, containerName);
         request = ContainerHttpWebRequestFactory.Lease(
             uri,
             context.Timeout,
             LeaseAction.Change,
             proposedLeaseId,
             null /* lease duration */,
             null /* break period */,
             accessCondition,
             opContext);
     }
     Assert.IsNotNull(request);
     Assert.AreEqual("PUT", request.Method);
     BlobTestUtils.VersionHeader(request, false);
     BlobTestUtils.LeaseIdHeader(request, accessCondition == null ? null : accessCondition.LeaseId);
     BlobTestUtils.LeaseActionHeader(request, "change");
     BlobTestUtils.LeaseDurationHeader(request, null);
     BlobTestUtils.ProposedLeaseIdHeader(request, proposedLeaseId);
     BlobTestUtils.BreakPeriodHeader(request, null);
     return request;
 }
        public void CopyFromToRestoreSnapshot(BlobContext context, string containerName, string blobName)
        {
            string oldText = "Old stuff";
            string newText = "New stuff";

            StorageCredentials accountAndKey = new StorageCredentials(context.Account, context.Key);
            CloudStorageAccount account = new CloudStorageAccount(accountAndKey, false);
            CloudBlobClient blobClient = new CloudBlobClient(new Uri(context.Address), account.Credentials);
            CloudBlobContainer container = blobClient.GetContainerReference(containerName);
            CloudBlockBlob blob = container.GetBlockBlobReference(blobName);
            BlobTestBase.UploadText(blob, oldText, Encoding.UTF8);
            CloudBlockBlob snapshot = blob.CreateSnapshot();
            Assert.IsNotNull(snapshot.SnapshotTime);
            BlobTestBase.UploadText(blob, newText, Encoding.UTF8);

            Uri sourceUri = new Uri(snapshot.Uri.AbsoluteUri + "?snapshot=" + Request.ConvertDateTimeToSnapshotString(snapshot.SnapshotTime.Value));
            OperationContext opContext = new OperationContext();
            HttpWebRequest request = BlobHttpWebRequestFactory.CopyFrom(blob.Uri, 30, sourceUri, null, null, opContext);
            Assert.IsTrue(request != null, "Failed to create HttpWebRequest");
            BlobTests.SignRequest(request, context);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Assert.AreEqual<HttpStatusCode>(response.StatusCode, HttpStatusCode.Accepted);

            string text = BlobTestBase.DownloadText(blob, Encoding.UTF8);
            Assert.AreEqual<string>(text, oldText);

            blob.Delete(DeleteSnapshotsOption.IncludeSnapshots, null, null);
        }
 /// <summary>
 /// Generates and validates a request to break a lease.
 /// </summary>
 /// <param name="context">The blob context.</param>
 /// <param name="containerName">The container name.</param>
 /// <param name="blobName">The blob name.</param>
 /// <param name="breakPeriod">The break period.</param>
 /// <returns>A web request for the operation.</returns>
 public static HttpWebRequest BreakLeaseRequest(BlobContext context, string containerName, string blobName, int? breakPeriod, AccessCondition accessCondition)
 {
     HttpWebRequest request;
     OperationContext opContext = new OperationContext();
     if (blobName != null)
     {
         // blob lease
         Uri uri = BlobTests.ConstructPutUri(context.Address, containerName, blobName);
         request = BlobHttpWebRequestFactory.Lease(
             uri,
             context.Timeout,
             LeaseAction.Break,
             null /* proposed lease ID */,
             null /* lease duration */,
             breakPeriod,
             accessCondition,
             opContext);
     }
     else
     {
         // container lease
         Uri uri = BlobClientTests.ConstructUri(context.Address, containerName);
         request = ContainerHttpWebRequestFactory.Lease(
             uri,
             context.Timeout,
             LeaseAction.Break,
             null /* proposed lease ID */,
             null /* lease duration */,
             breakPeriod,
             accessCondition,
             opContext);
     }
     Assert.IsNotNull(request);
     Assert.AreEqual("PUT", request.Method);
     BlobTestUtils.VersionHeader(request, false);
     BlobTestUtils.LeaseIdHeader(request, null);
     BlobTestUtils.LeaseActionHeader(request, "break");
     BlobTestUtils.LeaseDurationHeader(request, null);
     BlobTestUtils.ProposedLeaseIdHeader(request, null);
     BlobTestUtils.BreakPeriodHeader(request, breakPeriod.HasValue ? breakPeriod.Value.ToString() : null);
     return request;
 }