# require File.dirname(__FILE__) + '/../lib/skinny_jeans' require File.expand_path(File.dirname(__FILE__) + "/../lib/skinny_jeans") require 'test/unit' require 'pp' class SkinnyJeans::StringParserTest < Test::Unit::TestCase def test_can_get_all_urls_from_string _string=<<-EOF 98.244.200.209 - - [01/Dec/2010:11:51:26 -0800] "GET /deals/apple-ipod-touch HTTP/1.1" 200 11448 "http://www.google.com/m/search?oe=UTF-8&client=safari&hl=en&q=best+deals+for+the+4th+generation+iPod+touch+32+gb&gws_link_params=spell:1&ei=aqb2TJDBLqGutgfp862NAg&ved=0CBEQBSgA" "Mozilla/5.0 (iPod; U; CPU iPhone OS 4_1 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8B117 Safari/6531.22.7" "-" EOF sjsp = SkinnyJeans::StringParser.new(_string) assert_equal 1, sjsp.all_urls.size assert_equal "http://www.google.com/m/search?oe=UTF-8&client=safari&hl=en&q=best+deals+for+the+4th+generation+iPod+touch+32+gb&gws_link_params=spell:1&ei=aqb2TJDBLqGutgfp862NAg&ved=0CBEQBSgA", sjsp.all_urls.first end def test_can_parse_crappy_url _string=<<-EOF http://www.bing.com/search?q=XPS+17+coupon&src={referrer:source?} EOF sjsp = SkinnyJeans::StringParser.new(_string) assert_equal "xps 17 coupon", sjsp.get_search_keyword end def test_return_nil_of_no_urls _string=<<-EOF 207.46.12.204 - - [01/Dec/2010:11:48:00 -0800] "GET /deals/skullcandy-inkd-earbuds HTTP/1.1" 200 5732 "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; SLCC1; .NET CLR 1.1.4322; .NET CLR 2.0.40607; .NET CLR 3.0.04506.648)" "-" EOF sjsp = SkinnyJeans::StringParser.new(_string) assert_nil sjsp.all_urls assert_nil sjsp.get_search_keyword end def test_can_get_keyword_from_url _string=<<-EOF 98.244.200.209 - - [01/Dec/2010:11:51:26 -0800] "GET /deals/apple-ipod-touch HTTP/1.1" 200 11448 "http://www.google.com/m/search?oe=UTF-8&client=safari&hl=en&q=best+deals+for+the+4th+generation+iPod+touch+32+gb&gws_link_params=spell:1&ei=aqb2TJDBLqGutgfp862NAg&ved=0CBEQBSgA" "Mozilla/5.0 (iPod; U; CPU iPhone OS 4_1 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8B117 Safari/6531.22.7" "-" EOF sjsp = SkinnyJeans::StringParser.new(_string) assert_equal "best deals for the 4th generation ipod touch 32 gb", sjsp.get_search_keyword end def test_return_param_from_url assert_equal "nowai", SkinnyJeans::StringParser.return_param_from_valid_url_or_path("/deals/hp-dv6-laptop?ok=yes&yea=true&drc=nowai","drc") assert_equal "iDesign Tower Stereo System".downcase, SkinnyJeans::StringParser.return_param_from_valid_url_or_path("http://us.yhs.search.yahoo.com/if?partnerid=yhs-if-freecause&fr=yhs-if-freecause&ei=UTF-8&YST_b=21&tid=61613&uid=47727219&p=iDesign Tower Stereo System","p") assert_equal "yes", SkinnyJeans::StringParser.return_param_from_valid_url_or_path("/deals/hp-dv6-laptop?ok=yes&yea=true&drc=nowai","ok") assert_equal "yes", SkinnyJeans::StringParser.return_param_from_valid_url_or_path("http://dealzon.com/deals/hp-dv6-laptop?ok=yes&yea=true&drc=nowai","ok") assert_equal "nowai", SkinnyJeans::StringParser.return_param_from_valid_url_or_path("http://dealzon.com/deals/hp-dv6-laptop?ok=yes&yea=true&drc=nowai","drc") assert_equal "hp coupon", SkinnyJeans::StringParser.return_param_from_valid_url_or_path("http://search.yahoo.com/search;_ylt=AqnIgbSoqn0rhe69ABoUdv.bvZx4?p=hp+coupon&toggle=1&cop=mss&ei=UTF-8&fr=yfp-t-701","p") assert_equal "hp coupon", SkinnyJeans::StringParser.return_param_from_valid_url_or_path("http://search.yahoo.com/search;_ylt=A0oG7h6HJPtMlGIBsFyl87UF;_ylc=X1MDMjE0MjQ3ODk0OARfcgMyBGZyA3NmcARuX2dwcwMxMARvcmlnaW4Dc3ljBHF1ZXJ5A2hwIGNvdXBvbgRzYW8DMQ--?p=hp+coupon&fr=sfp&fr2=&iscqry=","p") assert_equal "hp coupon", SkinnyJeans::StringParser.return_param_from_valid_url_or_path("http://www.ask.com/web?q=hp+coupon&search=&qsrc=0&o=0&l=dir","q") assert_equal "hp coupon", SkinnyJeans::StringParser.return_param_from_valid_url_or_path("http://search.aol.com/aol/search?enabled_terms=&s_it=comsearch50&q=hp+coupon","q") assert_equal "hp coupns", SkinnyJeans::StringParser.return_param_from_valid_url_or_path("http://www.bing.com/search?q=hp+coupns&go=&form=QBLH&qs=n&sk=&sc=8-6","q") assert_equal "dv6 coupon", SkinnyJeans::StringParser.return_param_from_valid_url_or_path("http://www.google.com/url?sa=t&source=web&cd=1&ved=0CCEQFjAA&url=http%3A%2F%2Fdealzon.com%2Fdeals%2Fhp-dv6-laptop&rct=j&q=dv6%20coupon&ei=1Pz7TKb5AoP7lwfEoeSgBQ&usg=AFQjCNGFVL4PvZ59hbxkCFVdmHMayEe3UQ&sig2=Vs7s9a1z2Elm23NVffMJ8A","q") assert_equal "dv6 coupon", SkinnyJeans::StringParser.return_param_from_valid_url_or_path("http://www.google.com/url?sa=t&source=web&cd=1&sqi=2&ved=0CCAQFjAA&url=http%3A%2F%2Fdealzon.com%2Fdeals%2Fhp-dv6-laptop&rct=j&q=dv6%20coupon&ei=xyT7TO7kEYGKlwfT9tSPDA&usg=AFQjCNGFVL4PvZ59hbxkCFVdmHMayEe3UQ&sig2=7pnUG3YTE8sHONsIqLl1sg","q") assert_equal "dv6 coupon", SkinnyJeans::StringParser.return_param_from_valid_url_or_path("http://www.google.com/url?sa=t&source=web&cd=1&sqi=2&ved=0CCAQFjAA&url=http%3A%2F%2Fdealzon.com%2Fdeals%2Fhp-dv6-laptop&rct=j&q=dv6%20coupon%20&ei=xyT7TO7kEYGKlwfT9tSPDA&usg=AFQjCNGFVL4PvZ59hbxkCFVdmHMayEe3UQ&sig2=7pnUG3YTE8sHONsIqLl1sg","q") end def test_extract_search_query assert_equal "\"dealzon\"", SkinnyJeans::StringParser.extract_search_query("http://www.google.com/search?client=safari&rls=en&q=%22dealzon%22&ie=UTF-8&oe=UTF-8") assert_equal "\'dealzon\'", SkinnyJeans::StringParser.extract_search_query("http://www.google.com/search?client=safari&rls=en&q='dealzon'&ie=UTF-8&oe=UTF-8") end end