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

Commit

Permalink
Only use underlying stream when buffered. Otherwise, it's not seekable.
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Hathcock committed May 27, 2015
1 parent d67d8b1 commit e536031
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/Nancy.Tests/Unit/IO/RequestStreamFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ namespace Nancy.Tests.Unit.IO
using FakeItEasy;
using Nancy.IO;
using Xunit;
using Xunit.Extensions;

public class RequestStreamFixture
{
Expand Down Expand Up @@ -148,6 +147,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()
{
Expand Down
17 changes: 16 additions & 1 deletion src/Nancy/IO/RequestStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,14 @@ public override bool CanRead
/// <returns>Returns depending on whether the stream is buffered or not.</returns>
public override bool CanSeek
{
get { return this.stream.CanSeek; }
get
{
if (!isBuffered)
{
return false;
}
return this.stream.CanSeek;
}
}

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

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

Expand Down

0 comments on commit e536031

Please sign in to comment.