public static async Task <HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log) { var content = await req.Content.ReadAsStringAsync(); var request = JsonConvert.DeserializeObject <ShortUrlRequest>(content); Assembly assembly = typeof(PostNewUrl).GetTypeInfo().Assembly; var builder = new ConfigurationBuilder() .AddJsonFile(new EmbeddedFileProvider(assembly, "post_new_url"), "appsettings.json", true, false); var configuration = builder.Build(); var result = new ShortUrlResponse( (shortUrl) => ShortenUrlFunctions.SaveShortUrl(() => configuration["StorageConnection"], shortUrl), ShortenUrlFunctions.ShortenUrl, (hash) => ShortenUrlFunctions.RetrieveShortUrl(() => configuration["StorageConnection"], hash), request); return(new HttpResponseMessage { StatusCode = result.StatusCode, Content = new StringContent(JObject.FromObject(result.value).ToString(), Encoding.UTF8, "application/json") }); }
public IActionResult Delete(string shorturl) { ShortUrlResponse result = _urlService.DeleteURL(shorturl); if (result != null) { _logger.LogInformation($"URL eliminada: {shorturl}"); return(Ok(result)); } return(NotFound(result)); }
public IActionResult Post([FromBody] ShortURLRequest model) { if (ModelState.IsValid) { ShortUrlResponse result = _urlService.SaveURL(model); if (result != null) { _logger.LogInformation($"URL creada: {result.Model.ShortURL} a {result.Model.LongURL}"); return(Ok(result)); } } return(BadRequest(ModelState.Values)); }
public void ShortenUrlShouldNotCallSaveFunctionWhenShortUrlFound() { var originalUrl = "http://google.com/"; var calledSave = false; var result = new ShortUrlResponse( (shortUrl) => { calledSave = true; return(Task.Run(() => true)); }, (url) => url, (h) => Task.Run(() => h), new ShortUrlRequest { OriginalUrl = originalUrl }); Assert.False(calledSave); }