public async Task WriteAsync_DoesNotWriteNullStrings()
        {
            // Arrange
            Encoding encoding = Encoding.UTF8;
            var memoryStream = new MemoryStream();
            var response = new Mock<HttpResponse>();
            response.SetupProperty<long?>(o => o.ContentLength);
            response.SetupGet(r => r.Body).Returns(memoryStream);
            var httpContext = new Mock<HttpContext>();
            httpContext.Setup(o => o.Response).Returns(response.Object);

            var formatter = new StringOutputFormatter();
            var context = new OutputFormatterWriteContext(
                httpContext.Object,
                new TestHttpResponseStreamWriterFactory().CreateWriter,
                typeof(string),
                @object: null);

            // Act
            await formatter.WriteResponseBodyAsync(context);

            // Assert
            Assert.Equal(0, memoryStream.Length);
            response.VerifySet(r => r.ContentLength = It.IsAny<long?>(), Times.Never());
        }
        public async Task WriteAsync_DoesNotWriteNullStrings()
        {
            // Arrange
            Encoding encoding     = Encoding.UTF8;
            var      memoryStream = new MemoryStream();
            var      response     = new Mock <HttpResponse>();

            response.SetupProperty <long?>(o => o.ContentLength);
            response.SetupGet(r => r.Body).Returns(memoryStream);
            var mockHttpContext = new Mock <HttpContext>();

            mockHttpContext.Setup(o => o.Response).Returns(response.Object);

            var formatter        = new StringOutputFormatter();
            var formatterContext = new OutputFormatterContext()
            {
                Object           = null,
                DeclaredType     = typeof(string),
                HttpContext      = mockHttpContext.Object,
                SelectedEncoding = encoding
            };

            // Act
            await formatter.WriteResponseBodyAsync(formatterContext);

            // Assert
            Assert.Equal(0, memoryStream.Length);
            response.VerifySet(r => r.ContentLength = It.IsAny <long?>(), Times.Never());
        }
        public void CanWriteResult_ReturnsTrueForStringTypes(
            object value,
            bool useDeclaredTypeAsString,
            bool expectedCanWriteResult)
        {
            // Arrange
            var expectedContentType = expectedCanWriteResult ? 
                MediaTypeHeaderValue.Parse("text/plain") :
                MediaTypeHeaderValue.Parse("application/json");

            var formatter = new StringOutputFormatter();
            var type = useDeclaredTypeAsString ? typeof(string) : typeof(object);

            var context = new OutputFormatterWriteContext(
                new DefaultHttpContext(),
                new TestHttpResponseStreamWriterFactory().CreateWriter,
                type,
                value);
            context.ContentType = MediaTypeHeaderValue.Parse("application/json");

            // Act
            var result = formatter.CanWriteResult(context);

            // Assert
            Assert.Equal(expectedCanWriteResult, result);
            Assert.Equal(expectedContentType, context.ContentType);
        }
示例#4
0
        public async Task WriteAsync_DoesNotWriteNullStrings()
        {
            // Arrange
            Encoding encoding     = Encoding.UTF8;
            var      memoryStream = new MemoryStream();
            var      response     = new Mock <HttpResponse>();

            response.SetupProperty <long?>(o => o.ContentLength);
            response.SetupGet(r => r.Body).Returns(memoryStream);
            var httpContext = new Mock <HttpContext>();

            httpContext.Setup(o => o.Response).Returns(response.Object);

            var formatter = new StringOutputFormatter();
            var context   = new OutputFormatterWriteContext(
                httpContext.Object,
                new TestHttpResponseStreamWriterFactory().CreateWriter,
                typeof(string),
                @object: null);

            // Act
            await formatter.WriteResponseBodyAsync(context);

            // Assert
            Assert.Equal(0, memoryStream.Length);
            response.VerifySet(r => r.ContentLength = It.IsAny <long?>(), Times.Never());
        }
示例#5
0
        public void CanWriteResult_ReturnsTrueForStringTypes(
            object value,
            bool useDeclaredTypeAsString,
            bool expectedCanWriteResult)
        {
            // Arrange
            var expectedContentType = expectedCanWriteResult ?
                                      MediaTypeHeaderValue.Parse("text/plain") :
                                      MediaTypeHeaderValue.Parse("application/json");

            var formatter = new StringOutputFormatter();
            var type      = useDeclaredTypeAsString ? typeof(string) : typeof(object);

            var context = new OutputFormatterWriteContext(
                new DefaultHttpContext(),
                new TestHttpResponseStreamWriterFactory().CreateWriter,
                type,
                value);

            context.ContentType = MediaTypeHeaderValue.Parse("application/json");

            // Act
            var result = formatter.CanWriteResult(context);

            // Assert
            Assert.Equal(expectedCanWriteResult, result);
            Assert.Equal(expectedContentType, context.ContentType);
        }
        public async Task WriteAsync_DoesNotWriteNullStrings()
        {
            // Arrange
            Encoding encoding = Encoding.UTF8;
            var memoryStream = new MemoryStream();
            var response = new Mock<HttpResponse>();
            response.SetupProperty<long?>(o => o.ContentLength);
            response.SetupGet(r => r.Body).Returns(memoryStream);
            var mockHttpContext = new Mock<HttpContext>();
            mockHttpContext.Setup(o => o.Response).Returns(response.Object);

            var formatter = new StringOutputFormatter();
            var formatterContext = new OutputFormatterContext()
            {
                Object = null,
                DeclaredType = typeof(string),
                HttpContext = mockHttpContext.Object,
                SelectedEncoding = encoding
            };

            // Act
            await formatter.WriteResponseBodyAsync(formatterContext);

            // Assert
            Assert.Equal(0, memoryStream.Length);
            response.VerifySet(r => r.ContentLength = It.IsAny<long?>(), Times.Never());
        }
        public void CanWriteResult_ReturnsTrueForStringTypes(object value, bool useDeclaredTypeAsString, bool expectedCanWriteResult)
        {
            // Arrange
            var formatter        = new StringOutputFormatter();
            var typeToUse        = useDeclaredTypeAsString ? typeof(string) : typeof(object);
            var formatterContext = new OutputFormatterContext()
            {
                Object       = value,
                DeclaredType = typeToUse
            };

            // Act
            var result = formatter.CanWriteResult(formatterContext, null);

            // Assert
            Assert.Equal(expectedCanWriteResult, result);
        }
        public void CanWriteResult_ReturnsTrueForStringTypes(object value, bool useDeclaredTypeAsString, bool expectedCanWriteResult)
        {
            // Arrange
            var formatter = new StringOutputFormatter();
            var typeToUse = useDeclaredTypeAsString ? typeof(string) : typeof(object);
            var formatterContext = new OutputFormatterContext()
            {
                Object = value,
                DeclaredType = typeToUse
            };

            // Act
            var result = formatter.CanWriteResult(formatterContext, null);

            // Assert
            Assert.Equal(expectedCanWriteResult, result);
        }