Example #1
0
 public ShortenedUrlController(
     UrlShortenerDbContext dbContext,
     UrlShorteningService urlShorteningService)
 {
     _dbContext            = dbContext;
     _urlShorteningService = urlShorteningService;
 }
Example #2
0
        public async void GetLongUrl_CacheNoneCacheTest()
        {
            var mockRepo   = new Mock <IUrlRepository>();
            var mockLogger = new Mock <ILogger <UrlShorteningService> >();
            var mockCache  = new Mock <ICacheService>();
            var service    = new UrlShorteningService(_settings, mockRepo.Object, mockCache.Object, mockLogger.Object);

            var shortUrlCode = "aBcDeF";
            var pk           = "aBc";
            var longUrl      = "https://example.com";

            mockCache.SetupSequence(c => c.GetCache(shortUrlCode))
            .Returns(longUrl)
            .Returns(string.Empty);

            // get value from cache
            Assert.Equal(longUrl, service.GetLongUrl(shortUrlCode).Result.Data);

            // check if cache set and repo called
            mockRepo.Setup(r => r.GetRedirectOptimizedUrl(pk, shortUrlCode))
            .Returns(Task.FromResult(new Url(pk, shortUrlCode, longUrl)));
            await service.GetLongUrl(shortUrlCode);

            mockRepo.Verify(v => v.GetRedirectOptimizedUrl(pk, shortUrlCode), Times.Once);
            mockCache.Verify(c => c.SetCache(shortUrlCode, longUrl), Times.Once);
        }
Example #3
0
        public async void CreateUrlTest_WhenEntryAlreadyExistsForSameLongUrl(string longUrl, string shortUrlCode, string userId)
        {
            var mockRepo   = new Mock <IUrlRepository>();
            var mockLogger = new Mock <ILogger <UrlShorteningService> >();

            mockRepo.Setup(r => r.GetRedirectOptimizedUrl(It.IsAny <string>(), It.IsAny <string>()))
            .Returns(Task.FromResult(new Url("", "", longUrl)));

            var service = new UrlShorteningService(_settings, mockRepo.Object, new Mock <ICacheService>().Object, mockLogger.Object);
            var result  = await service.CreateUrl(longUrl, userId);

            Assert.Equal(shortUrlCode, result?.Data);
        }
Example #4
0
        public void GetLongUrl_CacheThrowException()
        {
            var mockRepo   = new Mock <IUrlRepository>();
            var mockLogger = new Mock <ILogger <UrlShorteningService> >();
            var mockCache  = new Mock <ICacheService>();
            var service    = new UrlShorteningService(_settings, mockRepo.Object, mockCache.Object, mockLogger.Object);

            var    shortUrlCode = "aBcDeF";
            string cacheEx      = "Cache Exception";

            mockCache.Setup(c => c.GetCache(shortUrlCode))
            .Throws(new Exception(cacheEx));
            Assert.Equal(cacheEx, service.GetLongUrl(shortUrlCode).Result.Error);
        }
Example #5
0
        public async void CreateUrlTest_ThrowsException(string longUrl, string shortUrlCode, string userId)
        {
            string exMsg      = "Test Exception";
            var    mockRepo   = new Mock <IUrlRepository>();
            var    mockLogger = new Mock <ILogger <UrlShorteningService> >();

            mockRepo.Setup(r => r.GetRedirectOptimizedUrl(It.IsAny <string>(), It.IsAny <string>()))
            .Throws(new Exception(exMsg));

            var service = new UrlShorteningService(_settings, mockRepo.Object, new Mock <ICacheService>().Object, mockLogger.Object);
            var result  = await service.CreateUrl(longUrl, userId);

            Assert.Equal(exMsg, result?.Error);
        }
        public Link CreateLink(string originUrl)
        {
            var link = new Link()
            {
                OriginUrl = originUrl
            };

            link.ShortсutUrl = UrlShorteningService.GetShortenUrl(originUrl);

            Context.Links.Add(link);

            Context.SaveChanges();

            return(link);
        }
Example #7
0
        public void GetLongUrl_RepoThrowException()
        {
            var mockRepo   = new Mock <IUrlRepository>();
            var mockLogger = new Mock <ILogger <UrlShorteningService> >();
            var mockCache  = new Mock <ICacheService>();
            var service    = new UrlShorteningService(_settings, mockRepo.Object, mockCache.Object, mockLogger.Object);

            var shortUrlCode = "aBcDeF";
            var pk           = "aBc";

            string repoEx = "Repo Exception";

            mockRepo.Setup(r => r.GetRedirectOptimizedUrl(pk, shortUrlCode))
            .Throws(new Exception(repoEx));
            Assert.Equal(repoEx, service.GetLongUrl(shortUrlCode).Result.Error);
        }
