Exemplo n.º 1
0
        public async Task TestGetOpenGraphImageFromUri()
        {
            Uri testUri = new Uri("http://www.bbc.com/news/technology-32194196");

            using (var httpResource = new HttpResource(testUri, new HttpResourceOptions()
            {
                Timeout = TimeSpan.FromSeconds(30)
            }))
            {
                await httpResource.GiddyUp();

                List <string> acceptable = new List <string>()
                {
                    "://ichef.bbci.co.uk/news/1024/media/images/80755000/jpg/_80755021_163765270.jpg",     //'merica test
                    "://ichef-1.bbci.co.uk/news/1024/media/images/80755000/jpg/_80755021_163765270.jpg",   //'merica test part 2
                    "://ichef-1.bbci.co.uk/news/1024/media/images/82142000/jpg/_82142761_026611869-1.jpg", //'merica test part 3
                    "://ichef.bbci.co.uk/news/1024/media/images/82142000/jpg/_82142761_026611869-1.jpg",   //'merica test part 4
                    "://news.bbcimg.co.uk/media/images/80755000/jpg/_80755021_163765270.jpg"               //Yuro test
                };

                Assert.IsNotNull(httpResource.Image, "Expeced a valid Image Uri");
                var expected = httpResource.Image.ToString();

                var passed = acceptable.Any(x => {
                    var result = expected.EndsWith(x, StringComparison.OrdinalIgnoreCase);
                    return(result);
                });

                Assert.IsTrue(passed, $"HttpResource was unable to find an acceptable image path. Found: \"{expected}\"");
            }
        }
Exemplo n.º 2
0
        public async Task Local_FileLifeCycle()
        {
            var fileName = Guid.NewGuid().ToString() + ".png";
            var fm       = new LocalNetworkFileManager();
            var key      = new FileKey()
            {
                ID = fileName, FileType = FileType.Thumbnail
            };
            string partialPath = String.Join("/", new string[] { VoatSettings.Instance.DestinationPathThumbs, fileName }.ToPathParts());

            Assert.IsFalse(await fm.Exists(key));

            using (var httpRehorse = new HttpResource("https://voat.co/Graphics/voat-goat.png"))
            {
                await httpRehorse.GiddyUp();

                await fm.Upload(key, httpRehorse.Stream);
            }

            Assert.IsTrue(await fm.Exists(key));

            var url = fm.Uri(key, new PathOptions()
            {
                FullyQualified = false, ProvideProtocol = false
            });

            Assert.AreEqual($"/{partialPath}", url, "Condition:1.1");

            fm.Delete(key);

            Assert.IsFalse(await fm.Exists(key));
        }
Exemplo n.º 3
0
        public static async Task <bool> Validate(HttpRequest request)
        {
            //if (VoatSettings.Instance.OutgoingTraffic.Enabled)
            //{
            string privateKey      = VoatSettings.Instance.RecaptchaPrivateKey;
            string encodedResponse = request.Form["g-Recaptcha-Response"];

            using (var httpResource = new HttpResource(
                       new Uri("https://www.google.com/recaptcha/api/siteverify"),
                       null))
            {
                var content = new FormUrlEncodedContent(new[]
                {
                    new KeyValuePair <string, string>("secret", privateKey),
                    new KeyValuePair <string, string>("response", encodedResponse),
                });

                await httpResource.GiddyUp(HttpMethod.Post, content);

                var responseString = await httpResource.Response.Content.ReadAsStringAsync().ConfigureAwait(false);

                var captchaResponse = JsonConvert.DeserializeObject <ReCaptchaResponse>(responseString);

                return(captchaResponse.Success);
            }
            //}
            //else
            //{
            //    return false;
            //}
        }
Exemplo n.º 4
0
        public async Task HttpResource_Get_NoRedirect()
        {
            var httpRehorse = new HttpResource("http://www.microsoft.com/en/us/default.aspx?redir=true");
            await httpRehorse.GiddyUp();

            //var w = x.Response.Headers.AcceptRanges.First();
            var s        = new StreamReader(httpRehorse.Stream);
            var contents = s.ReadToEnd();

            var ss = new MemoryStream();
            await httpRehorse.Response.Content.CopyToAsync(ss);
        }
Exemplo n.º 5
0
        public async Task TestGetTitleFromUri()
        {
            const string testUri = "http://www.google.com";
            string       result  = null;

            using (var httpResource = new HttpResource(testUri))
            {
                await httpResource.GiddyUp();

                result = httpResource.Title;
            }

            Assert.AreEqual("Google", result, "Unable to extract title from given Uri.");
        }
