public async Task XmlSerializerFormatterReadsSimpleTypes() { // Arrange var expectedInt = 10; var expectedString = "TestString"; var expectedDateTime = XmlConvert.ToString(DateTime.UtcNow, XmlDateTimeSerializationMode.Utc); var input = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<TestLevelOne><SampleInt>" + expectedInt + "</SampleInt>" + "<sampleString>" + expectedString + "</sampleString>" + "<SampleDate>" + expectedDateTime + "</SampleDate></TestLevelOne>"; var formatter = new XmlSerializerInputFormatter(); var contentBytes = Encoding.UTF8.GetBytes(input); var context = GetInputFormatterContext(contentBytes, typeof(TestLevelOne)); // Act await formatter.ReadAsync(context); // Assert Assert.NotNull(context.Model); Assert.IsType<TestLevelOne>(context.Model); var model = context.Model as TestLevelOne; Assert.Equal(expectedInt, model.SampleInt); Assert.Equal(expectedString, model.sampleString); Assert.Equal(XmlConvert.ToDateTime(expectedDateTime, XmlDateTimeSerializationMode.Utc), model.SampleDate); }
public async Task XmlSerializerFormatterIgnoresBOMCharacters() { // Arrange var sampleString = "Test"; var sampleStringBytes = Encoding.UTF8.GetBytes(sampleString); var inputStart = Encoding.UTF8.GetBytes("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + Environment.NewLine + "<TestLevelTwo><SampleString>" + sampleString); byte[] bom = { 0xef, 0xbb, 0xbf }; var inputEnd = Encoding.UTF8.GetBytes("</SampleString></TestLevelTwo>"); var expectedBytes = new byte[sampleString.Length + bom.Length]; var contentBytes = new byte[inputStart.Length + bom.Length + inputEnd.Length]; Buffer.BlockCopy(inputStart, 0, contentBytes, 0, inputStart.Length); Buffer.BlockCopy(bom, 0, contentBytes, inputStart.Length, bom.Length); Buffer.BlockCopy(inputEnd, 0, contentBytes, inputStart.Length + bom.Length, inputEnd.Length); var formatter = new XmlSerializerInputFormatter(); var context = GetInputFormatterContext(contentBytes, typeof(TestLevelTwo)); // Act await formatter.ReadAsync(context); // Assert Assert.NotNull(context.Model); var model = context.Model as TestLevelTwo; Buffer.BlockCopy(sampleStringBytes, 0, expectedBytes, 0, sampleStringBytes.Length); Buffer.BlockCopy(bom, 0, expectedBytes, sampleStringBytes.Length, bom.Length); Assert.Equal(expectedBytes, Encoding.UTF8.GetBytes(model.SampleString)); }
public async Task XmlSerializerFormatterReadsSimpleTypes() { // Arrange var expectedInt = 10; var expectedString = "TestString"; var expectedDateTime = XmlConvert.ToString(DateTime.UtcNow, XmlDateTimeSerializationMode.Utc); var input = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<TestLevelOne><SampleInt>" + expectedInt + "</SampleInt>" + "<sampleString>" + expectedString + "</sampleString>" + "<SampleDate>" + expectedDateTime + "</SampleDate></TestLevelOne>"; var formatter = new XmlSerializerInputFormatter(); var contentBytes = Encoding.UTF8.GetBytes(input); var context = GetInputFormatterContext(contentBytes, typeof(TestLevelOne)); // Act await formatter.ReadAsync(context); // Assert Assert.NotNull(context.Model); Assert.IsType <TestLevelOne>(context.Model); var model = context.Model as TestLevelOne; Assert.Equal(expectedInt, model.SampleInt); Assert.Equal(expectedString, model.sampleString); Assert.Equal(XmlConvert.ToDateTime(expectedDateTime, XmlDateTimeSerializationMode.Utc), model.SampleDate); }
public async Task VerifyStreamIsOpenAfterRead() { // Arrange var input = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<DummyClass><SampleInt>10</SampleInt></DummyClass>"; var formatter = new XmlSerializerInputFormatter(); var contentBytes = Encoding.UTF8.GetBytes(input); var context = GetInputFormatterContext(contentBytes, typeof(DummyClass)); // Act await formatter.ReadAsync(context); // Assert Assert.NotNull(context.Model); Assert.True(context.HttpContext.Request.Body.CanRead); }
public async Task XmlSerializerFormatterThrowsOnInvalidCharacters() { // Arrange var inpStart = Encodings.UTF16EncodingLittleEndian.GetBytes("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<DummyClass><SampleInt>"); byte[] inp = { 192, 193 }; var inpEnd = Encodings.UTF16EncodingLittleEndian.GetBytes("</SampleInt></DummyClass>"); var contentBytes = new byte[inpStart.Length + inp.Length + inpEnd.Length]; Buffer.BlockCopy(inpStart, 0, contentBytes, 0, inpStart.Length); Buffer.BlockCopy(inp, 0, contentBytes, inpStart.Length, inp.Length); Buffer.BlockCopy(inpEnd, 0, contentBytes, inpStart.Length + inp.Length, inpEnd.Length); var formatter = new XmlSerializerInputFormatter(); var context = GetInputFormatterContext(contentBytes, typeof(TestLevelTwo)); // Act await Assert.ThrowsAsync(typeof(XmlException), async() => await formatter.ReadAsync(context)); }
public async Task XmlSerializerFormatterReadsWhenMaxDepthIsModified() { // Arrange var expectedInt = 10; var input = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<DummyClass><SampleInt>" + expectedInt + "</SampleInt></DummyClass>"; var formatter = new XmlSerializerInputFormatter(); formatter.MaxDepth = 10; var contentBytes = Encoding.UTF8.GetBytes(input); var context = GetInputFormatterContext(contentBytes, typeof(DummyClass)); // Act await formatter.ReadAsync(context); // Assert Assert.NotNull(context.Model); Assert.IsType <DummyClass>(context.Model); var model = context.Model as DummyClass; Assert.Equal(expectedInt, model.SampleInt); }
public async Task XmlSerializerFormatterThrowsWhenReaderQuotasAreChanged() { // Arrange var input = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<TestLevelTwo><SampleString>test</SampleString>" + "<TestOne><SampleInt>10</SampleInt>" + "<sampleString>test</sampleString>" + "<SampleDate>" + XmlConvert.ToString(DateTime.UtcNow, XmlDateTimeSerializationMode.Utc) + "</SampleDate></TestOne></TestLevelTwo>"; var formatter = new XmlSerializerInputFormatter(); formatter.XmlDictionaryReaderQuotas.MaxStringContentLength = 10; var contentBytes = Encoding.UTF8.GetBytes(input); var context = GetInputFormatterContext(contentBytes, typeof(TestLevelTwo)); // Act & Assert await Assert.ThrowsAsync(typeof(InvalidOperationException), async() => await formatter.ReadAsync(context)); }
public async Task XmlSerializerFormatterIgnoresBOMCharacters() { // Arrange var sampleString = "Test"; var sampleStringBytes = Encoding.UTF8.GetBytes(sampleString); var inputStart = Encoding.UTF8.GetBytes("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + Environment.NewLine + "<TestLevelTwo><SampleString>" + sampleString); byte[] bom = { 0xef, 0xbb, 0xbf }; var inputEnd = Encoding.UTF8.GetBytes("</SampleString></TestLevelTwo>"); var expectedBytes = new byte[sampleString.Length + bom.Length]; var contentBytes = new byte[inputStart.Length + bom.Length + inputEnd.Length]; Buffer.BlockCopy(inputStart, 0, contentBytes, 0, inputStart.Length); Buffer.BlockCopy(bom, 0, contentBytes, inputStart.Length, bom.Length); Buffer.BlockCopy(inputEnd, 0, contentBytes, inputStart.Length + bom.Length, inputEnd.Length); var formatter = new XmlSerializerInputFormatter(); var context = GetInputFormatterContext(contentBytes, typeof(TestLevelTwo)); // Act await formatter.ReadAsync(context); // Assert Assert.NotNull(context.Model); var model = context.Model as TestLevelTwo; Buffer.BlockCopy(sampleStringBytes, 0, expectedBytes, 0, sampleStringBytes.Length); Buffer.BlockCopy(bom, 0, expectedBytes, sampleStringBytes.Length, bom.Length); Assert.Equal(expectedBytes, Encoding.UTF8.GetBytes(model.SampleString)); }
public async Task ReadAsync_AcceptsUTF16Characters() { // Arrange var expectedInt = 10; var expectedString = "TestString"; var expectedDateTime = XmlConvert.ToString(DateTime.UtcNow, XmlDateTimeSerializationMode.Utc); var input = "<?xml version=\"1.0\" encoding=\"UTF-16\"?>" + "<TestLevelOne><SampleInt>" + expectedInt + "</SampleInt>" + "<sampleString>" + expectedString + "</sampleString>" + "<SampleDate>" + expectedDateTime + "</SampleDate></TestLevelOne>"; var formatter = new XmlSerializerInputFormatter(); var contentBytes = Encoding.Unicode.GetBytes(input); var modelState = new ModelStateDictionary(); var httpContext = GetHttpContext(contentBytes, contentType: "application/xml; charset=utf-16"); var provider = new EmptyModelMetadataProvider(); var metadata = provider.GetMetadataForType(typeof(TestLevelOne)); var context = new InputFormatterContext( httpContext, modelName: string.Empty, modelState: modelState, metadata: metadata); // 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.Equal(XmlConvert.ToDateTime(expectedDateTime, XmlDateTimeSerializationMode.Utc), model.SampleDate); }
public async Task VerifyStreamIsOpenAfterRead() { // Arrange var input = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<DummyClass><SampleInt>10</SampleInt></DummyClass>"; var formatter = new XmlSerializerInputFormatter(); var contentBytes = Encoding.UTF8.GetBytes(input); var context = GetInputFormatterContext(contentBytes, typeof(DummyClass)); // Act await formatter.ReadAsync(context); // Assert Assert.NotNull(context.Model); Assert.True(context.HttpContext.Request.Body.CanRead); }
public async Task XmlSerializerFormatterThrowsWhenReaderQuotasAreChanged() { // Arrange var input = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<TestLevelTwo><SampleString>test</SampleString>" + "<TestOne><SampleInt>10</SampleInt>" + "<sampleString>test</sampleString>" + "<SampleDate>" + XmlConvert.ToString(DateTime.UtcNow, XmlDateTimeSerializationMode.Utc) + "</SampleDate></TestOne></TestLevelTwo>"; var formatter = new XmlSerializerInputFormatter(); formatter.XmlDictionaryReaderQuotas.MaxStringContentLength = 10; var contentBytes = Encoding.UTF8.GetBytes(input); var context = GetInputFormatterContext(contentBytes, typeof(TestLevelTwo)); // Act & Assert await Assert.ThrowsAsync(typeof(InvalidOperationException), async () => await formatter.ReadAsync(context)); }
public async Task XmlSerializerFormatterReadsWhenMaxDepthIsModified() { // Arrange var expectedInt = 10; var input = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<DummyClass><SampleInt>" + expectedInt + "</SampleInt></DummyClass>"; var formatter = new XmlSerializerInputFormatter(); formatter.MaxDepth = 10; var contentBytes = Encoding.UTF8.GetBytes(input); var context = GetInputFormatterContext(contentBytes, typeof(DummyClass)); // Act await formatter.ReadAsync(context); // Assert Assert.NotNull(context.Model); Assert.IsType<DummyClass>(context.Model); var model = context.Model as DummyClass; Assert.Equal(expectedInt, model.SampleInt); }
public async Task ReadAsync_AcceptsUTF16Characters() { // Arrange var expectedInt = 10; var expectedString = "TestString"; var expectedDateTime = XmlConvert.ToString(DateTime.UtcNow, XmlDateTimeSerializationMode.Utc); var input = "<?xml version=\"1.0\" encoding=\"UTF-16\"?>" + "<TestLevelOne><SampleInt>" + expectedInt + "</SampleInt>" + "<sampleString>" + expectedString + "</sampleString>" + "<SampleDate>" + expectedDateTime + "</SampleDate></TestLevelOne>"; var formatter = new XmlSerializerInputFormatter(); var contentBytes = Encodings.UTF16EncodingLittleEndian.GetBytes(input); var modelState = new ModelStateDictionary(); var httpContext = GetHttpContext(contentBytes, contentType: "application/xml; charset=utf-16"); var context = new InputFormatterContext(httpContext, modelState, typeof(TestLevelOne)); // Act var model = await formatter.ReadAsync(context); // Assert Assert.NotNull(model); Assert.IsType<TestLevelOne>(model); var levelOneModel = model as TestLevelOne; Assert.Equal(expectedInt, levelOneModel.SampleInt); Assert.Equal(expectedString, levelOneModel.sampleString); Assert.Equal(XmlConvert.ToDateTime(expectedDateTime, XmlDateTimeSerializationMode.Utc), levelOneModel.SampleDate); }
public async Task ReadAsync_UsesContentTypeCharSet_ToReadStream() { // Arrange var expectedException = TestPlatformHelper.IsMono ? typeof(InvalidOperationException) : typeof(XmlException); var expectedMessage = TestPlatformHelper.IsMono ? "There is an error in XML document." : "The expected encoding 'utf-16LE' does not match the actual encoding 'utf-8'."; var inputBytes = Encodings.UTF8EncodingWithoutBOM.GetBytes("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<DummyClass><SampleInt>1000</SampleInt></DummyClass>"); var formatter = new XmlSerializerInputFormatter(); var modelState = new ModelStateDictionary(); var httpContext = GetHttpContext(inputBytes, contentType: "application/xml; charset=utf-16"); var context = new InputFormatterContext(httpContext, modelState, typeof(TestLevelOne)); // Act and Assert var ex = await Assert.ThrowsAsync(expectedException, () => formatter.ReadAsync(context)); Assert.Equal(expectedMessage, ex.Message); }
public async Task ReadAsync_FallsbackToUTF8_WhenCharSet_NotInContentType() { // Arrange var expectedException = TestPlatformHelper.IsMono ? typeof(InvalidOperationException) : typeof(XmlException); var expectedMessage = TestPlatformHelper.IsMono ? "There is an error in XML document." : "The expected encoding 'utf-8' does not match the actual encoding 'utf-16LE'."; var inpStart = Encodings.UTF16EncodingLittleEndian.GetBytes("<?xml version=\"1.0\" encoding=\"UTF-16\"?>" + "<DummyClass><SampleInt>"); byte[] inp = { 192, 193 }; var inpEnd = Encodings.UTF16EncodingLittleEndian.GetBytes("</SampleInt></DummyClass>"); var contentBytes = new byte[inpStart.Length + inp.Length + inpEnd.Length]; Buffer.BlockCopy(inpStart, 0, contentBytes, 0, inpStart.Length); Buffer.BlockCopy(inp, 0, contentBytes, inpStart.Length, inp.Length); Buffer.BlockCopy(inpEnd, 0, contentBytes, inpStart.Length + inp.Length, inpEnd.Length); var formatter = new XmlSerializerInputFormatter(); var context = GetInputFormatterContext(contentBytes, typeof(TestLevelTwo)); // Act and Assert var ex = await Assert.ThrowsAsync(expectedException, () => formatter.ReadAsync(context)); Assert.Equal(expectedMessage, ex.Message); }
public async Task ReadAsync_ThrowsOnExceededMaxDepth() { if (TestPlatformHelper.IsMono) { // ReaderQuotas are not honored on Mono return; } // Arrange var input = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<TestLevelTwo><SampleString>test</SampleString>" + "<TestOne><SampleInt>10</SampleInt>" + "<sampleString>test</sampleString>" + "<SampleDate>" + XmlConvert.ToString(DateTime.UtcNow, XmlDateTimeSerializationMode.Utc) + "</SampleDate></TestOne></TestLevelTwo>"; var formatter = new XmlSerializerInputFormatter(); formatter.MaxDepth = 1; var contentBytes = Encoding.UTF8.GetBytes(input); var context = GetInputFormatterContext(contentBytes, typeof(TestLevelTwo)); // Act & Assert await Assert.ThrowsAsync(typeof(InvalidOperationException), () => formatter.ReadAsync(context)); }
public async Task XmlSerializerFormatterThrowsOnInvalidCharacters() { // Arrange var inpStart = Encodings.UTF16EncodingLittleEndian.GetBytes("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<DummyClass><SampleInt>"); byte[] inp = { 192, 193 }; var inpEnd = Encodings.UTF16EncodingLittleEndian.GetBytes("</SampleInt></DummyClass>"); var contentBytes = new byte[inpStart.Length + inp.Length + inpEnd.Length]; Buffer.BlockCopy(inpStart, 0, contentBytes, 0, inpStart.Length); Buffer.BlockCopy(inp, 0, contentBytes, inpStart.Length, inp.Length); Buffer.BlockCopy(inpEnd, 0, contentBytes, inpStart.Length + inp.Length, inpEnd.Length); var formatter = new XmlSerializerInputFormatter(); var context = GetInputFormatterContext(contentBytes, typeof(TestLevelTwo)); // Act await Assert.ThrowsAsync(typeof(XmlException), async () => await formatter.ReadAsync(context)); }
public async Task ReadAsync_ReadsComplexTypes() { // Arrange var expectedInt = 10; var expectedString = "TestString"; var expectedDateTime = XmlConvert.ToString(DateTime.UtcNow, XmlDateTimeSerializationMode.Utc); var expectedLevelTwoString = "102"; var input = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<TestLevelTwo><SampleString>" + expectedLevelTwoString + "</SampleString>" + "<TestOne><SampleInt>" + expectedInt + "</SampleInt>" + "<sampleString>" + expectedString + "</sampleString>" + "<SampleDate>" + expectedDateTime + "</SampleDate></TestOne></TestLevelTwo>"; var formatter = new XmlSerializerInputFormatter(); var contentBytes = Encoding.UTF8.GetBytes(input); var context = GetInputFormatterContext(contentBytes, typeof(TestLevelTwo)); // Act var result = await formatter.ReadAsync(context); // Assert Assert.NotNull(result); Assert.False(result.HasError); var model = Assert.IsType<TestLevelTwo>(result.Model); Assert.Equal(expectedLevelTwoString, model.SampleString); Assert.Equal(expectedInt, model.TestOne.SampleInt); Assert.Equal(expectedString, model.TestOne.sampleString); Assert.Equal( XmlConvert.ToDateTime(expectedDateTime, XmlDateTimeSerializationMode.Utc), model.TestOne.SampleDate); }