Esempio n. 1
0
        public async Task WriteToStreamAsync_UsesCorrectCharacterEncoding(string content, string encodingAsString, bool isDefaultEncoding)
        {
            // Arrange
            var formatter = new YamlOutputFormatter((Serializer) new SerializerBuilder()
                                                    .WithNamingConvention(CamelCaseNamingConvention.Instance)
                                                    .WithTypeInspector(inner => new CommentGatheringTypeInspector(inner))
                                                    .WithEmissionPhaseObjectGraphVisitor(args => new CommentsObjectGraphVisitor(args.InnerVisitor))
                                                    .Build());

            var expectedContent = $"\"{content}\"";
            var mediaType       = MediaTypeHeaderValue.Parse(string.Format("application/json; charset={0}", encodingAsString));
            var encoding        = CreateOrGetSupportedEncoding(formatter, encodingAsString, isDefaultEncoding);


            var body          = new MemoryStream();
            var actionContext = GetActionContext(mediaType, body);

            var outputFormatterContext = new OutputFormatterWriteContext(
                actionContext.HttpContext,
                new HttpResponseStreamWriterFactoryFake().CreateWriter,
                typeof(string),
                content)
            {
                ContentType = new StringSegment(mediaType.ToString()),
            };

            // Act
            await formatter.WriteResponseBodyAsync(outputFormatterContext, Encoding.GetEncoding(encodingAsString));

            // Assert
            var actualContent = string.Concat("\"", encoding.GetString(body.ToArray()).TrimEnd('\n'), "\"");

            Assert.Equal(expectedContent, actualContent, StringComparer.OrdinalIgnoreCase);
        }
Esempio n. 2
0
        public async Task WriteToStreamAsync_EncodingNull_ReturnsArgumentNullException(string content, string encodingAsString, bool isDefaultEncoding)
        {
            // Arrange
            var formatter = new YamlOutputFormatter((Serializer) new SerializerBuilder()
                                                    .WithNamingConvention(CamelCaseNamingConvention.Instance)
                                                    .WithTypeInspector(inner => new CommentGatheringTypeInspector(inner))
                                                    .WithEmissionPhaseObjectGraphVisitor(args => new CommentsObjectGraphVisitor(args.InnerVisitor))
                                                    .Build());

            var expectedContent = $"\"{content}\"";
            var mediaType       = MediaTypeHeaderValue.Parse(string.Format("application/json; charset={0}", encodingAsString));
            var encoding        = CreateOrGetSupportedEncoding(formatter, encodingAsString, isDefaultEncoding);


            var body          = new MemoryStream();
            var actionContext = GetActionContext(mediaType, body);

            var outputFormatterContext = new OutputFormatterWriteContext(
                actionContext.HttpContext,
                new HttpResponseStreamWriterFactoryFake().CreateWriter,
                typeof(string),
                content)
            {
                ContentType = new StringSegment(mediaType.ToString()),
            };

            // Act
            Exception caughtException = null;

            try
            {
                await formatter.WriteResponseBodyAsync(outputFormatterContext, null);
            }
            catch (Exception ex)
            {
                caughtException = ex;
            }

            // Assert
            Assert.True(caughtException is ArgumentNullException);
        }
Esempio n. 3
0
        public async Task WriteToStreamAsync_ContextNull_ReturnsArgumentNullException()
        {
            // Arrange
            var formatter = new YamlOutputFormatter((Serializer) new SerializerBuilder()
                                                    .WithNamingConvention(CamelCaseNamingConvention.Instance)
                                                    .WithTypeInspector(inner => new CommentGatheringTypeInspector(inner))
                                                    .WithEmissionPhaseObjectGraphVisitor(args => new CommentsObjectGraphVisitor(args.InnerVisitor))
                                                    .Build());

            // Act
            Exception caughtException = null;

            try
            {
                await formatter.WriteResponseBodyAsync(null, Encoding.UTF8);
            }
            catch (Exception ex)
            {
                caughtException = ex;
            }

            // Assert
            Assert.True(caughtException is ArgumentNullException);
        }