public async Task <ShortUrlCreateResponse> Create(ShortUrlCreateRequest shortUrlCreateRequest)
        {
            shortUrlCreateRequest.LongUrl = shortUrlCreateRequest.LongUrl?.Trim();
            ValidUrlGuard(shortUrlCreateRequest.LongUrl);

            var keyInfo = await GenerateShortKey();

            if (!keyInfo.Success)
            {
                throw new Exception("An internal error occured");
            }

            var shortUrlKey = await GetExistingShortUrl(shortUrlCreateRequest.LongUrl);

            if (string.IsNullOrEmpty(shortUrlKey))
            {
                shortUrlKey = await CreateShortUrl(shortUrlCreateRequest, keyInfo.Key);
            }
            var shortUrlCreateResponse = new ShortUrlCreateResponse
            {
                LongUrl = shortUrlCreateRequest.LongUrl,
                SortUrl = $"{shortUrlCreateRequest.HostUrl}/{shortUrlKey}"
            };

            return(shortUrlCreateResponse);
        }
        public async Task <IActionResult> Create([FromBody] ShortUrlCreateRequest shortUrlCreateRequest)
        {
            shortUrlCreateRequest.UserId  = _currentLoginUser.AccountId;
            shortUrlCreateRequest.HostUrl = GetHostUrl();
            var shortUrlCreateResponse = await _shortUrlService.Create(shortUrlCreateRequest);

            return(Ok(shortUrlCreateResponse));
        }
Exemplo n.º 3
0
        public void Url_Empty()
        {
            var shortUrlService  = _serviceProvider.GetService <IShortUrlService>();
            var currentLoginUser = _serviceProvider.GetService <ICurrentLoginUser>();

            var shortUrlCreateRequest = new ShortUrlCreateRequest
            {
                HostUrl = "http://127.0.0.1/r",
                UserId  = currentLoginUser.AccountId
            };
            var exception = Assert.ThrowsAsync <Exception>(() => shortUrlService.Create(shortUrlCreateRequest));

            StringAssert.AreEqualIgnoringCase(exception.Message, "Long url is required");
        }
Exemplo n.º 4
0
        public async Task Generate_Sort_Url_Shorter_Then_Long_Url()
        {
            var shortUrlService  = _serviceProvider.GetService <IShortUrlService>();
            var currentLoginUser = _serviceProvider.GetService <ICurrentLoginUser>();

            var shortUrlCreateRequest = new ShortUrlCreateRequest
            {
                LongUrl = "https://stackoverflow.com/questions/37724738/how-to-unit-test-asp-net-core-application-with-constructor-dependency-injection",
                HostUrl = "http://127.0.0.1/r",
                UserId  = currentLoginUser.AccountId
            };
            var response = await shortUrlService.Create(shortUrlCreateRequest);

            Assert.Greater(shortUrlCreateRequest.LongUrl.Length, response.SortUrl.Length);
        }
Exemplo n.º 5
0
        public void Invalid_Url()
        {
            var shortUrlService  = _serviceProvider.GetService <IShortUrlService>();
            var currentLoginUser = _serviceProvider.GetService <ICurrentLoginUser>();

            var shortUrlCreateRequest = new ShortUrlCreateRequest
            {
                LongUrl = "stackoverflow/questions/37724738/how-to-unit-test-asp-net-core-application-with-constructor-dependency-injection",
                HostUrl = "http://127.0.0.1/r",
                UserId  = currentLoginUser.AccountId
            };
            var exception = Assert.ThrowsAsync <Exception>(() => shortUrlService.Create(shortUrlCreateRequest));

            StringAssert.AreEqualIgnoringCase(exception.Message, "Url is not valid");
        }
        private async Task <string> CreateShortUrl(ShortUrlCreateRequest shortUrlCreateRequest, string key)
        {
            var shortUrlModel = new ShortUrlModel
            {
                Key        = key,
                CreatedUtc = DateTime.UtcNow,
                CreatorId  = shortUrlCreateRequest.UserId,
                ExpiresUtc = DateTime.UtcNow.AddDays(ApplicationVariable.ShortUrlExpireDays),
                UpdateDate = DateTime.UtcNow,
                Url        = shortUrlCreateRequest.LongUrl
            };
            await _shortUrlRepository.Insert(shortUrlModel);

            return(shortUrlModel.Key);
        }
Exemplo n.º 7
0
        public async Task Get_Same_Sort_Url_For_Same_Long_Url()
        {
            var shortUrlService  = _serviceProvider.GetService <IShortUrlService>();
            var currentLoginUser = _serviceProvider.GetService <ICurrentLoginUser>();

            var shortUrlCreateRequest = new ShortUrlCreateRequest
            {
                LongUrl = "https://stackoverflow.com/questions/37724738/how-to-unit-test-asp-net-core-application-with-constructor-dependency-injection",
                HostUrl = "http://127.0.0.1/r",
                UserId  = currentLoginUser.AccountId
            };
            var firstResponse = await shortUrlService.Create(shortUrlCreateRequest);

            var secondResponse = await shortUrlService.Create(shortUrlCreateRequest);

            StringAssert.AreEqualIgnoringCase(firstResponse.SortUrl, secondResponse.SortUrl);
        }
Exemplo n.º 8
0
        public async Task Visit_With_Invalid_Key()
        {
            var shortUrlService  = _serviceProvider.GetService <IShortUrlService>();
            var currentLoginUser = _serviceProvider.GetService <ICurrentLoginUser>();

            var shortUrlCreateRequest = new ShortUrlCreateRequest
            {
                LongUrl = "https://stackoverflow.com/questions/37724738/how-to-unit-test-asp-net-core-application-with-constructor-dependency-injection",
                HostUrl = "http://127.0.0.1/r",
                UserId  = currentLoginUser.AccountId
            };
            await shortUrlService.Create(shortUrlCreateRequest);

            var exception = Assert.ThrowsAsync <ShortUrlNotFoundException>(() => shortUrlService.GetShortUrl("Invalid_key", "127.0.0.1"));

            StringAssert.AreEqualIgnoringCase(exception.Message, "Url is not found");
        }
Exemplo n.º 9
0
        public async Task Visit_via_Sort_Url()
        {
            var shortUrlService  = _serviceProvider.GetService <IShortUrlService>();
            var currentLoginUser = _serviceProvider.GetService <ICurrentLoginUser>();

            var shortUrlCreateRequest = new ShortUrlCreateRequest
            {
                LongUrl = "https://stackoverflow.com/questions/37724738/how-to-unit-test-asp-net-core-application-with-constructor-dependency-injection",
                HostUrl = "http://127.0.0.1/r",
                UserId  = currentLoginUser.AccountId
            };

            var response = await shortUrlService.Create(shortUrlCreateRequest);

            var key           = response.SortUrl.Replace($"{shortUrlCreateRequest.HostUrl}/", "");
            var shortUrlModel = await shortUrlService.GetShortUrl(key, "127.0.0.1");

            var logs = await shortUrlService.GetShortUrlLogs(shortUrlModel.Id);

            Assert.AreEqual(1, logs.Count);
            StringAssert.AreEqualIgnoringCase(shortUrlCreateRequest.LongUrl, shortUrlModel.Url);
        }