Exemplo n.º 1
0
 public ApiController(ILogger <HomeController> logger, ShortUrlDbContext dbContext, IOptions <AppSettings> config, ShortUrlService shortUrlService)
 {
     _logger          = logger;
     _dbContext       = dbContext;
     _shortUrlService = shortUrlService;
     _config          = config.Value;
 }
Exemplo n.º 2
0
        public ShortUrlSeriviceTests()
        {
            userRepository          = new Mock <IUserRepository>();
            shortUrlRepository      = new Mock <IShortUrlRepository>();
            visitRepository         = new Mock <IVisitRepository>();
            bannedDomainRepository  = new Mock <IBannedDomainRepository>();
            reservedAliasRepository = new Mock <IReservedAliasRepository>();
            unitOfWork             = new Mock <IUnitOfWork>();
            externalContentService = new Mock <IExternalContentService>();
            thumbnail    = new Mock <IThumbnail>();
            baseX        = new Mock <IBaseX>();
            urlResolver  = new Mock <IUrlResolver>();
            spamDetector = new Mock <ISpamDetector>();

            urlResolver.SetupGet(ur => ur.ApplicationRoot).Returns("http://shrinkr.com");

            shortUrlCreatedEvent      = new Mock <ShortUrlCreatedEvent>();
            possibleSpamDetectedEvent = new Mock <PossibleSpamDetectedEvent>();
            shortUrlVisitedEvent      = new Mock <ShortUrlVisitedEvent>();

            var eventAggregator = new Mock <IEventAggregator>();

            eventAggregator.Setup(ea => ea.GetEvent <ShortUrlCreatedEvent>()).Returns(shortUrlCreatedEvent.Object);
            eventAggregator.Setup(ea => ea.GetEvent <PossibleSpamDetectedEvent>()).Returns(possibleSpamDetectedEvent.Object);
            eventAggregator.Setup(ea => ea.GetEvent <ShortUrlVisitedEvent>()).Returns(shortUrlVisitedEvent.Object);

            shortUrlService = new ShortUrlService(userRepository.Object, shortUrlRepository.Object, visitRepository.Object, bannedDomainRepository.Object, reservedAliasRepository.Object, unitOfWork.Object, externalContentService.Object, thumbnail.Object, baseX.Object, urlResolver.Object, eventAggregator.Object, new[] { spamDetector.Object });
        }
Exemplo n.º 3
0
        public JsonResult GetShortJson(string oldUrl)
        {
            AjaxRespone ar = new AjaxRespone();

            try
            {
                if (string.IsNullOrEmpty(oldUrl))
                {
                    throw new Exception("请输入原始链接");
                }
                var url = ShortUrlService.DataBaseShortUrl(oldUrl);
                if (string.IsNullOrEmpty(url))
                {
                    throw new Exception("短链接转换为失败");
                }
                ar.ErrorCode = 0;
                ar.Result    = link + url;
            }
            catch (Exception ex)
            {
                ar.ErrorCode = 1;
                ar.ErrorDesc = ex.Message;
            }
            return(Json(ar));
        }
        public void GetUrlReturnsUrlWhenIndexValid()
        {
            // Run tests
            var shortUrl = new ShortUrlService();

            shortUrl.AddUrl(Url);
            var testUrl = shortUrl.GetUrl(0);

            // Assertions
            Assert.Equal(Url, testUrl);
        }
Exemplo n.º 5
0
        public HttpResponseMessage Get(HttpRequestMessage request, int id)
        {
            var principal = (ClaimsPrincipal)User;
            var apiKeyId  = int.Parse(principal.Claims.First(x => x.Type == ClaimTypes.Sid).Value);
            var isAdmin   = principal.Claims.First(x => x.Type == ClaimTypes.UserData).Value == "Admin";

            try
            {
                return(request.CreateResponse(HttpStatusCode.OK, ShortUrlService.FindOne(id, apiKeyId, isAdmin)));
            }
            catch (Exception)
            {
                return(new HttpResponseMessage(HttpStatusCode.BadRequest));
            }
        }
Exemplo n.º 6
0
        public void AddUrlReturnsShortUrl()
        {
            // Run tests
            var shortUrl  = new ShortUrlService();
            var shortUrl1 = shortUrl.AddUrl(Url1);
            var shortUrl2 = shortUrl.AddUrl(Url2);

            // Assertions
            Assert.IsType <ShortUrl>(shortUrl1);
            Assert.IsType <ShortUrl>(shortUrl2);
            Assert.Equal(Url1, shortUrl1.OriginalUrl);
            Assert.Equal(Url2, shortUrl2.OriginalUrl);
            Assert.Equal(0, shortUrl1.ShortenedUrl);
            Assert.Equal(1, shortUrl2.ShortenedUrl);
        }
