From f862cf2222221ce7463856499a668a6c17b1d22f Mon Sep 17 00:00:00 2001 From: "neiljp (Neil Pilgrim)" Date: Mon, 18 Sep 2017 21:12:56 -0700 Subject: [PATCH] wikipedia bot: encode urls correctly in http request & adjust tests. --- .../wikipedia/fixtures/test_hash_query.json | 22 +++++++++++++++++++ .../fixtures/test_incorrect_query.json | 2 +- .../wikipedia/fixtures/test_multi_word.json | 2 +- .../bots/wikipedia/test_wikipedia.py | 9 ++++++++ .../zulip_bots/bots/wikipedia/wikipedia.py | 4 +++- 5 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_hash_query.json diff --git a/zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_hash_query.json b/zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_hash_query.json new file mode 100644 index 0000000..7441f4d --- /dev/null +++ b/zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_hash_query.json @@ -0,0 +1,22 @@ +{ + "request": { + "api_url":"https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=%23&format=json" + }, + "response": { + "data": { + "status_code":200 + }, + "query": { + "search": [ + { + "title":"Number sign" + } + ] + } + }, + "response-headers":{ + "status":200, + "ok":true, + "content-type":"application/json; charset=utf-8" + } +} diff --git a/zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_incorrect_query.json b/zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_incorrect_query.json index 46ba8c2..b8e4cc7 100644 --- a/zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_incorrect_query.json +++ b/zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_incorrect_query.json @@ -1,6 +1,6 @@ { "request": { - "api_url":"https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=sssssss kkkkk&format=json" + "api_url":"https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=sssssss%20kkkkk&format=json" }, "response": { "data": { diff --git a/zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_multi_word.json b/zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_multi_word.json index 948463d..5195fe2 100644 --- a/zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_multi_word.json +++ b/zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_multi_word.json @@ -1,6 +1,6 @@ { "request": { - "api_url":"https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=The sky is blue&format=json" + "api_url":"https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=The%20sky%20is%20blue&format=json" }, "response": { "data": { diff --git a/zulip_bots/zulip_bots/bots/wikipedia/test_wikipedia.py b/zulip_bots/zulip_bots/bots/wikipedia/test_wikipedia.py index 799bb2c..cf72119 100755 --- a/zulip_bots/zulip_bots/bots/wikipedia/test_wikipedia.py +++ b/zulip_bots/zulip_bots/bots/wikipedia/test_wikipedia.py @@ -37,6 +37,15 @@ class TestWikipediaBot(BotTestCase): expected_method='send_reply' ) + # Hash query + bot_response = "For search term \"#\", https://en.wikipedia.org/wiki/Number_sign" + with self.mock_http_conversation('test_hash_query'): + self.assert_bot_response( + message = {'content': '#'}, + response = {'content': bot_response}, + expected_method='send_reply' + ) + # Incorrect word bot_response = "I am sorry. The search term you provided is not found :slightly_frowning_face:" with self.mock_http_conversation('test_incorrect_query'): diff --git a/zulip_bots/zulip_bots/bots/wikipedia/wikipedia.py b/zulip_bots/zulip_bots/bots/wikipedia/wikipedia.py index 506695a..0fa5ad3 100644 --- a/zulip_bots/zulip_bots/bots/wikipedia/wikipedia.py +++ b/zulip_bots/zulip_bots/bots/wikipedia/wikipedia.py @@ -3,6 +3,7 @@ from __future__ import print_function import requests import logging import re +from six.moves import urllib # See readme.md for instructions on running this code. @@ -41,7 +42,8 @@ class WikipediaHandler(object): if query == '': return help_text query_wiki_link = ('https://en.wikipedia.org/w/api.php?action=query&' - 'list=search&srsearch=%s&format=json' % (query,)) + 'list=search&srsearch=%s&format=json' + % (urllib.parse.quote(query),)) try: data = requests.get(query_wiki_link) except requests.exceptions.RequestException: