예제 #1
0
        public void WriteResponseContentHeaders_NoSupportedEncodings_NoEncodingIsSet()
        {
            // Arrange
            var formatter = new TestOutputFormatter();

            var testContentType = new StringSegment("text/json");

            formatter.SupportedEncodings.Clear();
            formatter.SupportedMediaTypes.Clear();
            formatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/json"));

            var context = new OutputFormatterWriteContext(
                new DefaultHttpContext(),
                new TestHttpResponseStreamWriterFactory().CreateWriter,
                objectType: null,
                @object: null)
            {
                ContentType = testContentType,
            };

            // Act
            formatter.WriteResponseHeaders(context);

            // Assert
            Assert.Null(MediaTypeHeaderValue.Parse(context.ContentType.Value).Encoding);
            Assert.Equal(testContentType, context.ContentType);

            // If we had set an encoding, it would be part of the content type header
            Assert.Equal(MediaTypeHeaderValue.Parse(testContentType.Value), context.HttpContext.Response.GetTypedHeaders().ContentType);
        }
예제 #2
0
        public void WriteResponseContentHeaders_NoSupportedEncodings_NoEncodingIsSet()
        {
            // Arrange
            var formatter = new TestOutputFormatter();

            var testContentType = MediaTypeHeaderValue.Parse("text/json");

            formatter.SupportedEncodings.Clear();
            formatter.SupportedMediaTypes.Clear();
            formatter.SupportedMediaTypes.Add(testContentType);

            var formatterContext = new OutputFormatterContext()
            {
                HttpContext = new DefaultHttpContext(),
            };

            // Act
            formatter.WriteResponseHeaders(formatterContext);

            // Assert
            Assert.Null(formatterContext.SelectedEncoding);
            Assert.Equal(testContentType, formatterContext.SelectedContentType);

            // If we had set an encoding, it would be part of the content type header
            Assert.Equal(testContentType, formatterContext.HttpContext.Response.GetTypedHeaders().ContentType);
        }
예제 #3
0
        public void WriteResponseContentHeaders_FormatterWithNoEncoding_Throws()
        {
            // Arrange
            var testFormatter    = new TestOutputFormatter();
            var testContentType  = MediaTypeHeaderValue.Parse("text/invalid");
            var formatterContext = new OutputFormatterContext();
            var mockHttpContext  = new Mock <HttpContext>();
            var httpRequest      = new DefaultHttpContext().Request;

            mockHttpContext.SetupGet(o => o.Request).Returns(httpRequest);
            formatterContext.HttpContext = mockHttpContext.Object;

            // Act & Assert
            var ex = Assert.Throws <InvalidOperationException>(
                () => testFormatter.WriteResponseHeaders(formatterContext));

            Assert.Equal("No encoding found for output formatter " +
                         "'Microsoft.AspNet.Mvc.Test.OutputFormatterTests+TestOutputFormatter'." +
                         " There must be at least one supported encoding registered in order for the" +
                         " output formatter to write content.", ex.Message);
        }
예제 #4
0
        public void WriteResponseContentHeaders_FormatterWithNoEncoding_Throws()
        {
            // Arrange
            var testFormatter = new TestOutputFormatter();
            var testContentType = MediaTypeHeaderValue.Parse("text/invalid");
            var formatterContext = new OutputFormatterContext();
            var mockHttpContext = new Mock<HttpContext>();
            var httpRequest = new DefaultHttpContext().Request;
            mockHttpContext.SetupGet(o => o.Request).Returns(httpRequest);
            formatterContext.HttpContext = mockHttpContext.Object;

            // Act & Assert
            var ex = Assert.Throws<InvalidOperationException>(
                        () => testFormatter.WriteResponseHeaders(formatterContext));
            Assert.Equal("No encoding found for output formatter " +
                         "'Microsoft.AspNet.Mvc.Test.OutputFormatterTests+TestOutputFormatter'." +
                         " There must be at least one supported encoding registered in order for the" +
                         " output formatter to write content.", ex.Message);
        }
예제 #5
0
        public void WriteResponseContentHeaders_NoSupportedEncodings_NoEncodingIsSet()
        {
            // Arrange
            var formatter = new TestOutputFormatter();

            var testContentType = new StringSegment("text/json");

            formatter.SupportedEncodings.Clear();
            formatter.SupportedMediaTypes.Clear();
            formatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/json"));

            var context = new OutputFormatterWriteContext(
                new DefaultHttpContext(),
                new TestHttpResponseStreamWriterFactory().CreateWriter,
                objectType: null,
                @object: null)
            {
                ContentType = testContentType,
            };

            // Act
            formatter.WriteResponseHeaders(context);

            // Assert
            Assert.Null(MediaTypeHeaderValue.Parse(context.ContentType.Value).Encoding);
            Assert.Equal(testContentType, context.ContentType);

            // If we had set an encoding, it would be part of the content type header
            Assert.Equal(MediaTypeHeaderValue.Parse(testContentType.Value), context.HttpContext.Response.GetTypedHeaders().ContentType);
        }
예제 #6
0
        public void WriteResponseContentHeaders_NoSupportedEncodings_NoEncodingIsSet()
        {
            // Arrange
            var formatter = new TestOutputFormatter();

            var testContentType = MediaTypeHeaderValue.Parse("text/json");

            formatter.SupportedEncodings.Clear();
            formatter.SupportedMediaTypes.Clear();
            formatter.SupportedMediaTypes.Add(testContentType);

            var formatterContext = new OutputFormatterContext()
            {
                HttpContext = new DefaultHttpContext(),
            };

            // Act
            formatter.WriteResponseHeaders(formatterContext);

            // Assert
            Assert.Null(formatterContext.SelectedEncoding);
            Assert.Equal(testContentType, formatterContext.SelectedContentType);

            // If we had set an encoding, it would be part of the content type header
            Assert.Equal(testContentType, formatterContext.HttpContext.Response.GetTypedHeaders().ContentType);
        }