Esempio n. 1
0
        public async Task ExecuteAsync_WritesXmlDataContractContent()
        {
            // Arrange
            var value   = new PurchaseOrder();
            var context = GetActionContext();

            CreateServices(context.HttpContext);

            //
            var result = new XmlResult(value)
            {
                XmlSerializerType = XmlSerializerType.DataContractSerializer
            };

            // Act
            await result.ExecuteResultAsync(context);

            // Assert
            Assert.Equal("application/xml; charset=utf-8", context.HttpContext.Response.ContentType);

            // Verify to as the new restored object
            //There may be differ DataContract style has been used
            var written  = GetWrittenBytes(context.HttpContext);
            var sWritten = Encoding.UTF8.GetString(written);

            StringReader           sreader  = new StringReader(sWritten);
            DataContractSerializer ser      = new DataContractSerializer(typeof(PurchaseOrder));
            PurchaseOrder          newValue = (PurchaseOrder)ser.ReadObject(XmlReader.Create(sreader));

            Assert.Equal(value.billTo.street, newValue.billTo.street);
            Assert.Equal(value.shipTo.street, newValue.shipTo.street);
        }
Esempio n. 2
0
        public async Task ExecuteAsync_WritesXmlContent()
        {
            // Arrange
            var value             = new PurchaseOrder();
            var xmlWriterSettings = FormattingUtilities.GetDefaultXmlWriterSettings();

            xmlWriterSettings.CloseOutput = false;
            var textw         = new StringWriter();
            var writer        = XmlWriter.Create(textw, xmlWriterSettings);
            var xmlSerializer = new XmlSerializer(value.GetType());

            xmlSerializer.Serialize(writer, value);
            var expected = Encoding.UTF8.GetBytes(textw.ToString());
            var context  = GetActionContext();

            CreateServices(context.HttpContext);

            //
            var result = new XmlResult(value);

            // Act
            await result.ExecuteResultAsync(context);

            // Assert
            var written = GetWrittenBytes(context.HttpContext);

            var s1 = Encoding.UTF8.GetString(expected);
            var s2 = Encoding.UTF8.GetString(written);

            //Assert.AreEqual(expected, written);
            Assert.Equal(s1, s2);
            Assert.Equal("application/xml; charset=utf-8", context.HttpContext.Response.ContentType);
        }
Esempio n. 3
0
        public async Task ExecuteAsync_WritesXmlContent_Negative()
        {
            // Arrange
            var value             = new PurchaseOrder();
            var xmlWriterSettings = FormattingUtilities.GetDefaultXmlWriterSettings();

            xmlWriterSettings.CloseOutput = false;
            var textw         = new StringWriter();
            var writer        = XmlWriter.Create(textw, xmlWriterSettings);
            var xmlSerializer = new XmlSerializer(value.GetType());

            xmlSerializer.Serialize(writer, value);
            var expected = Encoding.UTF8.GetBytes(textw.ToString());
            var context  = GetActionContext();

            CreateServices(context.HttpContext, true);

            //
            var result = new XmlResult(value);

            // Act
            await result.ExecuteResultAsync(context);

            // Assert
            Assert.Equal(context.HttpContext.Response.StatusCode, StatusCodes.Status406NotAcceptable);
        }
Esempio n. 4
0
        public async Task ExecuteAsync_WritesXmlDataContractContent_Negative()
        {
            // Arrange
            var value   = new PurchaseOrder();
            var context = GetActionContext();

            CreateServices(context.HttpContext, true);

            //
            var result = new XmlResult(value)
            {
                XmlSerializerType = XmlSerializerType.DataContractSerializer
            };

            // Act
            await result.ExecuteResultAsync(context);

            Assert.Equal(context.HttpContext.Response.StatusCode, StatusCodes.Status406NotAcceptable);
        }