private void WriteLog(string shortUrl, ShortUrlResult shortUrlResult) { _logger.LogInformation("查询,返回:200 {@ret}; 参数:{@shortUrl} 调用:{@sourceUrl}", shortUrlResult, shortUrl, _service.GetRemote(Request)); }
public ActionResult Create(CreateShortUrlCommand command) { Check.Argument.IsNotNull(command, "command"); ShortUrlResult result = shortUrlService.CreateWithUserName(command.Url, command.Alias, command.IPAddress, command.UserName); object model = (result.ShortUrl != null) ? new CreateUrlViewModel(result.ShortUrl) : null; return(this.AdaptivePostRedirectGet(result.RuleViolations, model, Url.Create())); }
public ShortUrlResultTests() { var alias = new Alias { Name = "dtntshtt", ShortUrl = new ShortUrl { Title = "Daily .NET News", Url = "http://dotnetshoutout.com" } }; dto = new ShortUrlDTO(alias, 3, "http://shrinkr.com/1", "http://shrinkr.com/Preview/1"); result = new ShortUrlResult(dto); }
public void Create_should_add_violation_rules_to_model_errors() { controller.MockHttpContext("/", "~/Create", "GET"); var violations = new List <RuleViolation> { new RuleViolation("someParam", "Error Message") }; var result = new ShortUrlResult(violations); shortUrlService.Setup(svc => svc.CreateWithApiKey(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())).Returns(result); Assert.IsType <ApiResult>(controller.Create(new CreateShortUrlApiCommand())); Assert.Equal(1, controller.ViewData.ModelState.Count); }
public ActionResult Preview(ShortUrlVisitCommand command) { Check.Argument.IsNotNull(command, "command"); ShortUrlResult result = shortUrlService.GetByAlias(command.Alias); ModelState.Merge(result.RuleViolations); if (result.ShortUrl == null) { return(new NotFoundResult()); } return(View(result.ShortUrl)); }
public ActionResult Create(CreateShortUrlApiCommand command) { Check.Argument.IsNotNull(command, "command"); ShortUrlResult result = shortUrlService.CreateWithApiKey(command.Url, command.Alias, command.IPAddress, command.ApiKey); ModelState.Merge(result.RuleViolations); if (result.ShortUrl != null) { ViewData.Model = new CreateUrlViewModel(result.ShortUrl); } return(new ApiResult(command.ResponseFormat)); }
public ActionResult Query([FromBody] string shortUrl) { var err = _service.CheckToken(Request); if (err != null) { _logger.LogWarning("查询,返回:404 {@err}; 参数:{@shortUrl} 调用:{@sourceUrl}", err, shortUrl, _service.GetRemote(Request)); return(BadRequest(err)); } var baseUrl = GetBaseUrl(); if (shortUrl.IndexOf(baseUrl, StringComparison.OrdinalIgnoreCase) == -1) { var shortUrlResult = new ShortUrlResult { Code = -3, ShortUrl = shortUrl, ErrMsg = "无效的地址。" }; WriteLog(shortUrl, shortUrlResult); return(Ok(shortUrlResult)); } var tmp = shortUrl.Substring(baseUrl.Length + 1); var ret = _service.GetLongUrl(tmp); if (ret == null) { var shortUrlResult = new ShortUrlResult { Code = -3, ShortUrl = shortUrl, ErrMsg = "没找到" }; WriteLog(shortUrl, shortUrlResult); return(Ok(shortUrlResult)); } var urlResult = new ShortUrlResult { Code = 0, ShortUrl = shortUrl, LongUrl = ret }; WriteLog(shortUrl, urlResult); return(Ok(urlResult)); }
public void Create_should_return_api_result_with_create_url_view_model() { controller.MockHttpContext("/", "~/Create", "GET"); var shortUrl = new ShortUrl { Url = "http://shirnkr.com/someurl" }; var shortUrlDto = new ShortUrlDTO(new Alias { ShortUrl = shortUrl }, 3, "http://shirnkr.com/visit", "http://shirnkr.com/preview"); var result = new ShortUrlResult(shortUrlDto); shortUrlService.Setup(svc => svc.CreateWithApiKey(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())).Returns(result); Assert.IsType <ApiResult>(controller.Create(new CreateShortUrlApiCommand())); Assert.Equal(shortUrl.Url, ((CreateUrlViewModel)controller.ViewData.Model).Url); }
public ActionResult Visit(ShortUrlVisitCommand command) { Check.Argument.IsNotNull(command, "command"); bool sameDomain = !string.IsNullOrWhiteSpace(command.Referrer) && command.Referrer.StartsWith(Url.ApplicationRoot(), StringComparison.OrdinalIgnoreCase); if (!sameDomain) { ShortUrlResult shortUrlResult = shortUrlService.GetByAlias(command.Alias); ShortUrlDTO shortUrl = shortUrlResult.ShortUrl; if (shortUrl == null) { return(new NotFoundResult()); } if (shortUrl.SpamStatus.IsSpam()) { return(RedirectToAction("Preview", new { alias = shortUrl.Alias })); } } if (sameDomain) { command.Referrer = null; } VisitResult visitResult = shortUrlService.Visit(command.Alias, command.IPAddress, command.Browser, command.Referrer); ModelState.Merge(visitResult.RuleViolations); if (visitResult.Visit == null) { return(new NotFoundResult()); } string url = visitResult.Visit.Url; return(settings.RedirectPermanently ? new PermanentRedirectResult(url) : Redirect(url)); }