summaryrefslogtreecommitdiff
path: root/tests/unit/engines/test_bing.py
blob: 48a5e744a8ba3ca654ae980d22a8ce255ce03406 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
from collections import defaultdict
import mock
from searx.engines import bing
from searx.testing import SearxTestCase


class TestBingEngine(SearxTestCase):

    def test_request(self):
        bing.supported_languages = ['en', 'fr', 'zh-CHS', 'zh-CHT', 'pt-PT', 'pt-BR']
        query = u'test_query'
        dicto = defaultdict(dict)
        dicto['pageno'] = 0
        dicto['language'] = 'fr-FR'
        params = bing.request(query.encode('utf-8'), dicto)
        self.assertTrue('url' in params)
        self.assertTrue(query in params['url'])
        self.assertTrue('language%3AFR' in params['url'])
        self.assertTrue('bing.com' in params['url'])

    def test_response(self):
        self.assertRaises(AttributeError, bing.response, None)
        self.assertRaises(AttributeError, bing.response, [])
        self.assertRaises(AttributeError, bing.response, '')
        self.assertRaises(AttributeError, bing.response, '[]')

        response = mock.Mock(text='<html></html>')
        self.assertEqual(bing.response(response), [])

        response = mock.Mock(text='<html></html>')
        self.assertEqual(bing.response(response), [])

        html = """
        <div class="sa_cc" u="0|5109|4755453613245655|UAGjXgIrPH5yh-o5oNHRx_3Zta87f_QO">
            <div Class="sa_mc">
                <div class="sb_tlst">
                    <h3>
                        <a href="http://this.should.be.the.link/" h="ID=SERP,5124.1">
                        <strong>This</strong> should be the title</a>
                    </h3>
                </div>
                <div class="sb_meta"><cite><strong>this</strong>.meta.com</cite>
                    <span class="c_tlbxTrg">
                        <span class="c_tlbxH" H="BASE:CACHEDPAGEDEFAULT" K="SERP,5125.1">
                        </span>
                    </span>
                </div>
                <p><strong>This</strong> should be the content.</p>
            </div>
        </div>
        """
        response = mock.Mock(text=html)
        results = bing.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 1)
        self.assertEqual(results[0]['title'], 'This should be the title')
        self.assertEqual(results[0]['url'], 'http://this.should.be.the.link/')
        self.assertEqual(results[0]['content'], 'This should be the content.')

        html = """
        <li class="b_algo" u="0|5109|4755453613245655|UAGjXgIrPH5yh-o5oNHRx_3Zta87f_QO">
            <div Class="sa_mc">
                <div class="sb_tlst">
                    <h2>
                        <a href="http://this.should.be.the.link/" h="ID=SERP,5124.1">
                        <strong>This</strong> should be the title</a>
                    </h2>
                </div>
                <div class="sb_meta"><cite><strong>this</strong>.meta.com</cite>
                    <span class="c_tlbxTrg">
                        <span class="c_tlbxH" H="BASE:CACHEDPAGEDEFAULT" K="SERP,5125.1">
                        </span>
                    </span>
                </div>
                <p><strong>This</strong> should be the content.</p>
            </div>
        </li>
        """
        response = mock.Mock(text=html)
        results = bing.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 1)
        self.assertEqual(results[0]['title'], 'This should be the title')
        self.assertEqual(results[0]['url'], 'http://this.should.be.the.link/')
        self.assertEqual(results[0]['content'], 'This should be the content.')

    def test_fetch_supported_languages(self):
        html = """<html></html>"""
        response = mock.Mock(text=html)
        results = bing._fetch_supported_languages(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 0)

        html = """
        <html>
            <body>
                <form>
                    <div id="limit-languages">
                        <div>
                            <div><input id="es" value="es"></input></div>
                        </div>
                        <div>
                            <div><input id="pt_BR" value="pt_BR"></input></div>
                            <div><input id="pt_PT" value="pt_PT"></input></div>
                        </div>
                    </div>
                </form>
            </body>
        </html>
        """
        response = mock.Mock(text=html)
        languages = bing._fetch_supported_languages(response)
        self.assertEqual(type(languages), list)
        self.assertEqual(len(languages), 3)
        self.assertIn('es', languages)
        self.assertIn('pt-BR', languages)
        self.assertIn('pt-PT', languages)