3 require File.dirname(__FILE__) + '/../lib/sitealizer/parser'
5 class SitealizerParserTest < Test::Unit::TestCase
7 def test_user_agent_platform
8 user_agent = "(Macintosh; U; PPC Mac OS X; en)"
9 assert_equal "Macintosh", Sitealizer::Parser::UserAgent.get_platform(user_agent)
11 user_agent = "(Windows; U; Windows NT 5.1; nl; rv:1.8)"
12 assert_equal "Windows", Sitealizer::Parser::UserAgent.get_platform(user_agent)
14 user_agent = "(X11; U; Linux i686; en-US; rv:1.8.0.2)"
15 assert_equal "Linux", Sitealizer::Parser::UserAgent.get_platform(user_agent)
17 user_agent = "(compatible; MSIE 5.0; SunOS 5.9 sun4u; X11)"
18 assert_equal "Sun Solaris", Sitealizer::Parser::UserAgent.get_platform(user_agent)
20 user_agent = "X11; U; FreeBSD i386; en-US; rv:1.7.8)"
21 assert_equal "FreeBSD", Sitealizer::Parser::UserAgent.get_platform(user_agent)
23 user_agent = "Some unknown user agent"
24 assert_equal "Other", Sitealizer::Parser::UserAgent.get_platform(user_agent)
27 def test_user_agent_browser
28 user_agent = "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/418.9.1 (KHTML, like Gecko) Safari/419.3"
29 assert_equal({:type => 'Safari', :version => nil},
30 Sitealizer::Parser::UserAgent.browser_info(user_agent))
32 user_agent = "Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.8.1) Gecko/20061026 BonEcho/2.0"
33 assert_equal({:type => 'Firefox BonEcho', :version => '2.0'},
34 Sitealizer::Parser::UserAgent.browser_info(user_agent))
36 user_agent = "Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 6.0)"
37 assert_equal({:type => 'MSIE', :version => '7.0'},
38 Sitealizer::Parser::UserAgent.browser_info(user_agent))
40 user_agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; nl; rv:1.8) Gecko/20051107 Firefox/1.5"
41 assert_equal({:type => 'Firefox', :version => '1.5'},
42 Sitealizer::Parser::UserAgent.browser_info(user_agent))
44 user_agent = "Mozilla/5.0 (Windows NT 5.1; U; en) Opera 8.50"
45 assert_equal({:type => 'Opera', :version => '8.50'},
46 Sitealizer::Parser::UserAgent.browser_info(user_agent))
48 user_agent = "Mozilla/4.0 (compatible; MSIE 6.0; America Online Browser 1.1; rev1.2; Windows NT 5.1;)"
49 assert_equal({:type => 'AOL', :version => '1.1'},
50 Sitealizer::Parser::UserAgent.browser_info(user_agent))
52 user_agent = "Mozilla/4.0 (compatible; MSIE 5.5; AOL 6.0; Windows 98)"
53 assert_equal({:type => 'AOL', :version => '6.0'},
54 Sitealizer::Parser::UserAgent.browser_info(user_agent))
56 user_agent = "Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.8.0.3) Gecko/20060427 Camino/1.0.1"
57 assert_equal({:type => 'Camino', :version => '1.0.1'},
58 Sitealizer::Parser::UserAgent.browser_info(user_agent))
60 user_agent = "Mozilla/5.0 (compatible; Konqueror/3.4; Linux) KHTML/3.4.2 (like Gecko)"
61 assert_equal({:type => 'Konqueror', :version => '3.4'},
62 Sitealizer::Parser::UserAgent.browser_info(user_agent))
64 user_agent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.7.13) Gecko/20050610 K-Meleon/0.9"
65 assert_equal({:type => 'K-Meleon', :version => '0.9'},
66 Sitealizer::Parser::UserAgent.browser_info(user_agent))
68 user_agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)"
69 assert_equal({:type => 'Netscape', :version => '7.1'},
70 Sitealizer::Parser::UserAgent.browser_info(user_agent))
72 user_agent = "Mozilla/4.0 (PSP (PlayStation Portable); 2.00)"
73 assert_equal({:type => 'PlayStation Portable (PSP)', :version => '2.00'},
74 Sitealizer::Parser::UserAgent.browser_info(user_agent))
76 user_agent = "Mozilla/5.0 (PLAYSTATION 3; 1.00)"
77 assert_equal({:type => 'PlayStation 3', :version => '1.00'},
78 Sitealizer::Parser::UserAgent.browser_info(user_agent))
80 user_agent = "Lynx/2.8.5rel.1 libwww-FM/2.14 SSL-MM/1.4.1 OpenSSL/0.9.8a"
81 assert_equal({:type => 'Lynx', :version => '2.8.5'},
82 Sitealizer::Parser::UserAgent.browser_info(user_agent))
84 user_agent = "Another user agent"
85 assert_equal({:type => 'Other', :version => nil},
86 Sitealizer::Parser::UserAgent.browser_info(user_agent))
90 def test_keyword_get_terms
91 referer = "http://search.yahoo.com/search?p=sitealizer&fr=yfp-t-501&toggle=1&cop=mss&ei=UTF-8"
92 assert_equal "sitealizer", Sitealizer::Parser::Keyword.get_terms(referer)
94 referer = "http://search.msn.com/results.aspx?srch=105&FORM=AS5&q=sitealizer"
95 assert_equal "sitealizer", Sitealizer::Parser::Keyword.get_terms(referer)
97 referer = "http://www.google.com/search?sourceid=navclient&ie=UTF-8&rlz=1T4DMUS_enUS202US205&q=sitealizer"
98 assert_equal "sitealizer", Sitealizer::Parser::Keyword.get_terms(referer)
100 referer = "http://search.aol.com/aolcom/search?invocationType=topsearchbox.search&query=sitealizer"
101 assert_equal "sitealizer", Sitealizer::Parser::Keyword.get_terms(referer)
103 referer = "http://alltheweb.com/search?cat=web&cs=iso88591&q=sitealizer&rys=0&itag=crv&_sb_lang=pref"
104 assert_equal "sitealizer", Sitealizer::Parser::Keyword.get_terms(referer)
106 referer = "http://www.ask.com/web?q=sitealizer&qsrc=0&o=333&l=dir"
107 assert_equal "sitealizer", Sitealizer::Parser::Keyword.get_terms(referer)
109 referer = "http://www.altavista.com/web/results?itag=ody&q=sitealizer&kgs=1&kls=0"
110 assert_equal "sitealizer", Sitealizer::Parser::Keyword.get_terms(referer)
113 def test_keyword_get_domain
114 referer = "http://search.yahoo.com/search?p=sitealizer&fr=yfp-t-501&toggle=1&cop=mss&ei=UTF-8"
115 assert_equal "search.yahoo.com", Sitealizer::Parser::Keyword.get_domain(referer, 'localhost')
117 referer = "http://search.msn.com/results.aspx?srch=105&FORM=AS5&q=sitealizer"
118 assert_equal "search.msn.com", Sitealizer::Parser::Keyword.get_domain(referer, 'localhost')
120 referer = "http://www.google.com/search?sourceid=navclient&ie=UTF-8&rlz=1T4DMUS_enUS202US205&q=sitealizer"
121 assert_equal "www.google.com", Sitealizer::Parser::Keyword.get_domain(referer, 'localhost')
123 referer = "http://search.aol.com/aolcom/search?invocationType=topsearchbox.search&query=sitealizer"
124 assert_equal "search.aol.com", Sitealizer::Parser::Keyword.get_domain(referer, 'localhost')
126 referer = "http://alltheweb.com/search?cat=web&cs=iso88591&q=sitealizer&rys=0&itag=crv&_sb_lang=pref"
127 assert_equal "alltheweb.com", Sitealizer::Parser::Keyword.get_domain(referer, 'localhost')
129 referer = "http://www.ask.com/web?q=sitealizer&qsrc=0&o=333&l=dir"
130 assert_equal "www.ask.com", Sitealizer::Parser::Keyword.get_domain(referer, 'localhost')
132 referer = "http://www.altavista.com/web/results?itag=ody&q=sitealizer&kgs=1&kls=0"
133 assert_equal "www.altavista.com", Sitealizer::Parser::Keyword.get_domain(referer, 'localhost')
136 def test_robot_get_name
137 user_agent = "Atomz/1.0"
138 assert_equal "Atomz.com", Sitealizer::Parser::Robot.get_name(user_agent)
140 user_agent = "Googlebot/2.X (+http://www.googlebot.com/bot.html)"
141 assert_equal "Googlebot", Sitealizer::Parser::Robot.get_name(user_agent)
143 user_agent = "InfoSeek Robot 1.0"
144 assert_equal "InfoSeek", Sitealizer::Parser::Robot.get_name(user_agent)
146 user_agent = "Mozilla/2.0 (compatible; Ask Jeeves/Teoma)"
147 assert_equal "Ask Jeeves", Sitealizer::Parser::Robot.get_name(user_agent)
149 user_agent = "Lycos/x."
150 assert_equal "Lycos", Sitealizer::Parser::Robot.get_name(user_agent)
152 user_agent = "MSNBOT/0.1 (http://search.msn.com/msnbot.htm)"
153 assert_equal "MSNBot", Sitealizer::Parser::Robot.get_name(user_agent)
155 user_agent = "Slurp/2.0"
156 assert_equal "Inktomi", Sitealizer::Parser::Robot.get_name(user_agent)
158 user_agent = "Mozilla/5.0 (compatible; Yahoo! Slurp; http://help.yahoo.com/help/us/ysearch/slurp )"
159 assert_equal "Yahoo Slurp", Sitealizer::Parser::Robot.get_name(user_agent)
162 def test_language_get_name
163 [["en","English"],["pt-pt","Portuguese"],["en-nz","English/New Zealand"],["es-mx","Spanish/Mexico",],
164 ["ru-ru","Russian"],["de-at","German/Austria"],["fr-fr","French/France"],["zh-cn","Chinese/China"]].each do |l|
165 assert_equal l[1], Sitealizer::Parser::Language.get_name(l[0])