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

Commit

Permalink
Merge pull request #2778 from thefringeninja/request-stream-regression
Browse files Browse the repository at this point in the history
Regression: System.NotSupportedException: The stream does not support writing.
  • Loading branch information
thecodejunkie committed Sep 1, 2017
2 parents b2671a1 + 5efd811 commit fac1675
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Nancy/IO/RequestStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ private void MoveStreamContentsToFileStream()
this.stream.Position = 0;
}
this.stream.CopyTo(targetStream, 8196);
if (this.stream.CanSeek)
if (this.stream.CanWrite)
{
this.stream.Flush();
}
Expand Down
93 changes: 93 additions & 0 deletions test/Nancy.Tests/Unit/RequestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace Nancy.Tests.Unit
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using FakeItEasy;
using Nancy.Helpers;
using Nancy.IO;
Expand Down Expand Up @@ -814,6 +816,21 @@ public void Should_not_replace_equal_key_value_query_with_bool()
ShouldAssertExtensions.ShouldBeOfType<string>(request.Query["key1"].Value);
}

[Fact]
public void Should_not_flush_underlying_stream_if_it_is_not_writable()
{
var largeStream = new NonWriteableStream(
new MemoryStream(new byte[1025]));
// Given
var memory = new RequestStream(largeStream, 1025, 1024, false);

// When
var exception = Record.Exception(() => new Request("POST", new Url { Path = "/", Scheme = "http" }, memory));

// Then
exception.ShouldBeNull();
}

private static RequestStream CreateRequestStream()
{
return CreateRequestStream(new MemoryStream());
Expand Down Expand Up @@ -894,5 +911,81 @@ private static byte[] BuildMultipartFileValues(Dictionary<string, Tuple<string,

return bytes;
}

private class NonWriteableStream : Stream
{
private readonly MemoryStream inner;

public NonWriteableStream(MemoryStream inner)
{
this.inner = inner;
}

public override void Flush()
{
throw new NotSupportedException();
}

public override Task FlushAsync(CancellationToken cancellationToken)
{
throw new NotSupportedException();
}

public override int Read(byte[] buffer, int offset, int count)
{
return this.inner.Read(buffer, offset, count);
}

public override long Seek(long offset, SeekOrigin origin)
{
return this.inner.Seek(offset, origin);
}

public override void SetLength(long value)
{
throw new NotSupportedException();
}

public override void Write(byte[] buffer, int offset, int count)
{
throw new NotSupportedException();
}

public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
throw new NotSupportedException();
}

public override void WriteByte(byte value)
{
throw new NotSupportedException();
}

public override bool CanRead
{
get { return this.inner.CanRead; }
}

public override bool CanSeek
{
get { return this.inner.CanSeek; }
}

public override bool CanWrite
{
get { return false; }
}

public override long Length
{
get { return this.inner.Length; }
}

public override long Position
{
get { return this.inner.Position; }
set { this.inner.Position = value; }
}
}
}
}

0 comments on commit fac1675

Please sign in to comment.