Exemplo n.º 1
0
        public async Task WithNonSeekableStream_UsingSystemTextJsonContentSerializer()
        {
            var model = new TestAliasObject
            {
                ShortNameForAlias        = nameof(WithNonSeekableStream_UsingSystemTextJsonContentSerializer),
                ShortNameForJsonProperty = nameof(TestAliasObject)
            };

            var localHandler = new MockHttpMessageHandler();

            var settings = new RefitSettings(new SystemTextJsonContentSerializer())
            {
                HttpMessageHandlerFactory = () => localHandler
            };

            using var utf8BufferWriter = new PooledBufferWriter();

            var utf8JsonWriter = new Utf8JsonWriter(utf8BufferWriter);

            System.Text.Json.JsonSerializer.Serialize(utf8JsonWriter, model);

            using var sourceStream = utf8BufferWriter.DetachStream();

            using var contentStream = new ThrowOnGetLengthMemoryStream { CanGetLength = true };

            sourceStream.CopyTo(contentStream);

            contentStream.Position = 0;

            contentStream.CanGetLength = false;

            var httpContent = new StreamContent(contentStream)
            {
                Headers =
                {
                    ContentType = new MediaTypeHeaderValue("application/json")
                    {
                        CharSet = Encoding.UTF8.WebName
                    }
                }
            };

            var expectedResponse = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = httpContent
            };

            expectedResponse.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            expectedResponse.StatusCode = HttpStatusCode.OK;

            localHandler.Expect(HttpMethod.Get, "http://api/aliasTest").Respond(req => expectedResponse);

            var localFixture = RestService.For <IMyAliasService>("http://api", settings);

            var result = await localFixture.GetTestObject();

            Assert.NotNull(result);
            Assert.Equal(nameof(WithNonSeekableStream_UsingSystemTextJsonContentSerializer), result.ShortNameForAlias);
            Assert.Equal(nameof(TestAliasObject), result.ShortNameForJsonProperty);
        }
Exemplo n.º 2
0
            public Memory <byte> GetMemory(int sizeHint)
            {
                if (sizeHint < 0)
                {
                    throw new ArgumentOutOfRangeException(nameof(sizeHint));
                }

                var result = buffer.Slice(position);

                if (!result.IsEmpty && result.Length >= sizeHint)
                {
                    goto exit;
                }

                if (extraBuffer is null)
                {
                    extraBuffer = new(null, sizeHint + position);
                    extraBuffer.Write(buffer.Span);
                }

                result = extraBuffer.GetMemory(sizeHint);

exit:
                return(result);
            }
Exemplo n.º 3
0
            protected override void Dispose(bool disposing)
            {
                if (disposing)
                {
                    extraBuffer?.Dispose();
                    extraBuffer = null;
                }

                base.Dispose(disposing);
            }
Exemplo n.º 4
0
        /// <summary>
        /// Converts asynchronous collection to the array.
        /// </summary>
        /// <param name="collection">The asynchronous collection.</param>
        /// <param name="initialCapacity">The initial capacity of internal buffer.</param>
        /// <param name="allocator">The memory allocator used by internal buffer.</param>
        /// <param name="token">The token that can be used to cancel the operation.</param>
        /// <typeparam name="T">Type of elements in the collection.</typeparam>
        /// <returns>The array representing all elements from the source collection.</returns>
        /// <exception cref="OperationCanceledException">The operation has been canceled.</exception>
        public static async Task <T[]> ToArrayAsync <T>(this IAsyncEnumerable <T> collection, int initialCapacity = 10, MemoryAllocator <T>?allocator = null, CancellationToken token = default)
        {
            using var buffer = new PooledBufferWriter <T>(allocator, initialCapacity);

            await foreach (var item in collection.WithCancellation(token))
            {
                buffer.Add(item);
            }

            return(buffer.WrittenMemory.ToArray());
        }
        /// <inheritdoc/>
        public Task <HttpContent> SerializeAsync <T>(T item)
        {
            using var utf8BufferWriter = new PooledBufferWriter();

            var utf8JsonWriter = new Utf8JsonWriter(utf8BufferWriter);

            JsonSerializer.Serialize(utf8JsonWriter, item, jsonSerializerOptions);

            var content = new StreamContent(utf8BufferWriter.DetachStream())
            {
                Headers =
                {
                    ContentType = new MediaTypeHeaderValue("application/json")
                    {
                        CharSet = Encoding.UTF8.WebName
                    }
                }
            };

            return(Task.FromResult <HttpContent>(content));
        }