public void Should_throw_parameters_greater_than_source_length()
		{
			var ms = new MemoryStream(new byte[] { 1, 2, 3, 4 });
			ArgumentOutOfRangeException exceptionFrom = null;
			ArgumentOutOfRangeException exceptionTo = null;

			try
			{
				var tested = new NarrowedStream(ms, 10, 20);
			}
			catch (ArgumentOutOfRangeException ex)
			{
				exceptionFrom = ex;
			}

			try
			{
				var tested = new NarrowedStream(ms, 0, 20);
			}
			catch (ArgumentOutOfRangeException ex)
			{
				exceptionTo = ex;
			}
			
			Assert.NotNull(exceptionFrom);
			Assert.NotNull(exceptionTo);
			Assert.Contains("from", exceptionFrom.Message);
			Assert.Contains("to", exceptionTo.Message);
		}
        public void Check_reading_from_some_offset()
        {
            var ms = PrepareSourceStream();
            var tested = new NarrowedStream(ms, 1, 6);

            var reader = new StreamReader(tested);
            var result = reader.ReadToEnd();
            Assert.Equal("000010", result);
        }
        public void Check_reading_from_begining()
        {
            var ms = PrepareSourceStream();
            var tested = new NarrowedStream(ms, 0, 5);

            var reader = new StreamReader(tested);
            var result = reader.ReadToEnd();
            Assert.Equal("000001", result);
        }
        public void Check_copy_async()
        {
            var ms = PrepareSourceStream();
            var tested = new NarrowedStream(ms, 0, ms.Length - 1);

            var reader = new StreamReader(tested);
            var result = new MemoryStream();
            tested.CopyToAsync(result).Wait();
            Assert.Equal(500000*6, result.Length);
        }
        public void Check_reading_all()
        {
            var ms = PrepareSourceStream();
            var tested = new NarrowedStream(ms, 0, ms.Length - 1);

            var reader = new StreamReader(tested);
            var result = reader.ReadToEnd();
            Assert.Equal(500000 * 6, result.Length);
            Assert.Equal(500000 * 6, tested.Length);
        }
		public SourceFilePart(NarrowedStream sourceChunk)
			: base(sourceChunk)
		{
			this.sourceChunk = sourceChunk;

			Headers.ContentDisposition = new ContentDispositionHeaderValue("file");
			Headers.ContentDisposition.Parameters.Add(new NameValueHeaderValue(SyncingMultipartConstants.NeedType,
			                                                                   SyncingNeedType));
			Headers.ContentDisposition.Parameters.Add(new NameValueHeaderValue(SyncingMultipartConstants.RangeFrom,
			                                                                   sourceChunk.From.ToString(
				                                                                   CultureInfo.InvariantCulture)));
			Headers.ContentDisposition.Parameters.Add(new NameValueHeaderValue(SyncingMultipartConstants.RangeTo,
			                                                                   sourceChunk.To.ToString(
				                                                                   CultureInfo.InvariantCulture)));

			Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
		}
		public override Stream GetStream(HttpContent parent, HttpContentHeaders headers)
		{
			if (parent == null)
				throw new ArgumentNullException("parent");

			if (headers == null)
				throw new ArgumentNullException("headers");

			var parameters = headers.ContentDisposition.Parameters.ToDictionary(t => t.Name);

			var needType = parameters[SyncingMultipartConstants.NeedType].Value;
			var from = Convert.ToInt64(parameters[SyncingMultipartConstants.RangeFrom].Value);
			var to = Convert.ToInt64(parameters[SyncingMultipartConstants.RangeTo].Value);
			var length = (to - from + 1);

			NumberOfFileParts++;

			if (needType == "source")
			{
				BytesTransfered += length;

				return synchronizingFile;
			}

			if (needType == "seed")
			{
				var limitedStream = new NarrowedStream(localFile, from, to);
				limitedStream.CopyTo(synchronizingFile, StorageConstants.MaxPageSize);

				BytesCopied += length;

				return Stream.Null; // we can return Stream.Null because 'seed' part is always empty
			}

			throw new ArgumentException(string.Format("Invalid need type: '{0}'", needType));
		}