Esempio n. 1
0
    public async Task ReadAsync_DoesNotDisposeBufferedStreamIfItDidNotCreateIt()
    {
        // Arrange
        var expectedInt    = 10;
        var expectedString = "TestString";

        var input = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                    "<TestLevelOne><SampleInt>" + expectedInt + "</SampleInt>" +
                    "<sampleString>" + expectedString + "</sampleString></TestLevelOne>";

        var formatter = new XmlSerializerInputFormatter(new MvcOptions());

        var contentBytes           = Encoding.UTF8.GetBytes(input);
        var httpContext            = new DefaultHttpContext();
        var testBufferedReadStream = new VerifyDisposeFileBufferingReadStream(new MemoryStream(contentBytes), 1024);

        httpContext.Request.Body = testBufferedReadStream;
        var context = GetInputFormatterContext(httpContext, typeof(TestLevelOne));

        // Act
        var result = await formatter.ReadAsync(context);

        // Assert
        Assert.NotNull(result);
        Assert.False(result.HasError);
        var model = Assert.IsType <TestLevelOne>(result.Model);

        Assert.Equal(expectedInt, model.SampleInt);
        Assert.Equal(expectedString, model.sampleString);
        Assert.False(testBufferedReadStream.Disposed);
    }
        public async Task ReadAsync_DoesNotDisposeBufferedReadStream()
        {
            // Arrange
            var formatter = GetInputFormatter();

            var content                = "{\"name\": \"Test\"}";
            var contentBytes           = Encoding.UTF8.GetBytes(content);
            var httpContext            = GetHttpContext(contentBytes);
            var testBufferedReadStream = new VerifyDisposeFileBufferingReadStream(httpContext.Request.Body, 1024);

            httpContext.Request.Body = testBufferedReadStream;

            var formatterContext = CreateInputFormatterContext(typeof(ComplexModel), httpContext);

            // Act
            var result = await formatter.ReadAsync(formatterContext);

            // Assert
            var userModel = Assert.IsType <ComplexModel>(result.Model);

            Assert.Equal("Test", userModel.Name);
            Assert.False(testBufferedReadStream.Disposed);
        }