Example #1
0
        public void Create_Insecure_Short_Url()
        {
            CreateUrlRequest createUrlRequest = new CreateUrlRequest
            {
                Url = "a"
            };

            Mock.Get(this.urlService.keyValueStore)
            .Setup(behavior => behavior.GetNewId())
            .Returns(Task.FromResult(1L));

            Mock.Get(this.urlService.httpContextAccessor)
            .Setup(behavior => behavior.HttpContext.Request.Host)
            .Returns(new HostString("localhost:44332"));

            Mock.Get(this.urlService.httpContextAccessor)
            .Setup(behavior => behavior.HttpContext.Request.IsHttps)
            .Returns(false);

            Task <CreateUrlResponse> awaitable = this.urlService.CreateUrl(createUrlRequest);

            CreateUrlResponse createUrlResponse = awaitable.Result;

            Assert.NotNull(createUrlResponse);
            Assert.AreEqual("n", createUrlResponse.Id);
            Assert.AreEqual("http://localhost:44332/n", createUrlResponse.ShortUrl);
            Assert.AreEqual("a", createUrlResponse.LongUrl);
        }
Example #2
0
        /// <summary>
        /// Redirects to original URL.
        /// </summary>
        /// <param name="shortUrl"></param>
        /// <returns></returns>
        /// <exception cref="ApplicationException"></exception>
        public async Task <GetUrlResponse> GetLongUrl(string shortUrl)
        {
            Ensure.NotNullOrEmpty(shortUrl, "url can't be null");

            CreateUrlResponse createUrlResponse = await this.keyValueStore.Get <CreateUrlResponse>(shortUrl);

            if (createUrlResponse == null)
            {
                throw new ApiNotFoundException($"Url {shortUrl} not found");
            }

            GetUrlResponse getUrlResponse = GetUrlResponse.Create(createUrlResponse.LongUrl);

            return(getUrlResponse);
        }
Example #3
0
        /// <summary>
        /// Creates the URL.
        /// </summary>
        /// <param name="createUrlRequest">The create URL request.</param>
        /// <returns></returns>
        public async Task <CreateUrlResponse> CreateUrl(CreateUrlRequest createUrlRequest)
        {
            Ensure.NotNullOrEmpty(createUrlRequest.Url, "url params can't be null");

            Base62Converter converter = new Base62Converter();

            long id = await this.keyValueStore.GetNewId();

            string segment = converter.Encode(id.ToString());

            string host = GetHost();

            CreateUrlResponse createUrlResponse = CreateUrlResponse
                                                  .Create(segment, $"{host}/{segment}", createUrlRequest.Url);

            await this.keyValueStore.Add(segment, createUrlResponse);

            return(createUrlResponse);
        }