Ejemplo n.º 1
0
        public ActionResult Clicks(string clickedURL)
        {
            ShortenURL shortenURL = new ShortenURL();
            ulong      clicks     = shortenURL.ClicksOnShortURL(clickedURL);

            return(RedirectToAction("Index", new { message = "Your short URL has been clicked " + clicks + " times." }));
        }
Ejemplo n.º 2
0
        public ActionResult Expand(string shortURL)
        {
            ShortenURL shortenURL  = new ShortenURL();
            string     expandedURL = shortenURL.Expand(shortURL);

            return(RedirectToAction("Index", new { message = "Your expanded URL is: " + expandedURL }));
        }
Ejemplo n.º 3
0
        public ActionResult Shorten(string url)
        {
            ShortenURL shortenURL   = new ShortenURL();
            string     shortenedURL = shortenURL.Shorten(url);

            return(RedirectToAction("Index", new { message = "Your shortened URL is: " + shortenedURL }));
        }
Ejemplo n.º 4
0
        public void ClicksOnShortURL_ZeroForNotShortenedURL()
        {
            ShortenURL shortenURL = new ShortenURL();
            string     shortURL   = shortenURL.FormatHashAsURL("somehash");

            ulong clicks = shortenURL.ClicksOnShortURL(shortURL);

            Assert.AreEqual((ulong)0, clicks);
        }
Ejemplo n.º 5
0
        public void ParseHashFromURL_NonURLReturnsEmptyString()
        {
            ShortenURL shortenURL = new ShortenURL();
            string     notURL     = "this_is_not_a_url_from_FormatHashAsURL";

            string hash = shortenURL.ParseHashFromURL(notURL);

            Assert.AreEqual(string.Empty, hash);
        }
Ejemplo n.º 6
0
        public void Expand_ReturnsEmptyStringForNotShortenedURL()
        {
            ShortenURL shortenURL = new ShortenURL();
            string     shortURL   = shortenURL.FormatHashAsURL("somehash");

            string expandedURL = shortenURL.Expand(shortURL);

            Assert.AreEqual(string.Empty, expandedURL);
        }
Ejemplo n.º 7
0
        public void FormatHashAsURL_ParseHashFromURL_UndoEachOther()
        {
            ShortenURL shortenURL = new ShortenURL();
            string     hash       = "abceasyas123";

            string url = shortenURL.FormatHashAsURL(hash);

            Assert.AreEqual(hash, shortenURL.ParseHashFromURL(url));
        }
Ejemplo n.º 8
0
        public void ClicksOnShortURL_ZeroClicksOnNewShortURL()
        {
            ShortenURL shortenURL = new ShortenURL();
            string     url        = "http://www.mlb.com/2020-world-series";

            string shortURL = shortenURL.Shorten(url);
            ulong  clicks   = shortenURL.ClicksOnShortURL(shortURL);

            Assert.AreEqual((ulong)0, clicks);
        }
Ejemplo n.º 9
0
        public void Expand_ShortenedURLReturnsFullURL()
        {
            ShortenURL shortenURL = new ShortenURL();
            string     url        = "http://www.mlb.com/2020-world-series";

            string shortURL = shortenURL.Shorten(url);

            string expandedURL = shortenURL.Expand(shortURL);

            Assert.AreEqual(url, expandedURL);
        }
Ejemplo n.º 10
0
        public void Shorten_FirstHashIsNotEmpty()
        {
            ShortenURL shortenURL = new ShortenURL();
            string     url        = "http://www.mlb.com/2020-world-series";

            string shortURL = shortenURL.Shorten(url);

            string shortURLHash = shortenURL.ParseHashFromURL(shortURL);

            Assert.AreNotEqual(string.Empty, shortURLHash);
        }
Ejemplo n.º 11
0
        public void ClicksOnShortURL_ExpandIncrementsClicks()
        {
            ShortenURL shortenURL = new ShortenURL();
            string     url        = "http://www.mlb.com/2020-world-series";

            string shortURL = shortenURL.Shorten(url);

            shortenURL.Expand(shortURL);
            ulong clicks = shortenURL.ClicksOnShortURL(shortURL);

            Assert.AreEqual((ulong)1, clicks);
        }
Ejemplo n.º 12
0
        public void Shorten_SameURLHasSameHash()
        {
            ShortenURL shortenURL = new ShortenURL();
            string     url        = "http://www.mlb.com/2020-world-series";

            string shortURL  = shortenURL.Shorten(url);
            string shortURL2 = shortenURL.Shorten(url);

            string shortURLHash  = shortenURL.ParseHashFromURL(shortURL);
            string shortURLHash2 = shortenURL.ParseHashFromURL(shortURL2);

            Assert.AreEqual(shortURLHash, shortURLHash2);
        }
Ejemplo n.º 13
0
        public void Shorten_FirstTwoHashesAreDifferent()
        {
            ShortenURL shortenURL = new ShortenURL();
            string     url        = "http://www.mlb.com/2020-world-series";

            string shortURL  = shortenURL.Shorten(url);
            string shortURL2 = shortenURL.Shorten(url + url);

            string shortURLHash  = shortenURL.ParseHashFromURL(shortURL);
            string shortURLHash2 = shortenURL.ParseHashFromURL(shortURL2);

            Assert.AreNotEqual(shortURLHash, shortURLHash2);
        }
Ejemplo n.º 14
0
        public void ClicksOnShortURL_ExpandOnlyIncrementsOneURL()
        {
            ShortenURL shortenURL = new ShortenURL();
            string     url        = "http://www.mlb.com/2020-world-series";
            string     url2       = "http://www.mlb.com/2020-world-series?team1=cubs&team2=rsox";


            string shortURL  = shortenURL.Shorten(url);
            string shortURL2 = shortenURL.Shorten(url2);

            shortenURL.Expand(shortURL2);

            ulong clicks  = shortenURL.ClicksOnShortURL(shortURL);
            ulong clicks2 = shortenURL.ClicksOnShortURL(shortURL2);

            Assert.AreEqual((ulong)0, clicks);
            Assert.AreEqual((ulong)1, clicks2);
        }
Ejemplo n.º 15
0
        public ResponseHeader ShortenUrl([FromBody] string originalUrl)
        {
            ResponseHeader response = new ResponseHeader();

            // Check whether the url is valid or not.
            // If the url is invalid, return the failure message.
            // Else return the shorten url
            bool validUrl = CheckURLValid(originalUrl);

            if (validUrl == false)
            {
                response.ShortUrl           = null;
                response.OriginalUrl        = originalUrl;
                response.Errors.OriginalUrl = "The OriginalUrl field is not a valid fully-qualified http,   https,  or ftp URL";
                return(response);
            }

            // Check that orginalUrl is not already in the database
            string shorten = AzureTable.GetShorten(originalUrl);

            if (shorten != null)
            {
                response.ShortUrl    = shorten;
                response.OriginalUrl = originalUrl;
                response.Errors      = null;

                return(response);
            }

            // Create a shorten for this Url -> saved in the database
            // Fill the response Header
            shorten              = ShortenURL.Create(originalUrl);
            response.ShortUrl    = shorten;
            response.OriginalUrl = originalUrl;
            response.Errors      = null;

            return(response);
        }