Beispiel #1
0
        public void SaveUrl()
        {
            var url = "http://someurl.com";

            HomeController.UrlShorterRequest urlEnt = new HomeController.UrlShorterRequest {
                url = url
            };
            var entity = new ShortedUrlEntity {
                Id = 5, Url = url, ShortUrlSuffix = "234NJI", NavigationsCount = 11, CreateDate = DateTime.Now
            };
            var mock = new Mock <IShortUrlsRepository>();

            mock.Setup(repo => repo.Create(It.IsAny <ShortedUrlEntity>()))
            .Callback((ShortedUrlEntity e) => {
                urls.Add(e);
            });
            mock.Setup(repo => repo.Save()).Callback(() => {}).Verifiable();;

            HomeController controller = new HomeController(mock.Object);
            var            result     = controller.ShortenUrl(urlEnt) as JsonResult;

            Assert.NotNull(result);
            System.Console.WriteLine($"Urls count {urls.Count}");
            Assert.True(urls.Any((e) => { return(e.Url == url); }));
        }
        public IActionResult ShortenUrl([FromBody] UrlShorterRequest request)
        {
            if (request.url == null || request.url == string.Empty)
            {
                return(new BadRequestObjectResult(URL_ERROR_EMPTY));
            }

            //TODO: check URL with regexp /http(s)?://[A-Za-z0-9\.]+\.[A-Za-z0-9]+.*/
            if (!masks.Any(s => request.url.StartsWith(s, StringComparison.OrdinalIgnoreCase)))
            {
                return(new BadRequestObjectResult(URL_ERROR_PROTOCOL_REQ));
            }

            var randString = "";

            do
            {
                randString = RandomString();
            } while(repo.getAll().Any(s => s.ShortUrlSuffix.Equals(s)));

            ShortedUrlEntity shortenUrl = new ShortedUrlEntity()
            {
                Url            = request.url,
                ShortUrlSuffix = randString,
                CreateDate     = DateTime.Now
            };

            try {
                repo.Create(shortenUrl);
                repo.Save();
            }
            catch (Exception)
            {
                return(new StatusCodeResult(500));
            }
            System.Console.WriteLine($">>>Tiny Url successfuly created { request.url }");
            return(Json(shortenUrl));
        }
Beispiel #3
0
 private void SaveUrl(ShortedUrlEntity entity)
 {
     urls.Add(entity);
 }