Exemplo n.º 6
0
        public async Task TestEscapedQuotesInTitle()
        {
            Uri testUri = new Uri("https://lwn.net/Articles/653411/");

            string result = null;

            using (var httpResource = new HttpResource(testUri.ToString()))
            {
                await httpResource.GiddyUp();

                result = httpResource.Title;
            }

            Assert.AreEqual("\"Big data\" features coming in PostgreSQL 9.5 [LWN.net]", result, "HTML in title not properly decoded");
        }
Exemplo n.º 7
0
        public async Task TestTagInTitle()
        {
            Uri    testUri = new Uri("http://stackoverflow.com/questions/1348683/will-the-b-and-i-tags-ever-become-deprecated");
            string result  = null;

            using (var httpResource = new HttpResource(testUri.ToString(), new HttpResourceOptions()
            {
                AllowAutoRedirect = true
            }))
            {
                await httpResource.GiddyUp();

                result = httpResource.Title;
                Assert.AreEqual(true, httpResource.Redirected);
                Assert.AreEqual("Will the <b> and <i> tags ever become deprecated?", result, "HTML in title not properly decoded");
            }
        }
Exemplo n.º 8
0
        public async Task ContentDelivery_FileLifeCycle()
        {
            string fmType = "AzureBlob";

            var handler = FileManagerConfigurationSettings.Instance.Handlers.FirstOrDefault(x => x.Name.IsEqual(fmType));

            if (handler == null)
            {
                Assert.Inconclusive($"Can't find {fmType}");
            }
            var fm = handler.Construct <AzureBlobFileManager>();

            if (fm == null)
            {
                Assert.Inconclusive($"Can't construct {fmType}");
            }
            var fileName = Guid.NewGuid().ToString() + ".png";
            var key      = new FileKey()
            {
                ID = fileName, FileType = FileType.Thumbnail
            };

            //string partialPath = String.Join("/", new string[] { VoatSettings.Instance.DestinationPathThumbs, fileName }.ToPathParts());

            Assert.IsFalse(await fm.Exists(key));

            using (var httpRehorse = new HttpResource("https://voat.co/Graphics/voat-goat.png"))
            {
                await httpRehorse.GiddyUp();

                await fm.Upload(key, httpRehorse.Stream);
            }

            Assert.IsTrue(await fm.Exists(key));

            var url = fm.Uri(key, new PathOptions()
            {
                FullyQualified = true, ProvideProtocol = true
            });
            //Assert.AreEqual($"/{partialPath}", url, "Condition:1.1");

            await fm.Delete(key);

            Assert.IsFalse(await fm.Exists(key));
        }
        public async Task <JsonResult> TitleFromUri()
        {
            if (VoatSettings.Instance.OutgoingTraffic.Enabled)
            {
                var uri = Request.Query["uri"].FirstOrDefault();
                uri = uri.TrimSafe();

                if (!string.IsNullOrEmpty(uri) && UrlUtility.IsUriValid(uri, true, true))
                {
                    //Old Code:
                    //string title = UrlUtility.GetTitleFromUri(uri);
                    using (var httpResource = new HttpResource(
                               new Uri(uri),
                               new HttpResourceOptions()
                    {
                        AllowAutoRedirect = true
                    },
                               VoatSettings.Instance.OutgoingTraffic.Proxy.ToWebProxy()))
                    {
                        await httpResource.GiddyUp();

                        string title = httpResource.Title;

                        if (title != null)
                        {
                            title = title.StripUnicode();
                            var resultList = new List <string>
                            {
                                title
                            };

                            return(Json(resultList /* CORE_PORT: Removed , JsonRequestBehavior.AllowGet */));
                        }
                    }
                }
            }
            Response.StatusCode = (int)HttpStatusCode.BadRequest;
            return(Json("Bad request." /* CORE_PORT: Removed , JsonRequestBehavior.AllowGet */));
        }
Exemplo n.º 10
0
        public async Task TestRedirect()
        {
            Uri testUri = new Uri("http://stackoverflow.com/questions/1348683/will-the-b-and-i-tags-ever-become-deprecated");

            using (var httpResource = new HttpResource(testUri.ToString()))
            {
                await httpResource.GiddyUp();

                Assert.AreEqual(false, httpResource.Redirected);
                Assert.AreEqual(HttpStatusCode.MovedPermanently, httpResource.Response.StatusCode);
            }

            using (var httpResource = new HttpResource(testUri.ToString(), new HttpResourceOptions()
            {
                AllowAutoRedirect = true
            }))
            {
                await httpResource.GiddyUp();

                Assert.AreEqual(true, httpResource.Redirected);
                Assert.AreEqual(HttpStatusCode.OK, httpResource.Response.StatusCode);
            }
        }