public async Task WritesData_InExpectedEncoding(string data, string encodingName)
        {
            // Arrange
            var encoding      = Encoding.GetEncoding(encodingName);
            var expectedBytes = encoding.GetBytes(data);
            var stream        = new MemoryStream();
            var writer        = new HttpResponseStreamWriter(stream, encoding);

            // Act
            using (writer)
            {
                await writer.WriteAsync(data);
            }

            // Assert
            Assert.Equal(expectedBytes, stream.ToArray());
        }
        public async Task WriteLineReadOnlyMemoryAsync_TokenCanceled_ReturnsCanceledTask()
        {
            // Arrange
            var stream            = new TestMemoryStream();
            var writer            = new HttpResponseStreamWriter(stream, Encoding.UTF8);
            var memory            = new ReadOnlyMemory <char>(new char[] { 'a' });
            var cancellationToken = new CancellationToken(true);

            // Act
            using (writer)
            {
                await Assert.ThrowsAsync <TaskCanceledException>(async() => await writer.WriteLineAsync(memory, cancellationToken));
            }

            // Assert
            Assert.Equal(0, stream.Length);
        }
        public async Task WriteReadOnlyMemoryAsync_WritesToStream(int byteLength)
        {
            // Arrange
            var stream = new TestMemoryStream();
            var writer = new HttpResponseStreamWriter(stream, Encoding.UTF8);

            // Act
            using (writer)
            {
                var array  = new string('a', byteLength).ToCharArray();
                var memory = new ReadOnlyMemory <char>(array);
                await writer.WriteAsync(memory);
            }

            // Assert
            Assert.Equal(byteLength, stream.Length);
        }
        public void WriteReadOnlySpanChar_WritesToStream(int byteLength)
        {
            // Arrange
            var stream = new TestMemoryStream();
            var writer = new HttpResponseStreamWriter(stream, Encoding.UTF8);

            // Act
            using (writer)
            {
                var array = new string('a', byteLength).ToCharArray();
                var span  = new ReadOnlySpan <char>(array);
                writer.Write(span);
            }

            // Assert
            Assert.Equal(byteLength, stream.Length);
        }
        public async Task DoesNotWriteBOM()
        {
            // Arrange
            var memoryStream    = new MemoryStream();
            var encodingWithBOM = new UTF8Encoding(encoderShouldEmitUTF8Identifier: true);
            var writer          = new HttpResponseStreamWriter(memoryStream, encodingWithBOM);
            var expectedData    = new byte[] { 97, 98, 99, 100 }; // without BOM

            // Act
            using (writer)
            {
                await writer.WriteAsync("abcd");
            }

            // Assert
            Assert.Equal(expectedData, memoryStream.ToArray());
        }
        public async Task FlushesBuffer_ButNotStream_OnFlushAsync(int byteLength)
        {
            // Arrange
            var stream = new TestMemoryStream();
            var writer = new HttpResponseStreamWriter(stream, Encoding.UTF8);
            await writer.WriteAsync(new string('a', byteLength));

            var expectedWriteCount = Math.Ceiling((double)byteLength / HttpResponseStreamWriter.DefaultBufferSize);

            // Act
            await writer.FlushAsync();

            // Assert
            Assert.Equal(0, stream.FlushAsyncCallCount);
            Assert.Equal(expectedWriteCount, stream.WriteAsyncCallCount);
            Assert.Equal(byteLength, stream.Length);
        }
Exemplo n.º 7
0
        public async Task WriteCharAsync_WritesToStream(int byteLength)
        {
            // Arrange
            var stream = new TestMemoryStream();
            var writer = new HttpResponseStreamWriter(stream, Encoding.UTF8);

            // Act
            using (writer)
            {
                for (var i = 0; i < byteLength; i++)
                {
                    await writer.WriteAsync('a');
                }
            }

            // Assert
            Assert.Equal(byteLength, stream.Length);
        }
