public async Task <IActionResult> CreateShortUrl([FromBody] UrlToShorten urlToShorten)
        {
            var shortUrl = await _urlShortenerService.CreateShortUrl(urlToShorten);

            if (shortUrl == null)
            {
                return(BadRequest());
            }

            return(Ok(shortUrl));
        }
        public async Task <ShortUrlResponse> CreateShortUrl(UrlToShorten urlToShorten)
        {
            if (!urlToShorten.LongUrl.Contains("https://") || !urlToShorten.LongUrl.Contains("http://"))
            {
                if (urlToShorten.LongUrl.StartsWith("www."))
                {
                    urlToShorten.LongUrl = $"{Https}{urlToShorten.LongUrl}";
                }
            }

            var validUri = Uri.IsWellFormedUriString(urlToShorten.LongUrl, UriKind.Absolute);

            if (validUri == false)
            {
                throw new ArgumentException($"The submitted value isn't a valid url: {urlToShorten}");
            }

            var shortId = CreateRandomString(ShortIdLength);

            var baseUrl = AssembleBaseUrl();

            var urlInformation = new UrlInformation
            {
                CreationDate = _clock.Now(),
                Id           = shortId,
                ShortId      = shortId,
                LogUrl       = urlToShorten.LongUrl,
                ShortUrl     = $"{baseUrl}/{shortId}"
            };

            var success = await _cosmosDbRepository.CreateShortUrl(urlInformation);

            if (success)
            {
                return new ShortUrlResponse
                       {
                           ShortUrl = urlInformation.ShortUrl
                       }
            }
            ;

            return(null);
        }
예제 #3
0
        public async Task CreateShortUrl_AnyInput_Fails()
        {
            // Arrange
            var urlToShortenItem = new UrlToShorten
            {
                LongUrl = "https://www.wikipedia.org/"
            };

            _cosmosDbRepositoryMock.Setup(x => x.CreateShortUrl(It.IsAny <UrlInformation>()))
            .Returns(Task.FromResult(false))
            .Verifiable();

            MockHttpAccessorContext();

            // Act
            var sut    = CreateUrlShortenerServiceSut();
            var result = await sut.CreateShortUrl(urlToShortenItem);

            // Assert
            result.Should().BeNull();
        }
예제 #4
0
        public async Task CreateShortUrl_DataRow_Successful(string uri)
        {
            // Arrange
            var urlToShortenItem = new UrlToShorten
            {
                LongUrl = uri
            };

            _cosmosDbRepositoryMock.Setup(x => x.CreateShortUrl(It.IsAny <UrlInformation>()))
            .Returns(Task.FromResult(true))
            .Verifiable();

            MockHttpAccessorContext();

            // Act
            var sut    = CreateUrlShortenerServiceSut();
            var result = await sut.CreateShortUrl(urlToShortenItem);

            // Assert
            result.Should().NotBeNull();
            result.Should().BeOfType <ShortUrlResponse>();
        }