private void TestSuccessfulCase(PageHtmlDownloader downloader, string address, string resultUriExpected = null)
        {
            if (resultUriExpected == null)
            {
                resultUriExpected = address;
            }
            if (resultUriExpected.EndsWith("/"))
            {
                resultUriExpected = resultUriExpected.Substring(0, resultUriExpected.Length - 1);
            }

            var result = downloader.Download(address).Result;

            Assert.IsNotNull(result);
            Assert.IsTrue(!string.IsNullOrEmpty(result.Source));

            string resultUri = result.ResultUri.ToString();

            if (resultUri.EndsWith("/"))
            {
                resultUri = resultUri.Substring(0, resultUri.Length - 1);
            }

            Assert.AreEqual(resultUriExpected, resultUri);
        }
        public void HtmlIsNotDownloaded_WhenSpecifiedInvalidUri()
        {
            PageHtmlDownloader d = new PageHtmlDownloader();

            TestUnsuccessfulCase(d, "github_not_exists.com");
            TestUnsuccessfulCase(d, "bing_not_exists.com");
        }
        public void HtmlIsDownloaded_WhenSpecifiedCompleteUri()
        {
            PageHtmlDownloader d = new PageHtmlDownloader();

            TestSuccessfulCase(d, "https://www.github.com");
            TestSuccessfulCase(d, "https://www.bing.com");
        }
Пример #4
0
        public async Task <object> Post(PageInfoRequest request)
        {
            string address = request.Address;

            PageHtmlDownloader.Result result;
            string source;

            PageHtmlDownloader downloader = new PageHtmlDownloader();

            try
            {
                result = await downloader.Download(address);

                if (result == null)
                {
                    return(new { Error = "PageNotAvailable" });
                }
                else
                {
                    source = result.Source;
                }
            }
            catch (UriFormatException)
            {
                return(new { Error = "InvalidAddress" });
            }

            string title = Regex.Match(source, @"\<title\b[^>]*\>\s*(?<Title>[\s\S]*?)\</title\>", RegexOptions.IgnoreCase).Groups["Title"].Value;

            FaviconLoader loader = new FaviconLoader();

            FaviconLoader.Result favicon = await loader.Load(result.ResultUri, source);

            return(new { Error = "", Title = title, FaviconUrl = favicon?.FaviconUrl, FaviconData = favicon?.Data, FaviconMime = favicon?.MimeType, ResultUrl = result.ResultUri.ToString().TrimEnd('/') });
        }
        public void HtmlIsDownloaded_WhenSpecifiedIncompleteUri()
        {
            PageHtmlDownloader d = new PageHtmlDownloader();

            TestSuccessfulCase(d, "github.com", "https://github.com");
            TestSuccessfulCase(d, "bing.com", "https://bing.com");

            // HTTPS can incorrect certificate, so it fails for https://
            TestSuccessfulCase(d, "clasevirtual.ru/index/peliculas/0-4", "http://clasevirtual.ru/index/peliculas/0-4");
        }
        private void TestUnsuccessfulCase(PageHtmlDownloader downloader, string address)
        {
            var result = downloader.Download(address).Result;

            Assert.IsNull(result);
        }