public async Task <object> HandleResponseAsync(HttpResponseMessage response) { if (!response.IsSuccessStatusCode) { throw new HttpResponseException(response); } blobSizeListener(response.Content.Headers.ContentLength ?? 0); using (Stream outputStream = new NotifyingOutputStream(destinationOutputStream, writtenByteCountListener, true)) { BlobDescriptor receivedBlobDescriptor; using (Stream contentStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false)) { receivedBlobDescriptor = await Digests.ComputeDigestAsync(contentStream, outputStream).ConfigureAwait(false); } if (!blobDigest.Equals(receivedBlobDescriptor.GetDigest())) { throw new UnexpectedBlobDigestException( "The pulled BLOB has digest '" + receivedBlobDescriptor.GetDigest() + "', but the request digest was '" + blobDigest + "'"); } } return(null); }
public void TestCallback_correctSequence() { MemoryStream byteArrayOutputStream = new MemoryStream(); List <long> byteCounts = new List <long>(); using (NotifyingOutputStream notifyingOutputStream = new NotifyingOutputStream(byteArrayOutputStream, byteCounts.Add)) { byte[] firstBytes = new byte[] { 0 }; notifyingOutputStream.Write(firstBytes); byte[] secondBites = new byte[] { 1, 2, 3 }; notifyingOutputStream.Write(secondBites, 0, secondBites.Length); notifyingOutputStream.Write(new byte[] { 1, 2, 3, 4, 5 }, 3, 2); } Assert.AreEqual(new[] { 1L, 3L, 2L }, byteCounts); CollectionAssert.AreEqual(new byte[] { 0, 1, 2, 3, 4, 5 }, byteArrayOutputStream.ToArray()); }
public void TestDelay() { MemoryStream byteArrayOutputStream = new MemoryStream(); IList <long> byteCounts = new List <long>(); Queue <Instant> instantQueue = new Queue <Instant>(); instantQueue.Enqueue(Instant.FromUnixTimeSeconds(0)); using (ThrottledAccumulatingConsumer byteCounter = new ThrottledAccumulatingConsumer( byteCounts.Add, Duration.FromSeconds(3), instantQueue.Dequeue)) using (NotifyingOutputStream notifyingOutputStream = new NotifyingOutputStream(byteArrayOutputStream, byteCounter.Accept)) { instantQueue.Enqueue(Instant.FromUnixTimeSeconds(0)); notifyingOutputStream.WriteByte(100); instantQueue.Enqueue(Instant.FromUnixTimeSeconds(0)); notifyingOutputStream.Write(new byte[] { 101, 102, 103 }); instantQueue.Enqueue(Instant.FromUnixTimeSeconds(0) + Duration.FromSeconds(4)); notifyingOutputStream.Write(new byte[] { 104, 105, 106 }); instantQueue.Enqueue(Instant.FromUnixTimeSeconds(0) + Duration.FromSeconds(10)); notifyingOutputStream.Write(new byte[] { 107, 108 }); instantQueue.Enqueue(Instant.FromUnixTimeSeconds(0) + Duration.FromSeconds(10)); notifyingOutputStream.Write(new byte[] { 109 }); instantQueue.Enqueue(Instant.FromUnixTimeSeconds(0) + Duration.FromSeconds(13)); notifyingOutputStream.Write(new byte[] { 0, 110 }, 1, 1); } Assert.AreEqual(new[] { 7L, 2L, 2L }, byteCounts); CollectionAssert.AreEqual( new byte[] { 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110 }, byteArrayOutputStream.ToArray()); }