# -*- coding: utf-8 -*- from collections import defaultdict import mock import lxml from searx.engines import google from searx.testing import SearxTestCase class TestGoogleEngine(SearxTestCase): def test_request(self): query = 'test_query' dicto = defaultdict(dict) dicto['pageno'] = 1 dicto['language'] = 'fr_FR' params = google.request(query, dicto) self.assertIn('url', params) self.assertIn(query, params['url']) self.assertIn('google.com', params['url']) self.assertIn('PREF', params['cookies']) self.assertIn('fr', params['headers']['Accept-Language']) dicto['language'] = 'all' params = google.request(query, dicto) self.assertIn('en', params['headers']['Accept-Language']) def test_response(self): self.assertRaises(AttributeError, google.response, None) self.assertRaises(AttributeError, google.response, []) self.assertRaises(AttributeError, google.response, '') self.assertRaises(AttributeError, google.response, '[]') response = mock.Mock(text='') self.assertEqual(google.response(response), []) html = """
  • This is the title

    test.psychologies.com/
    This should be the content.
  • This

  • This is

  • This is the

  • This is the

  • suggestion title

    """ response = mock.Mock(text=html) results = google.response(response) self.assertEqual(type(results), list) self.assertEqual(len(results), 2) self.assertEqual(results[0]['title'], 'This is the title') self.assertEqual(results[0]['url'], 'http://this.should.be.the.link/') self.assertEqual(results[0]['content'], 'This should be the content.') self.assertEqual(results[1]['suggestion'], 'suggestion title') html = """
  • """ response = mock.Mock(text=html) results = google.response(response) self.assertEqual(type(results), list) self.assertEqual(len(results), 0) def test_parse_images(self): html = """
  • """ dom = lxml.html.fromstring(html) results = google.parse_images(dom) self.assertEqual(type(results), list) self.assertEqual(len(results), 1) self.assertEqual(results[0]['url'], 'http://this.is.the.url/') self.assertEqual(results[0]['title'], '') self.assertEqual(results[0]['content'], '') self.assertEqual(results[0]['img_src'], 'https://this.is.the.image/image.jpg')