public void post_should_return_not_found_if_input_short_url_is_not_registered()
        {
            var shortenUrl = new ShortenUrlInputModel()
            {
                Url = "https://shortenurl.com/85249ecd-943c-4e53-8a03-d5a412b1d09b"
            };

            this.shortenUrlService.Setup(x => x.AlterUrl(It.IsAny <ShortenUrlInputModel>())).Returns(string.Empty);
            var result = sut.Post(shortenUrl) as NotFoundResult;

            result.StatusCode.Should().Be(404);
        }
        public void post_should_return_not_found_if_input_url_is_invalid()
        {
            var shortenUrl = new ShortenUrlInputModel()
            {
                Url = "https://shortenurl.com/85249ecd-943c-4e53-8a03-d5a412b1d09b"
            };

            this.shortenUrlService.Setup(x => x.AlterUrl(It.IsAny <ShortenUrlInputModel>())).Throws(new Exception("Url is invalid"));
            var result = sut.Post(shortenUrl) as BadRequestObjectResult;

            result.StatusCode.Should().Be(400);
            result.Value.Should().Be("Url is invalid");
        }
        public void post_should_return_long_url_if_input_url_is_valid_short_url()
        {
            var alterUrl   = "https://www.linkedin.com/in/ehasanulhoque/";
            var shortenUrl = new ShortenUrlInputModel()
            {
                Url = "https://shortenurl.com/85249ecd-943c-4e53-8a03-d5a412b1d09b"
            };

            this.shortenUrlService.Setup(x => x.AlterUrl(It.IsAny <ShortenUrlInputModel>())).Returns(alterUrl);
            var result = sut.Post(shortenUrl) as OkObjectResult;

            result.StatusCode.Should().Be(201);
            result.Value.Should().Be(alterUrl);
        }
        public IActionResult Post([FromBody] ShortenUrlInputModel shortenUrlInputModel)
        {
            var url = string.Empty;

            try
            {
                url = this.shortenUrlService.AlterUrl(shortenUrlInputModel);
                if (string.IsNullOrWhiteSpace(url))
                {
                    return(NotFound());
                }
                return(Ok(url));
            }
            catch (Exception exp)
            {
                //TODO: Log exception here
                return(BadRequest(exp.Message));
            }
        }
Exemple #5
0
        string IShortenUrlService.AlterUrl(ShortenUrlInputModel shortenUrlInputModel)
        {
            try
            {
                if (shortenUrlInputModel == null || string.IsNullOrWhiteSpace(shortenUrlInputModel.Url))
                {
                    throw new Exception("Url should not be null or empty");
                }
                if (!_isValidUrl(shortenUrlInputModel.Url))
                {
                    throw new Exception("Url is invalid");
                }

                return(this.urlCommandFactory.CreateCommand(GetServiceName(shortenUrlInputModel.Url)).ConvertUrl(shortenUrlInputModel.Url));
            }
            catch (Exception)
            {
                throw;
            }
        }