Пример #1
0
        /// <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(
            HttpResponseMessage 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.ReasonPhrase);
            }

            response.Dispose();
        }
Пример #2
0
        public void CopyFromToRestoreSnapshot(BlobContext context, string containerName, string blobName)
        {
            //TODO: Fix HTTPClient code cleanup
            //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();
            //HttpRequestMessage request = BlobHttpRequestMessageFactory.CopyFrom(blob.Uri, 30, sourceUri, false, null, null, null, opContext, null, null);
            //Assert.IsTrue(request != null, "Failed to create HttpRequestMessage");
            //BlobTests.SignRequest(request, context);
            //HttpResponseMessage response = BlobTestUtils.GetResponse(request, context);
            //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);
        }
Пример #3
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, ChecksumRequested.None, accessCondition, null, opContext, SharedKeyCanonicalizer.Instance, context.Credentials, null);
            }
            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);
        }
Пример #4
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();
        }
Пример #5
0
 public static void GetBlockListResponse(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);
     }
 }
Пример #6
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);
        }
Пример #7
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);
        }
Пример #8
0
        public static HttpRequestMessage 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);
            HttpRequestMessage request   = null;
            OperationContext   opContext = new OperationContext();

            try
            {
                request = BlobHttpRequestMessageFactory.Put(uri, context.Timeout, properties, blobType, pageBlobSize, null, accessCondition, null, opContext,
                                                            SharedKeyCanonicalizer.Instance, context.Credentials, null);
                if (fatal)
                {
                    Assert.Fail();
                }
            }
            catch (InvalidOperationException)
            {
                if (valid)
                {
                    Assert.Fail();
                }
            }
            if (valid)
            {
                Assert.IsNotNull(request);
                Assert.IsNotNull(request.Method);
                Assert.AreEqual(HttpMethod.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.ContentCrc64Header(request, null);
                BlobTestUtils.CacheControlHeader(request, null);
                BlobTestUtils.BlobTypeHeader(request, properties.BlobType);
                BlobTestUtils.BlobSizeHeader(request, (properties.BlobType == BlobType.PageBlob) ? properties.Length : (long?)null);
            }
            return(request);
        }
Пример #9
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);
        }
Пример #10
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);
        }
Пример #11
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);
        }
Пример #12
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.ContentChecksumHeader(response);
         BlobTestUtils.RequestIdHeader(response);
         BlobTestUtils.ContentLengthHeader(response, 0);
     }
     else
     {
         Assert.AreEqual(expectedError, response.StatusCode, response.ReasonPhrase);
     }
     response.Dispose();
 }
Пример #13
0
 public static void GetBlobResponse(HttpResponseMessage 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.ReasonPhrase);
     }
     response.Dispose();
 }
Пример #14
0
 public BlobClientTests(bool owner, bool isAsync, int timeout)
 {
     BlobContext = new BlobContext(owner, isAsync, timeout);
 }
Пример #15
0
        public static async Task <HttpResponseMessage> GetResponse(HttpRequestMessage request, BlobContext context, CancellationTokenSource token = null)
        {
            Assert.IsNotNull(request);
            HttpResponseMessage response = null;

            try
            {
                HttpClient 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);
        }