1
2"""
3Tests for L{nevow.useragent}.
4"""
5
6from twisted.trial.unittest import TestCase
7
8from nevow.useragent import UserAgent, browsers
9
10
11class UserAgentTests(TestCase):
12    """
13    Tests for L{UserAgent}.
14    """
15    def test_parseNetscape71(self):
16        """
17        L{UserAgent.parse_GECKO} should return a UserAgent instance for a
18        Netscape 7.1 User-Agent string.
19        """
20        agent = UserAgent.parse_GECKO(
21            'Mozilla/5.0 (Windows; U; Windows NT 5.1; ja-JP; rv:1.4) '
22            'Gecko/20030624 Netscape/7.1 (ax)')
23        self.assertEqual(agent.browser, browsers.GECKO)
24        self.assertEqual(agent.version, (20030624,))
25
26
27    def test_parseFirefox15(self):
28        """
29        L{UserAgent.parse_GECKO} should return a UserAgent instance for a
30        Firefox 1.5 User-Agent string.
31        """
32        agent = UserAgent.parse_GECKO(
33            'Mozilla/5.0 (Windows; U; Windows NT 5.1; en; rv:1.8.0.3) '
34            'Gecko/20060426 Firefox/1.5.0.3')
35        self.assertEqual(agent.browser, browsers.GECKO)
36        self.assertEqual(agent.version, (20060426,))
37
38
39    def test_parseBonEcho(self):
40        """
41        L{UserAgent.parse_GECKO} should return a UserAgent instance for a
42        BonEcho Firefox 2.0 alpha User-Agent string.
43        """
44        agent = UserAgent.parse_GECKO(
45            'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1a2) '
46            'Gecko/20060512 BonEcho/2.0a2')
47        self.assertEqual(agent.browser, browsers.GECKO)
48        self.assertEqual(agent.version, (20060512,))
49
50
51    def test_parseFirefox20(self):
52        """
53        L{UserAgent.parse_GECKO} should return a UserAgent instance for a
54        Firefox 2.0 User-Agent string.
55        """
56        agent = UserAgent.parse_GECKO(
57            'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.4) '
58            'Gecko/20070515 Firefox/2.0.0.4')
59        self.assertEqual(agent.browser, browsers.GECKO)
60        self.assertEqual(agent.version, (20070515,))
61
62
63    def test_parseExplorer45(self):
64        """
65        L{UserAgent.parse_MSIE} should return a UserAgent instance for an
66        Internet Explorer 4.5 User-Agent string.
67        """
68        agent = UserAgent.parse_MSIE(
69            'Mozilla/4.0 (compatible; MSIE 4.5; Windows 98; Win 9x 4.8410008)')
70        self.assertEqual(agent.browser, browsers.INTERNET_EXPLORER)
71        self.assertEqual(agent.version, (4, 5))
72
73
74    def test_parseExplorer55(self):
75        """
76        L{UserAgent.parse_MSIE} should return a UserAgent instance for an
77        Internet Explorer 5.5 User-Agent string.
78        """
79        agent = UserAgent.parse_MSIE(
80            'Mozilla/5.0 (compatible; MSIE 5.5; Windows 98; Win 9x 4.1704896)')
81        self.assertEqual(agent.browser, browsers.INTERNET_EXPLORER)
82        self.assertEqual(agent.version, (5, 5))
83
84
85    def test_parseExplorer65(self):
86        """
87        L{UserAgent.parse_MSIE} should return a UserAgent instance for an
88        Internet Explorer 6.5 User-Agent string.
89        """
90        agent = UserAgent.parse_MSIE(
91            'Mozilla/5.0 (compatible; MSIE 6.5; Windows 98; Win 9x 4.7654712)')
92        self.assertEqual(agent.browser, browsers.INTERNET_EXPLORER)
93        self.assertEqual(agent.version, (6, 5))
94
95
96    def test_parseOmniWeb607(self):
97        """
98        L{UserAgent.parse_WEBKIT} should return a UserAgent instance for an
99        OmniWeb User-Agent string.
100        """
101        agent = UserAgent.parse_WEBKIT(
102            'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-US) AppleWebKit/420+ '
103            '(KHTML, like Gecko, Safari/420) OmniWeb/v607.17')
104        self.assertEqual(agent.browser, browsers.WEBKIT)
105        self.assertEqual(agent.version, (420,))
106
107
108    def test_parseSafari20(self):
109        """
110        L{UserAgent.parse_WEBKIT} should return a UserAgent instance for a
111        Safari 2.0 User-Agent string.
112        """
113        agent = UserAgent.parse_WEBKIT(
114            'Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en) AppleWebKit/'
115            '418.9.1 (KHTML, like Gecko) Safari/419.3')
116        self.assertEqual(agent.browser, browsers.WEBKIT)
117        self.assertEqual(agent.version, (418, 9, 1))
118
119
120    def test_parseOpera9(self):
121        """
122        L{UserAgent.parse_OPERA} should return a UserAgent instance for an
123        Opera 9 User-Agent string.
124        """
125        agent = UserAgent.parse_OPERA('Opera/9.20 (Windows NT 6.0; U; en)')
126        self.assertEqual(agent.browser, browsers.OPERA)
127        self.assertEqual(agent.version, (9, 20))
128
129
130    def test_geckoParser(self):
131        """
132        It should be possible to invoke the Gecko parser via L{UserAgent.parse}
133        with an appropriate string.
134        """
135        agent = UserAgent.fromHeaderValue(
136            'Mozilla/5.0 (Windows; U; Windows NT 5.1; ja-JP; rv:1.4) '
137            'Gecko/20030624 Netscape/7.1 (ax)')
138        self.assertEqual(agent.browser, browsers.GECKO)
139
140
141    def test_webkitParser(self):
142        """
143        It should be possible to invoke the WebKit parser via
144        L{UserAgent.parse} with an appropriate string.
145        """
146        agent = UserAgent.fromHeaderValue(
147            'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-US) AppleWebKit/420+ '
148            '(KHTML, like Gecko, Safari/420) OmniWeb/v607.17')
149        self.assertEqual(agent.browser, browsers.WEBKIT)
150
151
152    def test_msieParser(self):
153        """
154        It should be possible to invoke the MSIE parser via L{UserAgent.parse}
155        with an appropriate string.
156        """
157        agent = UserAgent.fromHeaderValue(
158            'Mozilla/4.0 (compatible; MSIE 4.5; Windows 98; Win 9x 4.8410008)')
159        self.assertEqual(agent.browser, browsers.INTERNET_EXPLORER)
160
161
162    def test_operaParser(self):
163        """
164        It should be possible to invoke the Opera parser via L{UserAgent.parse}
165        with an appropriate string.
166        """
167        agent = UserAgent.fromHeaderValue('Opera/9.20 (Windows NT 6.0; U; en)')
168        self.assertEqual(agent.browser, browsers.OPERA)
169