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

Commit e536031

Browse files
author
Adam Hathcock
committed
Only use underlying stream when buffered. Otherwise, it's not seekable.
1 parent d67d8b1 commit e536031

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

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

+15-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ namespace Nancy.Tests.Unit.IO
55
using FakeItEasy;
66
using Nancy.IO;
77
using Xunit;
8-
using Xunit.Extensions;
98

109
public class RequestStreamFixture
1110
{
@@ -148,6 +147,21 @@ public void Should_return_false_when_queried_about_supporting_seeking()
148147
result.ShouldBeFalse();
149148
}
150149

150+
[Fact]
151+
public void Should_return_true_when_queried_about_supporting_seeking_if_buffered()
152+
{
153+
// Given
154+
var stream = new ConfigurableMemoryStream();
155+
var request = RequestStream.FromStream(stream, 0, 1, false);
156+
request.BufferStream();
157+
158+
// When
159+
var result = request.CanSeek;
160+
161+
// Then
162+
result.ShouldBeTrue();
163+
}
164+
151165
[Fact]
152166
public void Should_return_underlaying_stream_when_queried_about_supporting_timeout()
153167
{

src/Nancy/IO/RequestStream.cs

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

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

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

0 commit comments

Comments
 (0)