Ejemplo n.º 1
0
        public async Task DisablesResponseBuffering_IfBufferingFeatureAvailable()
        {
            // Arrange
            var formatter = new StreamOutputFormatter();

            var expected = Encoding.UTF8.GetBytes("Test data");

            var httpContext = new DefaultHttpContext();
            var body        = new MemoryStream();

            httpContext.Response.Body = body;

            var bufferingFeature = new TestBufferingFeature();

            httpContext.Features.Set <IHttpBufferingFeature>(bufferingFeature);

            var context = new OutputFormatterWriteContext(
                httpContext,
                new TestHttpResponseStreamWriterFactory().CreateWriter,
                typeof(Stream),
                new MemoryStream(expected));

            // Act
            await formatter.WriteAsync(context);

            // Assert
            Assert.Equal(expected, body.ToArray());
            Assert.True(bufferingFeature.DisableResponseBufferingInvoked);
        }
Ejemplo n.º 2
0
        public void CanWriteResult_SetsContentType()
        {
            // Arrange
            var formatter   = new StreamOutputFormatter();
            var contentType = new MediaTypeHeaderValue("text/plain");
            var context     = new OutputFormatterContext();

            context.Object = new MemoryStream();

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

            // Assert
            Assert.True(result);
            Assert.Same(contentType, context.SelectedContentType);
        }
Ejemplo n.º 3
0
        public void CanWriteResult_OnlyActsOnStreams(Type type)
        {
            // Arrange
            var formatter = new StreamOutputFormatter();
            var context = new OutputFormatterContext();
            var contentType = new MediaTypeHeaderValue("text/plain");

            context.Object = type != null ? Activator.CreateInstance(type) : null;

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

            // Assert
            Assert.False(result);
            Assert.Null(context.SelectedContentType);
        }
Ejemplo n.º 4
0
        public void CanWriteResult_OnlyActsOnStreams(Type type)
        {
            // Arrange
            var formatter   = new StreamOutputFormatter();
            var context     = new OutputFormatterContext();
            var contentType = new MediaTypeHeaderValue("text/plain");

            context.Object = type != null?Activator.CreateInstance(type) : null;

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

            // Assert
            Assert.False(result);
            Assert.Null(context.SelectedContentType);
        }
Ejemplo n.º 5
0
        public void CanWriteResult_OnlyActsOnStreams_IgnoringContentType(Type declaredType, string contentType)
        {
            // Arrange
            var formatter = new StreamOutputFormatter();
            var contentTypeHeader = contentType == null ? null : new MediaTypeHeaderValue(contentType);
            var formatterContext = new OutputFormatterContext()
            {
                DeclaredType = declaredType,
                Object = new SimplePOCO()
            };

            // Act
            var canWrite = formatter.CanWriteResult(formatterContext, contentTypeHeader);

            // Assert
            Assert.False(canWrite);
        }
Ejemplo n.º 6
0
        public void CanWriteResult_ReturnsTrue_ForStreams(Type declaredType, string contentType)
        {
            // Arrange
            var formatter         = new StreamOutputFormatter();
            var contentTypeHeader = contentType == null ? null : new MediaTypeHeaderValue(contentType);
            var formatterContext  = new OutputFormatterContext()
            {
                DeclaredType = declaredType,
                Object       = new MemoryStream()
            };

            // Act
            var canWrite = formatter.CanWriteResult(formatterContext, contentTypeHeader);

            // Assert
            Assert.True(canWrite);
        }
Ejemplo n.º 7
0
        public void CanWriteResult_OnlyActsOnStreams_IgnoringContentType(Type declaredType, string contentType)
        {
            // Arrange
            var formatter         = new StreamOutputFormatter();
            var contentTypeHeader = contentType == null ? null : new MediaTypeHeaderValue(contentType);
            var formatterContext  = new OutputFormatterContext()
            {
                DeclaredType = declaredType,
                Object       = new SimplePOCO()
            };

            // Act
            var canWrite = formatter.CanWriteResult(formatterContext, contentTypeHeader);

            // Assert
            Assert.False(canWrite);
        }
Ejemplo n.º 8
0
        public void CanWriteResult_ReturnsTrue_ForStreams(Type declaredType, string contentType)
        {
            // Arrange
            var formatter = new StreamOutputFormatter();
            var contentTypeHeader = contentType == null ? null : new MediaTypeHeaderValue(contentType);
            var formatterContext = new OutputFormatterContext()
            {
                DeclaredType = declaredType,
                Object = new MemoryStream()
            };

            // Act
            var canWrite = formatter.CanWriteResult(formatterContext, contentTypeHeader);

            // Assert
            Assert.True(canWrite);
        }
Ejemplo n.º 9
0
        public void CanWriteResult_OnlyActsOnStreams(Type type)
        {
            // Arrange
            var formatter = new StreamOutputFormatter();
            var @object   = type != null?Activator.CreateInstance(type) : null;

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

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

            // Assert
            Assert.False(result);
        }
Ejemplo n.º 10
0
        public void CanWriteResult_OnlyActsOnStreams(Type type)
        {
            // Arrange
            var formatter = new StreamOutputFormatter();
            var @object = type != null ? Activator.CreateInstance(type) : null;

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

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

            // Assert
            Assert.False(result);
        }
