Skip to content
This repository was archived by the owner on Jan 24, 2021. It is now read-only.

Commit 700b66f

Browse files
author
Adam Hathcock
committed
Only use underlying stream when buffered. Otherwise, it's not seekable.
1 parent 53b5f3f commit 700b66f

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/Nancy.Tests/Unit/IO/RequestStreamFixture.cs

+15
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,21 @@ public void Should_return_false_when_queried_about_supporting_seeking()
150150
result.ShouldBeFalse();
151151
}
152152

153+
[Fact]
154+
public void Should_return_true_when_queried_about_supporting_seeking_if_buffered()
155+
{
156+
// Given
157+
var stream = new ConfigurableMemoryStream();
158+
var request = RequestStream.FromStream(stream, 0, 1, false);
159+
request.BufferStream();
160+
161+
// When
162+
var result = request.CanSeek;
163+
164+
// Then
165+
result.ShouldBeTrue();
166+
}
167+
153168
[Fact]
154169
public void Should_return_underlaying_stream_when_queried_about_supporting_timeout()
155170
{

src/Nancy/IO/RequestStream.cs

+16-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,14 @@ public override bool CanRead
126126
/// <returns>Returns depending on whether the stream is buffered or not.</returns>
127127
public override bool CanSeek
128128
{
129-
get { return this.stream.CanSeek; }
129+
get
130+
{
131+
if (!isBuffered)
132+
{
133+
return false;
134+
}
135+
return this.stream.CanSeek;
136+
}
130137
}
131138

132139
/// <summary>
@@ -312,6 +319,10 @@ public override int ReadByte()
312319
/// <param name="origin">A value of type <see cref="T:System.IO.SeekOrigin"/> indicating the reference point used to obtain the new position. </param>
313320
public override long Seek(long offset, SeekOrigin origin)
314321
{
322+
if (!isBuffered)
323+
{
324+
throw new NotSupportedException();
325+
}
315326
return this.stream.Seek(offset, origin);
316327
}
317328

@@ -323,6 +334,10 @@ public override long Seek(long offset, SeekOrigin origin)
323334
/// <remarks>This functionality is not supported by the <see cref="RequestStream"/> type and will always throw <see cref="NotSupportedException"/>.</remarks>
324335
public override void SetLength(long value)
325336
{
337+
if (!isBuffered)
338+
{
339+
throw new NotSupportedException();
340+
}
326341
this.stream.SetLength(value);
327342
}
328343

0 commit comments

Comments
 (0)