public void CanShortenUrl()
        {
            var userUrl = new UserDto()
            {
                Id = 1000,
                OriginalUrl = "http://google.com",
                AccessCount = 0,
                LastAccessedOn = DateTime.Now
            };

            var dbUserUrl = new Shorty.Data.UserUrl()
            {
                Id = 1000,

            };

            var repo = new Mock<IRepository>();
            repo.Setup(r => r.SaveUrl(It.IsAny<UserUrl>()))
                .Returns(() => dbUserUrl);

            var converter = new BaseConverter();
            shortener = new ShortService(converter, repo.Object);

            var result = shortener.ShortenUrl(userUrl);

            Assert.AreEqual("Ge", result.Url);
        }
示例#2
0
文件: Site.cs 项目: Dotnetwill/vfy.be
        public Site(IShortener shortener)
        {
            StaticConfiguration.DisableCaches      = true;
            StaticConfiguration.DisableErrorTraces = false;

            Get["/"] = _ => View["index"];

            Get["/api/expand-url"] = _ =>
            {
                if (!Request.Query.Code.HasValue || String.IsNullOrEmpty(Request.Query.Code))
                {
                    return(HttpStatusCode.BadRequest);
                }

                String code = Request.Query.Code;

                if (code.Contains("/"))
                {
                    code = code.Split('/').Last();
                }
                Console.WriteLine(code);
                var info = shortener.Expand(code);
                var res  = new DetailsResponse();
                if (String.IsNullOrEmpty(info.Item1))
                {
                    res.Error = "No url found";
                }
                else
                {
                    res.Url    = info.Item1;
                    res.Clicks = info.Item2;
                }

                return(Response.AsJson(res));
            };

            Get["/{shortCode}"] = (arg) =>
            {
                String realUrl = shortener.Expand(arg.shortCode).Item1;
                if (String.IsNullOrEmpty(realUrl))
                {
                    return(HttpStatusCode.NotFound);
                }

                return(Response.AsRedirect(realUrl));
            };

            Post["/api/shorten-url"] = _ =>
            {
                if (!Request.Form.Url.HasValue || String.IsNullOrEmpty(Request.Form.Url))
                {
                    return(HttpStatusCode.BadRequest);
                }

                var url = HttpUtility.HtmlEncode(Request.Form.Url);
                return(String.Concat(SiteUrl, shortener.Shorten(url)));
            };
        }
示例#3
0
文件: Site.cs 项目: Dotnetwill/vfy.be
		public Site (IShortener shortener)
		{
			StaticConfiguration.DisableCaches = true;
			StaticConfiguration.DisableErrorTraces = false;
			
			Get["/"] = _ =>  View["index"];
			
			Get["/api/expand-url"] = _ => 
			{
				if(!Request.Query.Code.HasValue || String.IsNullOrEmpty(Request.Query.Code))
					return HttpStatusCode.BadRequest;
				
				String code = Request.Query.Code;
				
				if(code.Contains("/"))
				{
					code = code.Split('/').Last();
				}
				Console.WriteLine(code);
				var info = shortener.Expand(code);
				var res = new DetailsResponse();
				if(String.IsNullOrEmpty(info.Item1))
				{
					res.Error = "No url found";
				}
				else
				{
					res.Url = info.Item1;
					res.Clicks = info.Item2;
				}
				
				return Response.AsJson(res);
			};
			
			Get["/{shortCode}"] = (arg) =>  
			{
				String realUrl = shortener.Expand(arg.shortCode).Item1;
				if(String.IsNullOrEmpty(realUrl))
					return HttpStatusCode.NotFound;
				
				return Response.AsRedirect(realUrl);
			};
			
			Post["/api/shorten-url"] = _ => 
			{ 
				if(!Request.Form.Url.HasValue || String.IsNullOrEmpty(Request.Form.Url))
					return HttpStatusCode.BadRequest;
				
				var url = HttpUtility.HtmlEncode(Request.Form.Url);
				return String.Concat(SiteUrl, shortener.Shorten(url));
			};
			
		}
        public void CanExpandUrl()
        {
            var dbUserUrl = new Shorty.Data.UserUrl()
            {
                Id = 1000,
                OriginalUrl = "http://google.com",
                AccessCount = 0,
            };

            var testUrl = "http://bitly.com/Ge";
            var repo = new Mock<IRepository>();
            repo.Setup(r => r.GetById(It.IsAny<int>())).Returns(() => dbUserUrl);

            var converter = new BaseConverter();
            shortener = new ShortService(converter, repo.Object);

            var result = shortener.ExpandUrl(testUrl);
        }
示例#5
0
 public HomeController(IShortener shortService)
 {
     _shortService = shortService;
 }
示例#6
0
 public HomeController(IShortener shortener, UrlCutContext context)
 {
     _shortener = shortener;
     _context   = context;
 }
示例#7
0
 public ListController(IShortener shortener)
 {
     _shortener = shortener;
 }