Example #1
0
        public void CanWriteResult_ReturnsTrueForStringTypes(
            object value,
            bool useDeclaredTypeAsString,
            bool expectedCanWriteResult)
        {
            // Arrange
            var expectedContentType = new StringSegment("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 = expectedContentType;

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

            // Assert
            Assert.Equal(expectedCanWriteResult, result);
            Assert.Equal(expectedContentType, context.ContentType);
        }
Example #2
0
        public void CanWriteResult_DefaultContentType()
        {
            // Arrange
            var formatter = new StringOutputFormatter();
            var context   = new OutputFormatterWriteContext(
                new DefaultHttpContext(),
                new TestHttpResponseStreamWriterFactory().CreateWriter,
                typeof(string),
                "Thisisastring");

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

            // Assert
            Assert.True(result);
            Assert.Equal(new StringSegment("text/plain"), context.ContentType);
        }
Example #3
0
        public void CannotWriteNonStrings(object value)
        {
            // Arrange
            var expectedContentType = new StringSegment("text/plain");
            var formatter           = new StringOutputFormatter();
            var context             = new OutputFormatterWriteContext(
                new DefaultHttpContext(),
                new TestHttpResponseStreamWriterFactory().CreateWriter,
                typeof(object),
                value);

            context.ContentType = new StringSegment("text/plain");

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

            // Assert
            Assert.False(result);
            Assert.Equal(expectedContentType, context.ContentType);
        }
Example #4
0
        public void CanWriteResult_SetsAcceptContentType()
        {
            // Arrange
            var formatter           = new StringOutputFormatter();
            var expectedContentType = new StringSegment("application/json");

            var context = new OutputFormatterWriteContext(
                new DefaultHttpContext(),
                new TestHttpResponseStreamWriterFactory().CreateWriter,
                typeof(string),
                "Thisisastring");

            context.ContentType = expectedContentType;

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

            // Assert
            Assert.True(result);
            Assert.Equal(expectedContentType, context.ContentType);
        }
Example #5
0
        public void CannotWriteUnsupportedMediaType(string contentType)
        {
            // Arrange
            var formatter           = new StringOutputFormatter();
            var expectedContentType = new StringSegment(contentType);

            var context = new OutputFormatterWriteContext(
                new DefaultHttpContext(),
                new TestHttpResponseStreamWriterFactory().CreateWriter,
                typeof(string),
                "Thisisastring");

            context.ContentType = new StringSegment(contentType);

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

            // Assert
            Assert.False(result);
            Assert.Equal(expectedContentType, context.ContentType);
        }