Beispiel #1
0
            public async void ShouldReturnOkResult()
            {
                // Arrange
                var url = new UrlCreate
                {
                    ActualUrl = "https://google.com"
                };

                var urlDto = new UrlDto
                {
                    Id           = 1,
                    ActualUrl    = "https://google.com",
                    ShortenedUrl = "fGwY73",
                    AccessCount  = 0
                };

                UrlServiceMock.Setup(x => x.AddUrl(url)).ReturnsAsync(urlDto);

                // Act
                var result = await UrlControllerToTest.Post(url);

                // Assert
                var okResult = Assert.IsType <OkObjectResult>(result);

                Assert.Same(urlDto, okResult.Value);
            }
            public async void ShouldAdd()
            {
                // Arrange
                UrlCreate urlCreate = new UrlCreate {
                    ActualUrl = "https://bbc.co.uk"
                };

                // Act
                var result = await UrlServiceToTest.AddUrl(urlCreate);

                // Assert
                Assert.True(result.ActualUrl == urlCreate.ActualUrl);
            }
Beispiel #3
0
        public async Task <IActionResult> Post(UrlCreate url)
        {
            try
            {
                var result = await _urlService.AddUrl(url);

                return(Ok(result));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error in Post", new Object[] { url });
                throw;
            }
        }
        public async Task <UrlDto> AddUrl(UrlCreate url)
        {
            // Add the new url to the database
            if (url == null || url.ActualUrl == null || url.ActualUrl.Trim() == string.Empty)
            {
                throw new Exception("Invalid url");
            }

            // Now generate the short url.
            string shortenedUrl = string.Empty;

            while (shortenedUrl == string.Empty)
            {
                var shortenedValue = Shortener.CreateShortCode();
                var existing       = _context.Urls.Where(u => u.ShortenedUrl == shortenedValue).FirstOrDefault();
                if (existing == null)
                {
                    shortenedUrl = shortenedValue;
                }
            }

            _context.Urls.Add(new Url
            {
                ActualUrl    = url.ActualUrl,
                FriendlyName = url.FriendlyName,
                ShortenedUrl = shortenedUrl,
                Created      = DateTime.Now
            });

            await _context.SaveChangesAsync();

            return(new UrlDto
            {
                ActualUrl = url.ActualUrl,
                ShortenedUrl = shortenedUrl,
                AccessCount = 0
            });
        }