public void XmlResolverAddWithValidData() { var xmlResolver = new XmlPreloadedResolver(XmlKnownDtds.Xhtml10); byte[] data = "hello world" u8.ToArray(); MemoryStream stream = new MemoryStream(data); xmlResolver.Add(new Uri("-//Sample//URI//For Testing", UriKind.RelativeOrAbsolute), stream); Stream result = xmlResolver.GetEntity(new Uri("-//Sample//URI//For Testing", UriKind.RelativeOrAbsolute), null, typeof(Stream)) as Stream; Assert.NotNull(result); byte[] output = new byte[data.Length]; result.Read(output, 0, output.Length); Assert.Equal(data, output); DummyStream dummyStream = new DummyStream(data); xmlResolver.Add(new Uri("-//W3C//DTD XHTML 1.0 Strict//EN", UriKind.RelativeOrAbsolute), dummyStream); Stream otherResult = xmlResolver.GetEntity(new Uri("-//W3C//DTD XHTML 1.0 Strict//EN", UriKind.RelativeOrAbsolute), null, typeof(Stream)) as Stream; output = new byte[data.Length]; otherResult.Read(output, 0, output.Length); Assert.Equal(data, output); }
private static void VerifyResource(byte[] expected, XmlPreloadedResolver xmlResolver, Uri uri) { using (var resource = (Stream)xmlResolver.GetEntity(uri, null, typeof(Stream))) { EnumerableAssert.AreEqual(expected, resource.ReadToEnd(), "The URI is not mapped to the expected resource in the XML resolver."); } }
/// <summary> /// Extends GetEntity so that methods that return a specific type object given a Type parameter can be /// used as generic method and casting is not required. /// <example> /// xmlpreloadedresolver.GetEntity<int>(absoluteUri, role); /// </example> /// </summary> public static T GetEntity <T>(this XmlPreloadedResolver xmlpreloadedresolver, Uri absoluteUri, String role) { if (xmlpreloadedresolver == null) { throw new ArgumentNullException("xmlpreloadedresolver"); } return((T)xmlpreloadedresolver.GetEntity(absoluteUri, role, typeof(T))); }
public void XmlResolverGetEntityWithValidUserSuppliedData() { var uri = new Uri("-//W3C//DTD FAKE 1.0 Not Real//EN", UriKind.RelativeOrAbsolute); XmlPreloadedResolver xmlResolver = GetResolverWithStringData(XmlKnownDtds.Xhtml10, "Sample String Data", uri); Stream streamResult = xmlResolver.GetEntity(uri, null, null) as Stream; Assert.NotNull(streamResult); byte[] data = new byte[streamResult.Length]; streamResult.Read(data, 0, Convert.ToInt32(streamResult.Length)); Assert.Equal("Sample String Data", NormalizeContent(Encoding.ASCII.GetString(data))); uri = new Uri("-//W3C//DTD FAKE 1.0 Not Real//EN", UriKind.RelativeOrAbsolute); xmlResolver = GetResolverWithStringData(XmlKnownDtds.Xhtml10, "Sample String Data", uri); TextReader textResult = xmlResolver.GetEntity(uri, null, typeof(TextReader)) as TextReader; Assert.NotNull(textResult); Assert.Equal("Sample String Data", textResult.ReadLine()); }
public void XmlResolverGetKnownEntity() { var xmlResolver = new XmlPreloadedResolver(XmlKnownDtds.All); foreach (var dtdInfo in GetKnownDtds()) { string expectedContent = NormalizeContent(File.ReadAllText(dtdInfo.SourcePath)); var uri = new Uri(dtdInfo.PublicId, UriKind.RelativeOrAbsolute); Stream streamResult = xmlResolver.GetEntity(uri, null, null) as Stream; Assert.NotNull(streamResult); byte[] data = new byte[streamResult.Length]; streamResult.Read(data, 0, Convert.ToInt32(streamResult.Length)); Assert.Equal(expectedContent, NormalizeContent(Encoding.ASCII.GetString(data))); uri = new Uri(dtdInfo.SystemId, UriKind.RelativeOrAbsolute); streamResult = xmlResolver.GetEntity(uri, null, null) as Stream; Assert.NotNull(streamResult); data = new byte[streamResult.Length]; streamResult.Read(data, 0, Convert.ToInt32(streamResult.Length)); Assert.Equal(expectedContent, NormalizeContent(Encoding.ASCII.GetString(data))); } }
public void XmlResolverGetEntityWithInvalidData() { var xmlResolver = new XmlPreloadedResolver(XmlKnownDtds.Xhtml10); Assert.Throws <XmlException>(() => xmlResolver.GetEntity(new Uri("https://JustAUri"), null, typeof(string))); xmlResolver = new XmlPreloadedResolver(new XmlPreloadedResolver(), XmlKnownDtds.Xhtml10); Assert.Throws <XmlException>(() => xmlResolver.GetEntity(new Uri("https://JustAUri"), null, typeof(string))); xmlResolver = new XmlPreloadedResolver(XmlKnownDtds.Xhtml10); Assert.Throws <XmlException>(() => xmlResolver.GetEntity(new Uri("-//W3C//DTD XHTML 1.0 Transitional//EN", UriKind.RelativeOrAbsolute) , null, typeof(string))); xmlResolver = new XmlPreloadedResolver(new XmlPreloadedResolver(), XmlKnownDtds.Xhtml10); Assert.Throws <XmlException>(() => xmlResolver.GetEntity(new Uri("-//W3C//Invalid URI ", UriKind.RelativeOrAbsolute) , null, typeof(string))); }
public void XmlResolverGetEntityWithNullUri() { var xmlResolver = new XmlPreloadedResolver(XmlKnownDtds.None); Assert.Throws <ArgumentNullException>(() => xmlResolver.GetEntity(null, null, null)); }