Exemplo n.º 7
0
        public HttpResponseMessage Post(HttpRequestMessage request, ShortUrlDto dto)
        {
            var principal = (ClaimsPrincipal)User;
            var apiKeyId  = int.Parse(principal.Claims.First(x => x.Type == ClaimTypes.Sid).Value);

            dto.ApiKeyId = apiKeyId;

            try
            {
                return(request.CreateResponse(HttpStatusCode.Created, ShortUrlService.Create(dto)));
            }
            catch (Exception)
            {
                return(new HttpResponseMessage(HttpStatusCode.BadRequest));
            }
        }
Exemplo n.º 8
0
        public ActionResult Index()
        {
            var url = Request.Url.PathAndQuery.Substring(1);

            if (string.IsNullOrEmpty(url))
            {
                // Not Found'a yönlendir.
                return(View("NotFound"));
            }


            var shortUrl = ShortUrlService.FindByUrl(url);

            if (shortUrl == null)
            {
                // Not Found'a yönlendir.
                return(View("NotFound"));
            }

            // ShortUrl için log oluştur.
            // Bağlanan cihazla ilgili bilgileri kaydet.
            var usageDto = new ShortUrlUsageDto()
            {
                ShortUrlId    = shortUrl.Id,
                ClientIP      = Request.UserHostAddress,
                ClientBrowser = Request.Browser.Browser,
                ClientDevice  = Request.Browser.IsMobileDevice ? "Mobile" : "Desktop",
                ClientOS      = Request.Browser.Platform
            };

            try
            {
                ShortUrlService.AddUsage(usageDto);
            }
            catch (Exception)
            {
                Console.WriteLine("ERROR: On adding usage info.");
            }

            return(Redirect(shortUrl.OriginalUrl));
        }
Exemplo n.º 9
0
        public JsonResult GetLongJson(string newUrl)
        {
            AjaxRespone ar = new AjaxRespone();

            try
            {
                if (string.IsNullOrEmpty(newUrl))
                {
                    throw new Exception("请输入短链接");
                }
                var url = ShortUrlService.GetLongUrl(newUrl);
                ar.ErrorCode = 0;
                ar.Result    = url;
            }
            catch (Exception ex)
            {
                ar.ErrorCode = 1;
                ar.ErrorDesc = ex.Message;
            }
            return(Json(ar));
        }
Exemplo n.º 10
0
        /// <summary>
        /// 生成多媒体内容标签
        /// </summary>
        /// <param name="shrotUrl">短网址</param>
        /// <param name="parsedMedia">多媒体连接实体</param>
        private string ParsedMediaTagGenerateForMobile(string shrotUrl, ParsedMedia parsedMedia)
        {
            if (parsedMedia == null)
            {
                return(string.Empty);
            }

            if (parsedMedia.MediaType == MediaType.Audio)
            {
                string         musicHtml      = "<p><a href=\"{1}\" ntype='mediaPlay'>点击播放音乐</a></p>";
                ShortUrlEntity shortUrlEntity = new ShortUrlService().Get(parsedMedia.Alias);
                return(string.Format(musicHtml, SiteUrls.FullUrl(SiteUrls.Instance()._MusicDetail(parsedMedia.Alias)), shortUrlEntity == null ? null : shortUrlEntity.Url));
            }
            else if (parsedMedia.MediaType == MediaType.Video)
            {
                string         videoHtml      = "<p><a  href=\"{1}\" ntype=\"mediaPlay\">{1}<img src=\"{2}\"></a></p>";
                ShortUrlEntity shortUrlEntity = new ShortUrlService().Get(parsedMedia.Alias);
                return(string.Format(videoHtml, SiteUrls.FullUrl(SiteUrls.Instance()._VideoDetail(parsedMedia.Alias)), shortUrlEntity == null ? null : shortUrlEntity.Url, parsedMedia.ThumbnailUrl));
            }

            return(string.Empty);
        }
Exemplo n.º 11
0
        public void AddUrlAddsNewUrl()
        {
            // Run tests
            var shortUrl = new ShortUrlService();

            // Assertions
            Assert.Equal(0, shortUrl.Urls.Count);

            // More tests
            shortUrl.AddUrl(Url1);

            // More assertions
            Assert.Equal(1, shortUrl.Urls.Count);
            Assert.Contains(Url1, shortUrl.Urls);

            // More tests
            shortUrl.AddUrl(Url2);

            // More assertions
            Assert.Equal(2, shortUrl.Urls.Count);
            Assert.Contains(Url2, shortUrl.Urls);
        }
        public void GetUrlThrowsWhenIndexInvalid()
        {
            var shortUrl = new ShortUrlService();

            Assert.Throws <ArgumentOutOfRangeException>(() => shortUrl.GetUrl(0));
        }
Exemplo n.º 13
0
 public ShortnerController(ShortUrlService shortUrlService)
 {
     _shortUrlService = shortUrlService;
 }
Exemplo n.º 14
0
 public HomeController(ILogger <HomeController> logger, ShortUrlService shortUrlService, IOptions <AppSettings> config)
 {
     _logger          = logger;
     _shortUrlService = shortUrlService;
     _config          = config.Value;
 }
Exemplo n.º 15
0
 public UrlController(ShortUrlService shortUrlService)
 {
     _shortUrlService = shortUrlService;
 }