public static HttpWebRequest WriteRangeRequest(FileContext context, string shareName, string fileName, FileRange range, int length, AccessCondition accessCondition) { bool valid = FileTests.ShareNameValidator(shareName) && FileTests.FileNameValidator(fileName); Uri uri = FileTests.ConstructPutUri(context.Address, shareName, fileName); HttpWebRequest request = null; OperationContext opContext = new OperationContext(); try { request = FileHttpWebRequestFactory.PutRange(uri, context.Timeout, range, FileRangeWrite.Update, accessCondition, true, opContext); request.ContentLength = length; } catch (InvalidOperationException) { if (valid) { Assert.Fail(); } } if (valid) { Assert.IsNotNull(request); Assert.IsNotNull(request.Method); Assert.AreEqual("PUT", request.Method); FileTestUtils.RangeHeader(request, range.StartOffset, range.EndOffset); } return(request); }
public static HttpRequestMessage WriteRangeRequest(FileContext context, string shareName, string fileName, FileRange range, int length, AccessCondition accessCondition) { bool valid = FileTests.ShareNameValidator(shareName) && FileTests.FileNameValidator(fileName); Uri uri = FileTests.ConstructPutUri(context.Address, shareName, fileName); HttpRequestMessage request = null; OperationContext opContext = new OperationContext(); try { using (var ms = new MemoryStream()) { request = FileHttpRequestMessageFactory.PutRange(uri, context.Timeout, range, FileRangeWrite.Update, accessCondition, new StreamContent(ms), opContext, SharedKeyCanonicalizer.Instance, context.Credentials); } //HttpRequestHandler.SetContentLength(request, length); } catch (InvalidOperationException) { if (valid) { Assert.Fail(); } } if (valid) { Assert.IsNotNull(request); Assert.IsNotNull(request.Method); Assert.AreEqual(HttpMethod.Put, request.Method); FileTestUtils.RangeHeader(request, range.StartOffset, range.EndOffset); } return(request); }
public static HttpWebRequest PutFileRequest(FileContext context, string shareName, string fileName, FileProperties properties, byte[] content, long fileSize, AccessCondition accessCondition) { bool valid = FileTests.ShareNameValidator(shareName) && FileTests.FileNameValidator(fileName) && FileTestUtils.ContentValidator(content); Uri uri = FileTests.ConstructPutUri(context.Address, shareName, fileName); HttpWebRequest request = null; OperationContext opContext = new OperationContext(); try { request = FileHttpWebRequestFactory.Create(uri, context.Timeout, properties, fileSize, accessCondition, true, opContext); } catch (InvalidOperationException) { if (valid) { Assert.Fail(); } } if (valid) { Assert.IsNotNull(request); Assert.IsNotNull(request.Method); Assert.AreEqual("PUT", request.Method); FileTestUtils.VersionHeader(request, false); FileTestUtils.ContentTypeHeader(request, null); FileTestUtils.ContentDispositionHeader(request, properties.ContentDisposition); FileTestUtils.ContentEncodingHeader(request, properties.ContentEncoding); FileTestUtils.ContentLanguageHeader(request, null); FileTestUtils.CacheControlHeader(request, null); FileTestUtils.FileTypeHeader(request, "File"); FileTestUtils.FileSizeHeader(request, properties.Length); } return(request); }
public void ClearRangeScenarioTest(string shareName, string fileName, HttpStatusCode?expectedError) { // 1. Create Sparse File int fileSize = 128 * 1024; FileProperties properties = new FileProperties(); Uri uri = FileTests.ConstructPutUri(FileContext.Address, shareName, fileName); OperationContext opContext = new OperationContext(); HttpWebRequest webRequest = FileHttpWebRequestFactory.Create(uri, FileContext.Timeout, properties, fileSize, null, true, opContext); FileTests.SignRequest(webRequest, FileContext); using (HttpWebResponse response = webRequest.GetResponse() as HttpWebResponse) { FileTests.PutFileResponse(response, FileContext, expectedError); } // 2. Now upload some ranges for (int m = 0; m * 512 * 4 < fileSize; m++) { int startOffset = 512 * 4 * m; int length = 512; FileRange range = new FileRange(startOffset, startOffset + length - 1); opContext = new OperationContext(); HttpWebRequest rangeRequest = FileHttpWebRequestFactory.PutRange(uri, FileContext.Timeout, range, FileRangeWrite.Update, null, true, opContext); rangeRequest.ContentLength = 512; FileTests.SignRequest(rangeRequest, FileContext); Stream outStream = rangeRequest.GetRequestStream(); for (int n = 0; n < 512; n++) { outStream.WriteByte((byte)m); } outStream.Close(); using (HttpWebResponse rangeResponse = rangeRequest.GetResponse() as HttpWebResponse) { } } // 3. Now do a List Ranges List <FileRange> fileRanges = new List <FileRange>(); opContext = new OperationContext(); HttpWebRequest listRangesRequest = FileHttpWebRequestFactory.ListRanges(uri, FileContext.Timeout, null, null, null, null, true, opContext); FileTests.SignRequest(listRangesRequest, FileContext); using (HttpWebResponse rangeResponse = listRangesRequest.GetResponse() as HttpWebResponse) { ListRangesResponse listRangesResponse = new ListRangesResponse(rangeResponse.GetResponseStream()); fileRanges.AddRange(listRangesResponse.Ranges.ToList()); } // 4. Now Clear some ranges bool skipFlag = false; foreach (FileRange pRange in fileRanges) { skipFlag = !skipFlag; if (skipFlag) { continue; } opContext = new OperationContext(); HttpWebRequest clearRangeRequest = FileHttpWebRequestFactory.PutRange(uri, FileContext.Timeout, pRange, FileRangeWrite.Clear, null, true, opContext); clearRangeRequest.ContentLength = 0; FileTests.SignRequest(clearRangeRequest, FileContext); using (HttpWebResponse clearResponse = clearRangeRequest.GetResponse() as HttpWebResponse) { } } // 5. Get New ranges and verify List <FileRange> newFileRanges = new List <FileRange>(); opContext = new OperationContext(); HttpWebRequest newFileRangeRequest = FileHttpWebRequestFactory.ListRanges(uri, FileContext.Timeout, null, null, null, null, true, opContext); FileTests.SignRequest(newFileRangeRequest, FileContext); using (HttpWebResponse newFileRangeResponse = newFileRangeRequest.GetResponse() as HttpWebResponse) { ListRangesResponse listNewRangesResponse = new ListRangesResponse(newFileRangeResponse.GetResponseStream()); newFileRanges.AddRange(listNewRangesResponse.Ranges.ToList()); } Assert.AreEqual(fileRanges.Count(), newFileRanges.Count() * 2); for (int l = 0; l < newFileRanges.Count(); l++) { Assert.AreEqual(fileRanges[2 * l].StartOffset, newFileRanges[l].StartOffset); Assert.AreEqual(fileRanges[2 * l].EndOffset, newFileRanges[l].EndOffset); } }
public async Task ClearRangeScenarioTest(string shareName, string fileName, HttpStatusCode?expectedError) { // 1. Create Sparse File int fileSize = 128 * 1024; FileProperties properties = new FileProperties(); Uri uri = FileTests.ConstructPutUri(FileContext.Address, shareName, fileName); OperationContext opContext = new OperationContext(); HttpRequestMessage webRequest = FileHttpRequestMessageFactory.Create(uri, FileContext.Timeout, properties, fileSize, null, null, opContext, null, null); using (HttpResponseMessage response = await FileTestUtils.GetResponse(webRequest, FileContext)) { FileTests.PutFileResponse(response, FileContext, expectedError); } // 2. Now upload some ranges for (int m = 0; m * 512 * 4 < fileSize; m++) { int startOffset = 512 * 4 * m; int length = 512; FileRange range = new FileRange(startOffset, startOffset + length - 1); opContext = new OperationContext(); HttpRequestMessage rangeRequest = FileHttpRequestMessageFactory.PutRange(uri, FileContext.Timeout, range, FileRangeWrite.Update, null, null, opContext, null, null); HttpRequestHandler.SetContentLength(rangeRequest, 512); byte[] content = new byte[length]; random.NextBytes(content); rangeRequest.Content = new ByteArrayContent(content); using (HttpResponseMessage rangeResponse = await FileTestUtils.GetResponse(rangeRequest, FileContext)) { } } // 3. Now do a List Ranges List <FileRange> fileRanges = new List <FileRange>(); opContext = new OperationContext(); HttpRequestMessage listRangesRequest = FileHttpRequestMessageFactory.ListRanges(uri, FileContext.Timeout, null, null, null, null, null, opContext, null, null); using (HttpResponseMessage rangeResponse = await FileTestUtils.GetResponse(listRangesRequest, FileContext)) { var ranges = await ListRangesResponse.ParseAsync(await rangeResponse.Content.ReadAsStreamAsync(), CancellationToken.None); fileRanges.AddRange(ranges); } // 4. Now Clear some ranges bool skipFlag = false; foreach (FileRange pRange in fileRanges) { skipFlag = !skipFlag; if (skipFlag) { continue; } opContext = new OperationContext(); HttpRequestMessage clearRangeRequest = FileHttpRequestMessageFactory.PutRange(uri, FileContext.Timeout, pRange, FileRangeWrite.Clear, null, null, opContext, null, null); HttpRequestHandler.SetContentLength(clearRangeRequest, 0); using (HttpResponseMessage clearResponse = await FileTestUtils.GetResponse(clearRangeRequest, FileContext)) { } } // 5. Get New ranges and verify List <FileRange> newFileRanges = new List <FileRange>(); opContext = new OperationContext(); HttpRequestMessage newFileRangeRequest = FileHttpRequestMessageFactory.ListRanges(uri, FileContext.Timeout, null, null, null, null, null, opContext, null, null); using (HttpResponseMessage newFileRangeResponse = await FileTestUtils.GetResponse(newFileRangeRequest, FileContext)) { var ranges = await ListRangesResponse.ParseAsync(await newFileRangeResponse.Content.ReadAsStreamAsync(), CancellationToken.None); newFileRanges.AddRange(ranges); } Assert.AreEqual(fileRanges.Count(), newFileRanges.Count() * 2); for (int l = 0; l < newFileRanges.Count(); l++) { Assert.AreEqual(fileRanges[2 * l].StartOffset, newFileRanges[l].StartOffset); Assert.AreEqual(fileRanges[2 * l].EndOffset, newFileRanges[l].EndOffset); } }