Exemplo n.º 8
0
        public async Task HttpResponseStreamWriter_WritesDataCorrectly_ForCharactersHavingSurrogatePairs(int characterSize)
        {
            // Arrange
            // Here "𐐀" (called Deseret Long I) actually represents 2 characters. Try to make this character split across
            // the boundary
            var content = new string('a', characterSize - 1) + "𐐀";
            var stream  = new TestMemoryStream();
            var writer  = new HttpResponseStreamWriter(stream, Encoding.Unicode);

            // Act
            await writer.WriteAsync(content);

            await writer.FlushAsync();

            // Assert
            stream.Seek(0, SeekOrigin.Begin);
            var streamReader  = new StreamReader(stream, Encoding.Unicode);
            var actualContent = await streamReader.ReadToEndAsync();

            Assert.Equal(content, actualContent);
        }
Exemplo n.º 9
0
        public async Task WritesData_OfDifferentLength_InExpectedEncoding(
            char character,
            int charCount,
            string encodingName)
        {
            // Arrange
            var    encoding      = Encoding.GetEncoding(encodingName);
            string data          = new string(character, charCount);
            var    expectedBytes = encoding.GetBytes(data);
            var    stream        = new MemoryStream();
            var    writer        = new HttpResponseStreamWriter(stream, encoding);

            // Act
            using (writer)
            {
                await writer.WriteAsync(data);
            }

            // Assert
            Assert.Equal(expectedBytes, stream.ToArray());
        }
Exemplo n.º 10
0
        public void HttpResponseStreamWriter_UsingPooledBuffers()
        {
            // Arrange
            var encoding = Encoding.UTF8;
            var stream   = new MemoryStream();

            var expectedBytes = encoding.GetBytes("Hello, World!");

            using (var writer = new HttpResponseStreamWriter(
                       stream,
                       encoding,
                       1024,
                       ArrayPool <byte> .Shared,
                       ArrayPool <char> .Shared))
            {
                // Act
                writer.Write("Hello, World!");
            }

            // Assert
            Assert.Equal(expectedBytes, stream.ToArray());
        }
Exemplo n.º 11
0
        public async Task FlushWriteThrows_DontFlushInDispose(int byteLength)
        {
            // Arrange
            var stream = new TestMemoryStream()
            {
                ThrowOnWrite = true
            };
            var writer = new HttpResponseStreamWriter(stream, Encoding.UTF8);

            await writer.WriteAsync(new string('a', byteLength));

            await Assert.ThrowsAsync <IOException>(() => writer.FlushAsync());

            // Act
            writer.Dispose();

            // Assert
            Assert.Equal(1, stream.WriteAsyncCallCount);
            Assert.Equal(0, stream.WriteCallCount);
            Assert.Equal(0, stream.FlushCallCount);
            Assert.Equal(0, stream.FlushAsyncCallCount);
            Assert.Equal(0, stream.Length);
        }
Exemplo n.º 12
0
        public async Task ExecuteAsync(ActionContext context, ViewComponentResult viewComponentResult)
        {
            var response = context.HttpContext.Response;

            var viewData = viewComponentResult.ViewData;
            if (viewData == null)
            {
                viewData = new ViewDataDictionary(_modelMetadataProvider, context.ModelState);
            }

            var tempData = viewComponentResult.TempData;
            if (tempData == null)
            {
                tempData = _tempDataDictionaryFactory.GetTempData(context.HttpContext);
            }

            string resolvedContentType;
            Encoding resolvedContentTypeEncoding;
            ResponseContentTypeHelper.ResolveContentTypeAndEncoding(
                viewComponentResult.ContentType,
                response.ContentType,
                ViewExecutor.DefaultContentType,
                out resolvedContentType,
                out resolvedContentTypeEncoding);

            response.ContentType = resolvedContentType;

            if (viewComponentResult.StatusCode != null)
            {
                response.StatusCode = viewComponentResult.StatusCode.Value;
            }

            using (var writer = new HttpResponseStreamWriter(response.Body, resolvedContentTypeEncoding))
            {
                var viewContext = new ViewContext(
                    context,
                    NullView.Instance,
                    viewData,
                    tempData,
                    writer,
                    _htmlHelperOptions);

                (_viewComponentHelper as IViewContextAware)?.Contextualize(viewContext);
                var result = await GetViewComponentResult(_viewComponentHelper, _logger, viewComponentResult);

                result.WriteTo(writer, _htmlEncoder);
            }
        }