Example #8
0
        public async void CreateUrlTest_WhenEntryAlreadyExistsForAnotherLongUrl_AndAllReTriesFail(string longUrl, string errorMessage, string userId)
        {
            var mockRepo   = new Mock <IUrlRepository>();
            var mockLogger = new Mock <ILogger <UrlShorteningService> >();

            mockRepo.SetupSequence(r => r.GetRedirectOptimizedUrl(It.IsAny <string>(), It.IsAny <string>()))
            .Returns(Task.FromResult(new Url("", "", Guid.NewGuid().ToString())))
            .Returns(Task.FromResult(new Url("", "", Guid.NewGuid().ToString())))
            .Returns(Task.FromResult(new Url("", "", Guid.NewGuid().ToString())))
            .Returns(Task.FromResult(new Url("", "", Guid.NewGuid().ToString())))
            .Returns(Task.FromResult(new Url("", "", Guid.NewGuid().ToString())))
            .Returns(Task.FromResult(new Url("", "", Guid.NewGuid().ToString())))
            .Returns(Task.FromResult(new Url("", "", Guid.NewGuid().ToString())))
            .Returns(Task.FromResult(new Url("", "", Guid.NewGuid().ToString())));

            var service = new UrlShorteningService(_settings, mockRepo.Object, new Mock <ICacheService>().Object, mockLogger.Object);
            var result  = await service.CreateUrl(longUrl, userId);

            Assert.Equal(false, result?.Success);
            Assert.Equal(errorMessage, result?.Error);
        }
Example #9
0
 public void Setup()
 {
     shortnerDbContext    = new ShortnerDbContext(new DbContextOptions <ShortnerDbContext>());
     shorteningRepository = new ShorteningRepository(shortnerDbContext);
     urlShorteningService = new UrlShorteningService(shorteningRepository);
 }
        private void ParseTextHereAndTinyUpAnyURLsFound(ref string tweetText)
        {
            //parse the text here and tiny up any URLs found.
            ShorteningService service;
            if (!string.IsNullOrEmpty(AppSettings.UrlShorteningService))
                service = (ShorteningService)Enum.Parse(typeof(ShorteningService), AppSettings.UrlShorteningService);
            else
                service = ShorteningService.isgd;

            UrlShorteningService urlHelper = new UrlShorteningService(service);
            tweetText = urlHelper.ShrinkUrls(tweetText);
        }
Example #11
0
        private static void OnTweetTextChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            string text = args.NewValue as string;

            if (!string.IsNullOrEmpty(text))
            {
                TweetTextBlock textblock = (TweetTextBlock)obj;
                textblock.Inlines.Clear();
                textblock.Inlines.Add(" ");

                string[] words = Regex.Split(text, @"([ \(\)\{\}\[\]])");

                string possibleUserName = words[0].ToString();

                if ((possibleUserName.Length > 1) && (possibleUserName.Substring(1, 1) == "@"))
                {
                    textblock = FormatName(textblock, possibleUserName);
                    words.SetValue("", 0);
                }

                foreach (string word in words)
                {
                    // clickable hyperlinks
                    if (UrlShorteningService.IsUrl(word))
                    {
                        try
                        {
                            Hyperlink link = new Hyperlink();
                            link.NavigateUri = new Uri(word);
                            link.Inlines.Add(word);
                            link.Click  += new RoutedEventHandler(link_Click);
                            link.ToolTip = "Open link in the default browser";
                            textblock.Inlines.Add(link);
                        }
                        catch
                        {
                            //TODO:What are we catching here? Why? Log it?
                            textblock.Inlines.Add(word);
                        }
                    }
                    // clickable @name
                    else if (word.StartsWith("@"))
                    {
                        textblock = FormatName(textblock, word);
                    }

                    // clickable #hashtag
                    else if (word.StartsWith("#"))
                    {
                        string hashtag      = String.Empty;
                        Match  foundHashtag = Regex.Match(word, @"#(\w+)(?<suffix>.*)");
                        if (foundHashtag.Success)
                        {
                            hashtag = foundHashtag.Groups[1].Captures[0].Value;
                            Hyperlink tag = new Hyperlink();
                            tag.Inlines.Add(hashtag);

                            string hashtagUrl = "http://search.twitter.com/search?q=%23{0}";

                            // The main application has access to the Settings class, where a
                            // user-defined hashtagUrl can be stored.  This hardcoded one that
                            // is used to set the NavigateUri is just a default behavior that
                            // will be used if the click event is not handled for some reason.

                            tag.NavigateUri = new Uri(String.Format(hashtagUrl, hashtag));
                            tag.ToolTip     = "Show statuses that include this hashtag";
                            tag.Tag         = hashtag;

                            tag.Click += new RoutedEventHandler(hashtag_Click);

                            textblock.Inlines.Add("#");
                            textblock.Inlines.Add(tag);
                            textblock.Inlines.Add(foundHashtag.Groups["suffix"].Captures[0].Value);
                        }
                    }
                    else
                    {
                        textblock.Inlines.Add(word);
                    }
                }

                textblock.Inlines.Add(" ");
            }
        }
 public void Addresses_that_have_commas_should_be_urls()
 {
     Assert.IsTrue(UrlShorteningService.IsUrl("http://www.foo.com/x,123"));
 }
 public void Addresses_that_dont_end_in_a_filename_extension_should_be_urls()
 {
     Assert.IsTrue(UrlShorteningService.IsUrl("http://www.foo.com/x"));
 }
 public void Web_site_root_addresses_should_be_urls()
 {
     Assert.IsTrue(UrlShorteningService.IsUrl("http://www.foo.com/"));
 }
 public void Things_that_start_with_feed_should_not_be_urls()
 {
     Assert.IsFalse(UrlShorteningService.IsUrl("feed://www.foo.com/blah/blah.aspx"));
 }
 public void Things_that_start_with_https_should_not_be_urls()
 {
     Assert.IsTrue(UrlShorteningService.IsUrl("https://www.foo.com/blah/blah.aspx"));
 }