summaryrefslogtreecommitdiff
path: root/tests/unit/engines/test_startpage.py
blob: 4a8a443ddd32ea7c6ae6e50d63676b6ffc1a5925 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# -*- coding: utf-8 -*-
from collections import defaultdict
import mock
from searx.engines import startpage
from searx.testing import SearxTestCase


class TestStartpageEngine(SearxTestCase):

    def test_request(self):
        query = 'test_query'
        dicto = defaultdict(dict)
        dicto['pageno'] = 1
        dicto['language'] = 'fr_FR'
        params = startpage.request(query, dicto)
        self.assertIn('url', params)
        self.assertIn('startpage.com', params['url'])
        self.assertIn('data', params)
        self.assertIn('query', params['data'])
        self.assertIn(query, params['data']['query'])
        self.assertIn('with_language', params['data'])
        self.assertIn('lang_fr', params['data']['with_language'])

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

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

        html = """
        <li class="search-result search-item">
            <h3>
                <a href='http://this.should.be.the.link/' id='title_2' name='title_2' >
                    This should be the title
                </a>
                <span id='title_stars_2' name='title_stars_2'>  </span>
            </h3>
            <p class="search-item__body">
                This should be the content.
            </p>
            <p>
                <span class='url'>www.speed<b>test</b>.net/fr/
                </span>
                  -
                <A class="proxy" id="proxy_link" HREF="https://ixquick-proxy.com/do/spg/proxy?ep=&edata=&ek=&ekdata="
                    class='proxy'>
                    Navigation avec Ixquick Proxy
                </A>
                    -
                <A HREF="https://ixquick-proxy.com/do/spg/highlight.pl?l=francais&c=hf&cat=web&q=test&rl=NONE&rid=
                    &hlq=https://startpage.com/do/search&mtabp=-1&mtcmd=process_search&mtlanguage=francais&mtengine0=
                    &mtcat=web&u=http:%2F%2Fwww.speedtest.net%2Ffr%2F" class='proxy'>
                    Mis en surbrillance
                </A>
            </p>
        </li>
        """
        response = mock.Mock(text=html.encode('utf-8'))
        results = startpage.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="search-result search-item">
            <h3>
                <a href='http://www.google.com/aclk?sa=l&ai=C' id='title_2' name='title_2' >
                    This should be the title
                </a>
                <span id='title_stars_2' name='title_stars_2'>  </span>
            </h3>
            <p class="search-item__body">
                This should be the content.
            </p>
            <p>
                <span class='url'>www.speed<b>test</b>.net/fr/
                </span>
                  -
                <A class="proxy" id="proxy_link" HREF="https://ixquick-proxy.com/do/spg/proxy?ep=&edata=&ek=&ekdata="
                    class='proxy'>
                    Navigation avec Ixquick Proxy
                </A>
                    -
                <A HREF="https://ixquick-proxy.com/do/spg/highlight.pl?l=francais&c=hf&cat=web&q=test&rl=NONE&rid=
                    &hlq=https://startpage.com/do/search&mtabp=-1&mtcmd=process_search&mtlanguage=francais&mtengine0=
                    &mtcat=web&u=http:%2F%2Fwww.speedtest.net%2Ffr%2F" class='proxy'>
                    Mis en surbrillance
                </A>
            </p>
        </li>
        <li class="search-result search-item">
            <h3>
                <span id='title_stars_2' name='title_stars_2'>  </span>
            </h3>
            <p class="search-item__body">
                This should be the content.
            </p>
            <p>
                <span class='url'>www.speed<b>test</b>.net/fr/
                </span>
            </p>
        </li>
        <li class="search-result search-item">
            <h3>
                <a href='http://this.should.be.the.link/' id='title_2' name='title_2' >
                    This should be the title
                </a>
                <span id='title_stars_2' name='title_stars_2'>  </span>
            </h3>
            <p>
                <span class='url'>www.speed<b>test</b>.net/fr/
                </span>
                  -
                <A class="proxy" id="proxy_link" HREF="https://ixquick-proxy.com/do/spg/proxy?ep=&edata=&ek=&ekdata="
                    class='proxy'>
                    Navigation avec Ixquick Proxy
                </A>
                    -
                <A HREF="https://ixquick-proxy.com/do/spg/highlight.pl?l=francais&c=hf&cat=web&q=test&rl=NONE&rid=
                    &hlq=https://startpage.com/do/search&mtabp=-1&mtcmd=process_search&mtlanguage=francais&mtengine0=
                    &mtcat=web&u=http:%2F%2Fwww.speedtest.net%2Ffr%2F" class='proxy'>
                    Mis en surbrillance
                </A>
            </p>
        </li>
        """
        response = mock.Mock(text=html.encode('utf-8'))
        results = startpage.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 1)
        self.assertEqual(results[0]['content'], '')