test_www1x.py 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. from collections import defaultdict
  2. import mock
  3. from searx.engines import www1x
  4. from searx.testing import SearxTestCase
  5. class TestWww1xEngine(SearxTestCase):
  6. def test_request(self):
  7. query = 'test_query'
  8. params = www1x.request(query, defaultdict(dict))
  9. self.assertTrue('url' in params)
  10. self.assertTrue(query in params['url'])
  11. self.assertTrue('1x.com' in params['url'])
  12. def test_response(self):
  13. self.assertRaises(AttributeError, www1x.response, None)
  14. self.assertRaises(AttributeError, www1x.response, [])
  15. self.assertRaises(AttributeError, www1x.response, '')
  16. self.assertRaises(AttributeError, www1x.response, '[]')
  17. response = mock.Mock(text='<html></html>')
  18. self.assertEqual(www1x.response(response), [])
  19. html = """
  20. <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE characters
  21. [
  22. <!ELEMENT characters (character*) >
  23. <!ELEMENT character (#PCDATA ) >
  24. <!ENTITY iexcl "&#161;" >
  25. <!ENTITY cent "&#162;" >
  26. <!ENTITY pound "&#163;" >
  27. ]
  28. ><root><searchresult><![CDATA[<table border="0" cellpadding="0" cellspacing="0" width="100%">
  29. <tr>
  30. <td style="min-width: 220px;" valign="top">
  31. <div style="font-size: 30px; margin: 0px 0px 20px 0px;">Photos</div>
  32. <div>
  33. <a href="/photo/123456" class="dynamiclink">
  34. <img border="0" class="searchresult" src="/images/user/testimage-123456.jpg" style="width: 125px; height: 120px;">
  35. </a>
  36. <a title="sjoerd lammers street photography" href="/member/sjoerdlammers" class="dynamiclink">
  37. <img border="0" class="searchresult" src="/images/profile/60c48b394c677d2fa4d9e7d263aabf44-square.jpg">
  38. </a>
  39. </div>
  40. </td>
  41. </table>
  42. ]]></searchresult></root>
  43. """
  44. response = mock.Mock(text=html)
  45. results = www1x.response(response)
  46. self.assertEqual(type(results), list)
  47. self.assertEqual(len(results), 1)
  48. self.assertEqual(results[0]['url'], 'https://1x.com/photo/123456')
  49. self.assertEqual(results[0]['thumbnail_src'], 'https://1x.com/images/user/testimage-123456.jpg')
  50. self.assertEqual(results[0]['content'], '')
  51. self.assertEqual(results[0]['template'], 'images.html')