test_startpage.py 5.68 KB
Newer Older
Cqoicebordel's avatar
Cqoicebordel committed
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
# -*- 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, '[]')

Adam Tauber's avatar
Adam Tauber committed
30
        response = mock.Mock(text='<html></html>')
Cqoicebordel's avatar
Cqoicebordel committed
31
32
33
        self.assertEqual(startpage.response(response), [])

        html = """
34
        <li class="search-result search-item">
Cqoicebordel's avatar
Cqoicebordel committed
35
36
37
38
39
40
            <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>
41
            <p class="search-item__body">
Cqoicebordel's avatar
Cqoicebordel committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
                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>
59
        </li>
Cqoicebordel's avatar
Cqoicebordel committed
60
        """
Adam Tauber's avatar
Adam Tauber committed
61
        response = mock.Mock(text=html.encode('utf-8'))
Cqoicebordel's avatar
Cqoicebordel committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
        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 = """
        <div class='result' style=' *width : auto; *margin-right : 10%;'>
            <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>
77
            <p class='desc clk'>
Cqoicebordel's avatar
Cqoicebordel committed
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
                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>
        </div>
        <div class='result' style=' *width : auto; *margin-right : 10%;'>
            <h3>
                <span id='title_stars_2' name='title_stars_2'>  </span>
            </h3>
100
            <p class='desc clk'>
Cqoicebordel's avatar
Cqoicebordel committed
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
                This should be the content.
            </p>
            <p>
                <span class='url'>www.speed<b>test</b>.net/fr/
                </span>
            </p>
        </div>
        <div class='result' style=' *width : auto; *margin-right : 10%;'>
            <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>
        </div>
        """
Adam Tauber's avatar
Adam Tauber committed
132
        response = mock.Mock(text=html.encode('utf-8'))
Cqoicebordel's avatar
Cqoicebordel committed
133
134
135
136
        results = startpage.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 1)
        self.assertEqual(results[0]['content'], '')