Ejemplo n.º 11
0
        public void CanWriteResult_OnlyActsOnStreams_IgnoringContentType(Type type, string contentType)
        {
            // Arrange
            var formatter         = new StreamOutputFormatter();
            var contentTypeHeader = contentType == null ? null : new MediaTypeHeaderValue(contentType);

            var context = new OutputFormatterWriteContext(
                new DefaultHttpContext(),
                new TestHttpResponseStreamWriterFactory().CreateWriter,
                type,
                new SimplePOCO())
            {
                ContentType = contentTypeHeader,
            };

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

            // Assert
            Assert.False(canWrite);
        }
Ejemplo n.º 12
0
        public void CanWriteResult_OnlyActsOnStreams_IgnoringContentType(Type type, string contentType)
        {
            // Arrange
            var formatter = new StreamOutputFormatter();
            var contentTypeHeader = contentType == null ? null : new MediaTypeHeaderValue(contentType);

            var context = new OutputFormatterWriteContext(
                new DefaultHttpContext(),
                new TestHttpResponseStreamWriterFactory().CreateWriter,
                type,
                new SimplePOCO())
            {
                ContentType = contentTypeHeader,
            };

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

            // Assert
            Assert.False(canWrite);
        }
Ejemplo n.º 13
0
        public void CanWriteResult_ReturnsTrue_ForStreams(Type type, string contentType)
        {
            // Arrange
            var formatter = new StreamOutputFormatter();
            var contentTypeHeader = contentType == null ? null : new MediaTypeHeaderValue(contentType);

            var context = new OutputFormatterWriteContext(
                new DefaultHttpContext(),
                new TestHttpResponseStreamWriterFactory().CreateWriter,
                type,
                new MemoryStream())
            {
                ContentType = contentTypeHeader,
            };

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

            // Assert
            Assert.True(canWrite);
        }
Ejemplo n.º 14
0
        public void CanWriteResult_ReturnsTrue_ForStreams(Type type, string contentType)
        {
            // Arrange
            var formatter         = new StreamOutputFormatter();
            var contentTypeHeader = contentType == null ? null : new MediaTypeHeaderValue(contentType);

            var context = new OutputFormatterWriteContext(
                new DefaultHttpContext(),
                new TestHttpResponseStreamWriterFactory().CreateWriter,
                type,
                new MemoryStream())
            {
                ContentType = contentTypeHeader,
            };

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

            // Assert
            Assert.True(canWrite);
        }
Ejemplo n.º 15
0
        public async Task DisablesResponseBuffering_IfBufferingFeatureAvailable()
        {
            // Arrange
            var expected    = Encoding.UTF8.GetBytes("Test data");
            var formatter   = new StreamOutputFormatter();
            var httpContext = new DefaultHttpContext();
            var ms          = new MemoryStream();

            httpContext.Response.Body = ms;
            var bufferingFeature = new TestBufferingFeature();

            httpContext.Features.Set <IHttpBufferingFeature>(bufferingFeature);
            var context = new OutputFormatterContext();

            context.Object      = new MemoryStream(expected);
            context.HttpContext = httpContext;

            // Act
            await formatter.WriteAsync(context);

            // Assert
            Assert.Equal(expected, ms.ToArray());
            Assert.True(bufferingFeature.DisableResponseBufferingInvoked);
        }
Ejemplo n.º 16
0
        public async Task DisablesResponseBuffering_IfBufferingFeatureAvailable()
        {
            // Arrange
            var formatter = new StreamOutputFormatter();

            var expected = Encoding.UTF8.GetBytes("Test data");

            var httpContext = new DefaultHttpContext();
            var body = new MemoryStream();
            httpContext.Response.Body = body;

            var bufferingFeature = new TestBufferingFeature();
            httpContext.Features.Set<IHttpBufferingFeature>(bufferingFeature);

            var context = new OutputFormatterWriteContext(
                httpContext,
                new TestHttpResponseStreamWriterFactory().CreateWriter,
                typeof(Stream),
                new MemoryStream(expected));

            // Act
            await formatter.WriteAsync(context);

            // Assert
            Assert.Equal(expected, body.ToArray());
            Assert.True(bufferingFeature.DisableResponseBufferingInvoked);
        }
Ejemplo n.º 17
0
        public void CanWriteResult_SetsContentType()
        {
            // Arrange
            var formatter = new StreamOutputFormatter();
            var contentType = new MediaTypeHeaderValue("text/plain");
            var context = new OutputFormatterContext();
            context.Object = new MemoryStream();

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

            // Assert
            Assert.True(result);
            Assert.Same(contentType, context.SelectedContentType);
        }
Ejemplo n.º 18
0
        public async Task DisablesResponseBuffering_IfBufferingFeatureAvailable()
        {
            // Arrange
            var expected = Encoding.UTF8.GetBytes("Test data");
            var formatter = new StreamOutputFormatter();
            var httpContext = new DefaultHttpContext();
            var ms = new MemoryStream();
            httpContext.Response.Body = ms;
            var bufferingFeature = new TestBufferingFeature();
            httpContext.Features.Set<IHttpBufferingFeature>(bufferingFeature);
            var context = new OutputFormatterContext();
            context.Object = new MemoryStream(expected);
            context.HttpContext = httpContext;

            // Act
            await formatter.WriteAsync(context);

            // Assert
            Assert.Equal(expected, ms.ToArray());
            Assert.True(bufferingFeature.DisableResponseBufferingInvoked);
        }