From 700b66ffb0de10c3ec0c62c13cc0882cfdac2f10 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Wed, 27 May 2015 15:37:54 +0100 Subject: [PATCH] Only use underlying stream when buffered. Otherwise, it's not seekable. --- src/Nancy.Tests/Unit/IO/RequestStreamFixture.cs | 15 +++++++++++++++ src/Nancy/IO/RequestStream.cs | 17 ++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/Nancy.Tests/Unit/IO/RequestStreamFixture.cs b/src/Nancy.Tests/Unit/IO/RequestStreamFixture.cs index 1609121374..ea8662fdba 100644 --- a/src/Nancy.Tests/Unit/IO/RequestStreamFixture.cs +++ b/src/Nancy.Tests/Unit/IO/RequestStreamFixture.cs @@ -150,6 +150,21 @@ public void Should_return_false_when_queried_about_supporting_seeking() result.ShouldBeFalse(); } + [Fact] + public void Should_return_true_when_queried_about_supporting_seeking_if_buffered() + { + // Given + var stream = new ConfigurableMemoryStream(); + var request = RequestStream.FromStream(stream, 0, 1, false); + request.BufferStream(); + + // When + var result = request.CanSeek; + + // Then + result.ShouldBeTrue(); + } + [Fact] public void Should_return_underlaying_stream_when_queried_about_supporting_timeout() { diff --git a/src/Nancy/IO/RequestStream.cs b/src/Nancy/IO/RequestStream.cs index 607c0be8e7..9822a22983 100644 --- a/src/Nancy/IO/RequestStream.cs +++ b/src/Nancy/IO/RequestStream.cs @@ -126,7 +126,14 @@ public override bool CanRead /// Returns depending on whether the stream is buffered or not. public override bool CanSeek { - get { return this.stream.CanSeek; } + get + { + if (!isBuffered) + { + return false; + } + return this.stream.CanSeek; + } } /// @@ -312,6 +319,10 @@ public override int ReadByte() /// A value of type indicating the reference point used to obtain the new position. public override long Seek(long offset, SeekOrigin origin) { + if (!isBuffered) + { + throw new NotSupportedException(); + } return this.stream.Seek(offset, origin); } @@ -323,6 +334,10 @@ public override long Seek(long offset, SeekOrigin origin) /// This functionality is not supported by the type and will always throw . public override void SetLength(long value) { + if (!isBuffered) + { + throw new NotSupportedException(); + } this.stream.SetLength(value); }