/// <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 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 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); }
public async Task ClearPageRangeScenarioTest(string containerName, string blobName, HttpStatusCode?expectedError) { // 1. Create Sparse Page Blob int blobSize = 128 * 1024; BlobProperties properties = new BlobProperties() { BlobType = BlobType.PageBlob }; Uri uri = BlobTests.ConstructPutUri(BlobContext.Address, containerName, blobName); OperationContext opContext = new OperationContext(); HttpRequestMessage webRequest = BlobHttpRequestMessageFactory.Put(uri, BlobContext.Timeout, properties, BlobType.PageBlob, blobSize, null, null, null, opContext, null, null); using (HttpResponseMessage response = await BlobTestUtils.GetResponse(webRequest, BlobContext)) { BlobTests.PutBlobResponse(response, BlobContext, expectedError); } // 2. Now upload some page ranges for (int m = 0; m * 512 * 4 < blobSize; m++) { int startOffset = 512 * 4 * m; int length = 512; PageRange range = new PageRange(startOffset, startOffset + length - 1); opContext = new OperationContext(); HttpRequestMessage pageRequest = BlobHttpRequestMessageFactory.PutPage(uri, BlobContext.Timeout, range, PageWrite.Update, null, null, opContext, null, null); HttpRequestHandler.SetContentLength(pageRequest, 512); byte[] content = new byte[length]; random.NextBytes(content); pageRequest.Content = new ByteArrayContent(content); using (HttpResponseMessage pageResponse = await BlobTestUtils.GetResponse(pageRequest, BlobContext)) { } } // 3. Now do a Get Page Ranges List <PageRange> pageRanges = new List <PageRange>(); opContext = new OperationContext(); HttpRequestMessage pageRangeRequest = BlobHttpRequestMessageFactory.GetPageRanges(uri, BlobContext.Timeout, null, null, null, null, null, opContext, null, null); using (HttpResponseMessage pageRangeResponse = await BlobTestUtils.GetResponse(pageRangeRequest, BlobContext)) { IEnumerable <PageRange> ranges = await GetPageRangesResponse.ParseAsync(HttpResponseParsers.GetResponseStream(pageRangeResponse), CancellationToken.None); pageRanges.AddRange(ranges); } // 4. Now Clear some pages bool skipFlag = false; foreach (PageRange pRange in pageRanges) { skipFlag = !skipFlag; if (skipFlag) { continue; } opContext = new OperationContext(); HttpRequestMessage clearPageRequest = BlobHttpRequestMessageFactory.PutPage(uri, BlobContext.Timeout, pRange, PageWrite.Clear, null, null, opContext, null, null); HttpRequestHandler.SetContentLength(clearPageRequest, 0); using (HttpResponseMessage clearResponse = await BlobTestUtils.GetResponse(clearPageRequest, BlobContext)) { } } // 5. Get New Page ranges and verify List <PageRange> newPageRanges = new List <PageRange>(); opContext = new OperationContext(); HttpRequestMessage newPageRangeRequest = BlobHttpRequestMessageFactory.GetPageRanges(uri, BlobContext.Timeout, null, null, null, null, null, opContext, null, null); using (HttpResponseMessage newPageRangeResponse = await BlobTestUtils.GetResponse(newPageRangeRequest, BlobContext)) { IEnumerable <PageRange> ranges = await GetPageRangesResponse.ParseAsync(HttpResponseParsers.GetResponseStream(newPageRangeResponse), CancellationToken.None); newPageRanges.AddRange(ranges); } Assert.AreEqual(pageRanges.Count(), newPageRanges.Count() * 2); for (int l = 0; l < newPageRanges.Count(); l++) { Assert.AreEqual(pageRanges[2 * l].StartOffset, newPageRanges[l].StartOffset); Assert.AreEqual(pageRanges[2 * l].EndOffset, newPageRanges[l].EndOffset); } }