コード例 #1
0
        public async Task XmlResolverGetKnownEntityAsync()
        {
            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 = await xmlResolver.GetEntityAsync(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 = await xmlResolver.GetEntityAsync(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)));
            }
        }
コード例 #2
0
        public void XmlResolverGetEntityAsyncWithInvalidData()
        {
            var xmlResolver = new XmlPreloadedResolver(XmlKnownDtds.Xhtml10);

            Assert.ThrowsAsync <ArgumentNullException>(() => xmlResolver.GetEntityAsync(null, null, null));
            Assert.ThrowsAsync <XmlException>(() => xmlResolver.GetEntityAsync(new Uri("https://DummyUri"), null, null));
            Assert.ThrowsAsync <XmlException>(() =>
                                              xmlResolver.GetEntityAsync(new Uri("-//W3C//ENTITIES Latin 1 for XHTML//EN", UriKind.RelativeOrAbsolute), null, typeof(string)));

            xmlResolver = new XmlPreloadedResolver(new XmlPreloadedResolver(), XmlKnownDtds.Xhtml10);
            Assert.ThrowsAsync <XmlException>(() =>
                                              xmlResolver.GetEntityAsync(new Uri("https://DummyUri", UriKind.RelativeOrAbsolute), null, typeof(string)));

            Assert.ThrowsAsync <XmlException>(() =>
                                              xmlResolver.GetEntityAsync(new Uri("-//W3C//ENTITIES Latin 1 for XHTML//EN", UriKind.RelativeOrAbsolute), null, typeof(TextReader)));
        }
コード例 #3
0
        public void XmlResolverGetEntityAsyncWithValidUserSuppliedData()
        {
            byte[] inpData     = Encoding.ASCII.GetBytes("hello world");
            var    xmlResolver = new XmlPreloadedResolver(XmlKnownDtds.Xhtml10);

            xmlResolver.Add(new Uri("-//W3C//DTD FAKE 1.0 Not Real//EN", UriKind.RelativeOrAbsolute), inpData);
            Task <object> output = xmlResolver.GetEntityAsync(new Uri("-//W3C//DTD FAKE 1.0 Not Real//EN",
                                                                      UriKind.RelativeOrAbsolute), null, typeof(Stream));
            var result = new byte[inpData.Length];

            (output.Result as Stream).Read(result, 0, result.Length);
            Assert.Equal(inpData, result);
        }