From b8dde0300030fd7575faa057eb637c1fe8689988 Mon Sep 17 00:00:00 2001 From: mutantmonkey Date: Sat, 7 Sep 2013 15:58:47 -0700 Subject: [PATCH] hs: handle case where query is <= 2 characters --- modules/hs.py | 5 +++++ modules/test/test_hs.py | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/modules/hs.py b/modules/hs.py index c72eb78..c9fbbb9 100644 --- a/modules/hs.py +++ b/modules/hs.py @@ -21,6 +21,11 @@ def search(query): except (web.ConnectionError, web.HTTPError): raise GrumbleError("THE INTERNET IS FUCKING BROKEN. Please try again later.") + # apparently the failure mode if you search for <3 characters is a blank + # XML page... + if len(r) <= 0: + return False + xml = lxml.etree.fromstring(r.encode('utf-8')) results = xml.findall('{0}directory-entries/{0}entry'.format(NS)) if len(results) <= 0: diff --git a/modules/test/test_hs.py b/modules/test/test_hs.py index 7214ebd..c305fdf 100644 --- a/modules/test/test_hs.py +++ b/modules/test/test_hs.py @@ -40,6 +40,13 @@ class TestHs(unittest.TestCase): out = self.phenny.reply.call_args[0][0] self.assertRegex(out, pattern) + def test_2char(self): + input = Mock(group=lambda x: 'hs') + hs(self.phenny, input) + + out = self.phenny.reply.call_args[0][0] + self.phenny.reply.assert_called_once_with("No results found") + def test_none(self): input = Mock(group=lambda x: 'THIS_IS_NOT_A_REAL_SEARCH_QUERY') hs(self.phenny, input)