Skip to content

Commit

Permalink
Merge pull request #1010 from cocytus/fix_netstd21_write_main
Browse files Browse the repository at this point in the history
Fixes issue #952 - did not write everything to output stream
  • Loading branch information
clairernovotny authored Nov 26, 2020
2 parents f99fa3a + f01a96b commit 4c2b17e
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 6 deletions.
8 changes: 8 additions & 0 deletions Refit.Tests/RefitStubs.Net46.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2542,6 +2542,14 @@ Task IRequestBin.PostGeneric<T>(T param)
var func = requestBuilder.BuildRestResultFuncForMethod("PostGeneric", new Type[] { typeof(T) }, new Type[] { typeof(T) });
return (Task)func(Client, arguments);
}

/// <inheritdoc />
Task IRequestBin.PostBig(BigObject big)
{
var arguments = new object[] { big };
var func = requestBuilder.BuildRestResultFuncForMethod("PostBig", new Type[] { typeof(BigObject) });
return (Task)func(Client, arguments);
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions Refit.Tests/RefitStubs.Net5.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2542,6 +2542,14 @@ Task IRequestBin.PostGeneric<T>(T param)
var func = requestBuilder.BuildRestResultFuncForMethod("PostGeneric", new Type[] { typeof(T) }, new Type[] { typeof(T) });
return (Task)func(Client, arguments);
}

/// <inheritdoc />
Task IRequestBin.PostBig(BigObject big)
{
var arguments = new object[] { big };
var func = requestBuilder.BuildRestResultFuncForMethod("PostBig", new Type[] { typeof(BigObject) });
return (Task)func(Client, arguments);
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions Refit.Tests/RefitStubs.NetCore2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2542,6 +2542,14 @@ Task IRequestBin.PostGeneric<T>(T param)
var func = requestBuilder.BuildRestResultFuncForMethod("PostGeneric", new Type[] { typeof(T) }, new Type[] { typeof(T) });
return (Task)func(Client, arguments);
}

/// <inheritdoc />
Task IRequestBin.PostBig(BigObject big)
{
var arguments = new object[] { big };
var func = requestBuilder.BuildRestResultFuncForMethod("PostBig", new Type[] { typeof(BigObject) });
return (Task)func(Client, arguments);
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions Refit.Tests/RefitStubs.NetCore3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2542,6 +2542,14 @@ Task IRequestBin.PostGeneric<T>(T param)
var func = requestBuilder.BuildRestResultFuncForMethod("PostGeneric", new Type[] { typeof(T) }, new Type[] { typeof(T) });
return (Task)func(Client, arguments);
}

/// <inheritdoc />
Task IRequestBin.PostBig(BigObject big)
{
var arguments = new object[] { big };
var func = requestBuilder.BuildRestResultFuncForMethod("PostBig", new Type[] { typeof(BigObject) });
return (Task)func(Client, arguments);
}
}
}

Expand Down
45 changes: 45 additions & 0 deletions Refit.Tests/RestService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public class RootObject
}
#pragma warning restore IDE1006 // Naming Styles

public class BigObject
{
public byte[] BigData { get; set; }
}

[Headers("User-Agent: Refit Integration Tests")]
public interface INpmJs
{
Expand All @@ -50,7 +55,11 @@ public interface IRequestBin

[Post("/1h3a5jm1")]
Task PostGeneric<T>(T param);

[Post("/big")]
Task PostBig(BigObject big);
}

public interface IApiBindPathToObject
{
[Get("/foos/{request.someProperty}/bar/{request.someProperty2}")]
Expand Down Expand Up @@ -1312,6 +1321,42 @@ public async Task CanGetDataOutOfErrorResponses()
}
}

[Fact]
public async Task CanSerializeBigData()
{
var mockHttp = new MockHttpMessageHandler();

var settings = new RefitSettings
{
HttpMessageHandlerFactory = () => mockHttp,
ContentSerializer = new SystemTextJsonContentSerializer()
};

var bigObject = new BigObject
{
BigData = Enumerable.Range(0, 800000).Select(x => (byte)(x % 256)).ToArray()
};

mockHttp.Expect(HttpMethod.Post, "http://httpbin.org/big")
.With(m =>
{
async Task<bool> T()
{
using var s = await m.Content.ReadAsStreamAsync();
var it = await System.Text.Json.JsonSerializer.DeserializeAsync<BigObject>(s, new System.Text.Json.JsonSerializerOptions { PropertyNamingPolicy = System.Text.Json.JsonNamingPolicy.CamelCase });
return it.BigData.SequenceEqual(bigObject.BigData);
}

return T().Result;
})
.Respond(HttpStatusCode.OK);

var fixture = RestService.For<IRequestBin>("http://httpbin.org/", settings);

await fixture.PostBig(bigObject);

mockHttp.VerifyNoOutstandingExpectation();
}

[Fact]
public async Task ErrorsFromApiReturnErrorContent()
Expand Down
9 changes: 4 additions & 5 deletions Refit/Buffers/PooledBufferWriter.Stream.NETStandard21.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if NETSTANDARD2_1
#if NETSTANDARD2_1 || NET5_0

using System;
using System.IO;
Expand All @@ -14,18 +14,17 @@ internal sealed partial class PooledBufferWriter
private sealed partial class PooledMemoryStream : Stream
{
/// <inheritdoc/>
public override void CopyTo(Stream destination, int bufferSize)
public Task CopyToInternalAsync(Stream destination, CancellationToken cancellationToken)
{
if (pooledBuffer is null) ThrowObjectDisposedException();

var bytesAvailable = length - position;
var spanLength = Math.Min(bytesAvailable, bufferSize);

var source = pooledBuffer.AsSpan(position, spanLength);
var source = pooledBuffer.AsMemory(position, bytesAvailable);

position += source.Length;

destination.Write(source);
return destination.WriteAsync(source, cancellationToken).AsTask();
}

/// <inheritdoc/>
Expand Down
5 changes: 4 additions & 1 deletion Refit/Buffers/PooledBufferWriter.Stream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,12 @@ public override Task CopyToAsync(Stream destination, int bufferSize, Cancellatio

try
{
#if NETSTANDARD2_1 || NET5_0
return CopyToInternalAsync(destination, cancellationToken);
#else
CopyTo(destination, bufferSize);

return Task.CompletedTask;
#endif
}
catch (OperationCanceledException e)
{
Expand Down

0 comments on commit 4c2b17e

Please sign in to comment.