public async Task with_a_valid_url() { var idService = new Mock <IIdService>(); var uniqueId = Guid.NewGuid().ToString(); idService.Setup(id => id.GetUniqueId(It.IsAny <CancellationToken>())) .ReturnsAsync(uniqueId); var repo = new Mock <IModifiedUrlRepository>(); var response = new ModifiedUrlResponse() { ShortUrl = uniqueId }; repo.Setup(rep => rep.AddShortenedUrl(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <CancellationToken>())) .ReturnsAsync(response); var controller = new TestModifiedUrlController(repo.Object, idService.Object); var req = new ShortenUrlRequest() { Url = $"http://{Guid.NewGuid()}" }; var result = (await controller.AddShortUrlAsync(req).ConfigureAwait(false)).Result as OkObjectResult; Assert.NotNull(result); var value = result.Value as ShortUrlResponse; Assert.NotNull(value); Assert.AreEqual(uniqueId, value.ShortUrl); }
public async Task <IActionResult> Post([FromBody] ShortenUrlRequest request) { if (string.IsNullOrWhiteSpace(request?.FullUrl)) { return(this.BadRequest("a url to shorten is required.")); } if (!request.IsValid()) { return(this.BadRequest("the supplied url is not valid.")); } try { var slotNumber = await this._slotStorage.GetNextAvailableSlot(); var shorten = SlotEncoder.Encode(slotNumber); await this._urlStore.Store(shorten, request.FullUrl); var responseModel = new ShortenUrlResponse { Token = shorten }; var fetchUrl = this.Url.Link("GetByToken", new { Token = shorten }); return(this.Created(fetchUrl, responseModel)); } catch (Exception ex) { this._logger.LogError(1, ex, "Unable to register url"); return(this.StatusCode(500, "Unable to register url")); } }
public async Task <IActionResult> ShortenUrlAsync(ShortenUrlRequest request) { var shortUrl = await _shortenService.ShortenUrl(request.Url); var response = new ShortenedUrlResponse(shortUrl, request.Url); HttpContext.Session.AddValue("shortenedUrls", response); return(Ok($"{Request.Scheme}://{Request.Host}/{shortUrl}")); }
public async Task <ActionResult> Shorten([FromBody] ShortenUrlRequest shortenUrlRequest) { if (ModelState.IsValid) { var shortenedUrl = await _urlShortnerService.AddShortenedUrl(shortenUrlRequest.Url); return(Ok(shortenedUrl.UrlHash)); } return(BadRequest("Invalid Url")); }
public async Task with_an_empty_url() { var repo = new Mock <IModifiedUrlRepository>().Object; var idService = new Mock <IIdService>().Object; var controller = new TestModifiedUrlController(repo, idService); var req = new ShortenUrlRequest() { Url = "" }; var result = (await controller.AddShortUrlAsync(req).ConfigureAwait(false)).Result as BadRequestObjectResult; Assert.NotNull(result); }
public async Task <ActionResult <ShortUrlResponse> > AddShortUrlAsync([Required] ShortenUrlRequest url, CancellationToken cancellationToken = default) { if (string.IsNullOrWhiteSpace(url.Url)) { return(BadRequest("Invalid URL")); } var uniqueKey = await _idService.GetUniqueId(cancellationToken) .ConfigureAwait(false); var shortUrl = await GetRepository() .AddShortenedUrl(uniqueKey, Uri.UnescapeDataString(url.Url), cancellationToken) .ConfigureAwait(false); return(Ok(shortUrl.ToShortUrlResponse())); }
public async Task <IActionResult> ShortenURLAsync([FromBody] ApiShortenUrlRequest request) { if (request == null) { throw new InvalidShortenUrlRequestException("Please provide a valid url request"); } if (string.IsNullOrEmpty(request.LongUrl) || !Uri.TryCreate(request.LongUrl, UriKind.Absolute, out Uri uri)) { throw new InvalidShortenUrlRequestException("The URL is either missing or not a valid URI"); } var shortenUrlRequest = new ShortenUrlRequest { LongUrl = request.LongUrl, IpAddress = IpAddress, Timestamp = DateTimeOffset.UtcNow }; var shortUrl = await _urlService.ShortenUrlAsync(shortenUrlRequest); return(Ok(shortUrl)); }
public IActionResult Shorten([Bind("Url")] ShortenUrlRequest request) { var result = _dbContext.Urls.FirstOrDefault(u => u.OriginalUrl.Equals(request.Url)); if (!_urlValidator.IsValid(request.Url)) { return(BadRequest(new { title = "Bad request", message = "URL is not valid" })); } if (result == null) { var hash = _hasher.Hash(request.Url); result = new Url { OriginalUrl = request.Url, Hash = hash }; _dbContext.Urls.Add(result); _dbContext.SaveChanges(); } var url = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host}/{result.Hash}"; return(Json(new { url })); }
/// <summary> /// Shortens a url /// </summary> public async Task <string> ShortenUrlAsync(ShortenUrlRequest request) { if (request == null) { throw new InvalidShortenUrlRequestException("ShortenUrlRequest is null"); } if (string.IsNullOrEmpty(request.LongUrl) || !Uri.TryCreate(request.LongUrl, UriKind.Absolute, out Uri uri)) { throw new InvalidShortenUrlRequestException("The URL is either missing or not a valid URI"); } // Check if the url is a known phishing site var isPhish = await _phishSubmissionsRepository.IsPhishAsync(uri.Host); if (isPhish) { //TODO: Flag ip address throw new InvalidShortenUrlRequestException($"The URL {request.LongUrl} is flagged for phishing"); } // TODO: add logic for shortening the submitted URL var encodedUrl = request.LongUrl; var shortUrl = new ShortUrl { Id = Guid.NewGuid(), ShortenUrl = encodedUrl, LongUrl = request.LongUrl, CreatedOn = DateTimeOffset.UtcNow }; await _urlRepository.AddUrlAsync(shortUrl); return(encodedUrl); }
public async Task <ActionResult> Put([FromBody] ShortenUrlRequest url) { return(Ok(await shortenUrlService.TryAddShortenUrl(url))); }